|Yacc precedence bug email@example.com (S Voogd10 Jan 91 11:20 GMT) (1991-01-10)|
|Re: Yacc precedence bug firstname.lastname@example.org (1991-01-11)|
|Re: Yacc precedence bug corbett@road.Eng.Sun.COM (1991-01-11)|
|Re: Yacc precedence bug S.R.Adams@ecs.southampton.ac.uk (Stephen Adams) (1991-01-11)|
|Re: Yacc precedence bug email@example.com (Lars Henrik Mathiesen) (1991-01-11)|
|Re: Yacc precedence bug klaus%ipkima.hanse.de@RELAY.CS.NET (Klaus Thalhofer) (1991-01-14)|
|From:||corbett@road.Eng.Sun.COM (Robert Corbett)|
|Keywords:||yacc, parse, debug|
|Organization:||Sun Microsystems, Mt. View, Ca.|
|Date:||11 Jan 91 05:37:44 GMT|
In article <firstname.lastname@example.org> S Voogd <email@example.com> writes:
>While working with YACC, we came across a POSSIBLE BUG
>in the precedence mechanism of YACC.
>You can give a token a precedence and associativity by
>declaring this in the declaration-part of a YACC-file
>According to the manual you can also change the precedence
>and associativity of a token in the grammar-rules by
>using the '%prec'-mechanism.
>But it seems to us that this mechanism doesn't work!!!
Yacc is doing what it is defined to do. You might consider what it
does to be a design error, but it is not a bug.
The manual does not say that you can change the precedence and
associativity of a token in the grammar-rules by using the
'%prec'-mechanism. It says
A precedence and associativity is associated with each grammar
rule; it is the precedence and associativity of the last token
or literal in the body of the rule. If the %prec construction
is used, it overrides this default.
Thus, %prec changes the precedence and associativity of a grammar
rule, not of any token in the rule. Therefore, when you write
expr: expr PLUS expr %prec MUL
| expr MUL expr %prec PLUS
you change the precedence and associativity of the rules but not
of PLUS and MUL. Therefore, when you reach the state whose item
expr : expr . PLUS expr
expr : expr . MUL expr
expr : expr MUL expr .
the shift/reduce conflict on PLUS (between the first and third
items) is resolved as if the rule
expr : expr MUL expr
has the precedence and associativity of PLUS and the lookahead
symbol PLUS has the precedence and associativity of PLUS. Since
PLUS has been defined to be left associative, Yacc chooses to
reduce in this case. Thus, the results presented are to be
There is no way to override the precedence and associativity of
a lookahead symbol. If I knew specifically what you were trying
to do, I might be able to tell you how to do it.
Return to the
Search the comp.compilers archives again.