Related articles |
---|
Yacc precedence bug slv@seg.npl.co.uk (S Voogd10 Jan 91 11:20 GMT) (1991-01-10) |
Re: Yacc precedence bug megatest!djones@decwrl.dec.com (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 thorinn@diku.dk (Lars Henrik Mathiesen) (1991-01-11) |
Re: Yacc precedence bug klaus%ipkima.hanse.de@RELAY.CS.NET (Klaus Thalhofer) (1991-01-14) |
Newsgroups: | comp.compilers |
From: | corbett@road.Eng.Sun.COM (Robert Corbett) |
Keywords: | yacc, parse, debug |
Organization: | Sun Microsystems, Mt. View, Ca. |
References: | <7993.9101101030@guava.seg.npl.co.uk> |
Date: | 11 Jan 91 05:37:44 GMT |
In article <7993.9101101030@guava.seg.npl.co.uk> S Voogd <slv@seg.npl.co.uk> writes:
>Dear comp.compiler-readers,
>
>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
set is
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
expected.
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.
Yours truly,
Bob Corbett
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.