[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
%left and precedence
From: |
Bernd Prager |
Subject: |
%left and precedence |
Date: |
Wed, 4 Dec 2002 15:48:49 -0500 |
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:
---- snip ----------------
%type <str> object word sequence term
%token <str> WORD
%left '-'
%start object
%%
object: word
{
printf( "object: (word >%s<)\n", $1);
$$ = strdup($1);
}
| sequence
{
;
printf ("object: (sequence >%s<)\n", $1);
$$ = strdup($1);
}
| term
{
printf ("object: (term >%s<)\n", $1);
$$ = strdup($1);
}
;
word: WORD
{
printf( "word: >%s<\n", $1);
$$ = strdup($1);
}
;
sequence: object '-' object
{
printf( "sequence: >%s<->%s<\n", $1, $3);
$$ = (char *)malloc(strlen($1)+strlen($3)+2);
sprintf($$, "%s-%s", $1, $3);
}
;
term: object object
{
char buf[1024];
printf( "term: (word >%s< object >%s<)\n", $1, $2);
$$ = (char *)malloc(strlen($1)+strlen($2)+2);
sprintf($$, "%s %s", $1, $2);
}
;
---- snip ----------------
I thought the precedence rule (%left '-') would take care
of this. But when I try my grammar with the teststring "New-York is fun",
I get:
---- snip ----------------
word: >New<
object: (word >New<)
word: >York<
object: (word >York<)
word: >is<
object: (word >is<)
word: >fun<
object: (word >fun<)
term: (word >is< object >fun<)
object: (term >is fun<)
term: (word >York< object >is fun<)
object: (term >York is fun<)
sequence: >New<->York is fun<
object: (sequence >New-York is fun<)
---- snip ----------------
I do not want the sequence >New<->York is fun<.
I want the sequence >New<->York< ...
I do understand that as soon as "York" is pushed from the
stack, bison thinks it is a part of the term "York is fun" and removes it.
Is there a way to achieve what I want with precedence rules or
is that one of the look-ahead things bison can't do?
Thanks,
-- Bernd