help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: %left and precedence


From: Bernd Prager
Subject: Re: %left and precedence
Date: Wed, 4 Dec 2002 19:41:54 -0500

----- Original Message ----- 
From: "Jonathon Duerig" <address@hidden>
Sent: Wednesday, December 04, 2002 6:52 PM
...

> -- Bernd Prager --
> Hi,
> I want words which are combined with a '-' character
> interpreted with a higher priority then uncombined words.
> So this is the grammar I tried:
> -- /Bernd Prager --
...
> I'm by no means an expert in this domain, I'd try something along the
> following lines:
> 
... 

Well, I solved the problem by rewriting my scanner and handle the 
space character (' ') now as a token. This works well in the first case.
But now I face another puzzling problem that doesn't seem to be
related at all.
I have also a list construct, delimited by commas.
bison eventually recognizes this construct, but not before telling me
it would be a sequence as well. Why is that happening?
-- snip ----------------
%type <str> object word sequence list listelem
%token <str> WORD
%left ' '
%left ','
%left '-'

%start object
%%


/* any possible object (with or without semantic value) */
object: word
       {
          printf( "object: (word >%s<)\n", $1);
          $$ = strdup($1);
       }
    |  sequence
       {
          printf ("object: (sequence >%s<)\n", $1);
          $$ = strdup($1);
       }
    |  list
       {
          printf ("object: (list >%s<)\n", $1);
          $$ = strdup($1);
       }
    ;

/* a single word */
word:  WORD
       {
          printf( "word: >%s<\n", $1);
          $$ = strdup($1);
       }
    ;

sequence:   object ' ' object
       {
          char buf[1024];
          printf( "sequence: (object >%s< object >%s<)\n", $1, $3);
          $$ = (char *)malloc(strlen($1)+strlen($3)+1);
          sprintf($$, "%s %s", $1, $3);
       }
    ;

listelem:   object ',' object
      {
          printf( "sequence: (object >%s< object >%s<)\n", $1, $3);
          $$ = (char *)malloc(strlen($1)+strlen($3)+1);
          sprintf($$, "%s,%s", $1, $3);
      }
    |   object ',' listelem
      {
          printf( "sequence: (object >%s< listelem >%s<)\n", $1, $3);
          $$ = (char *)malloc(strlen($1)+strlen($3)+1);
          sprintf($$, "%s,%s", $1, $3);
      }
    ;

list:   '(' listelem ')'
      {
          printf( "list: >%s<\n", $2);
          $$ = strdup($2);
      }
    ;

%%
-- snip ----------------
This is the output:
string to check: "(green, blue,red)"
word: >green<
object: (word >green<)
word: >blue<
object: (word >blue<)
word: >red<
object: (word >red<)
sequence: (object >blue< object >red<)
sequence: (object >green< listelem >blue,red<)
list: >green,blue,red<
object: (list >green,blue,red<)

The sequences are clearly wrong here!

(BTW, Thanks for all you patience with me here.)
-- Bernd





reply via email to

[Prev in Thread] Current Thread [Next in Thread]