help-bison
[Top][All Lists]
Advanced

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

Re: %left and precedence


From: Tomas Crhak
Subject: Re: %left and precedence
Date: Fri, 6 Dec 2002 10:20:37 +0100

On Wed, 4 Dec 2002 19:41:54 -0500
"Bernd Prager" <address@hidden> wrote:

> ----- 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?

What do you mean? The two output lines in the snippet at the bottom:

> sequence: (object >blue< object >red<)
> sequence: (object >green< listelem >blue,red<)

are there not because it is recognized as a sequence,
but as it is recognized as a list item and this is the
way the rule tells you that:

> 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);
>       }
>     ;
>

Or am I missing something?
Tomas

> -- 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
> 
> 
> 
> _______________________________________________
> address@hidden http://mail.gnu.org/mailman/listinfo/help-bison
> 




reply via email to

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