Re: Yacc precedence bug

Klaus Thalhofer <klaus%ipkima.hanse.de@RELAY.CS.NET>
Mon, 14 Jan 91 11:54:36 MET

          From comp.compilers

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)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Klaus Thalhofer <klaus%ipkima.hanse.de@RELAY.CS.NET>
Keywords: yacc, parse, debug
Organization: Compilers Central
References: <7993.9101101030@guava.seg.npl.co.uk>
Date: Mon, 14 Jan 91 11:54:36 MET

S. Voogd (slv@seg.npl.co.uk) writes:
> While working with YACC, we came across a POSSIBLE BUG
> in the precedence mechanism of YACC.
> [...]
> %token NUMBER
> %left PLUS
> %left MUL
>
> expr : expr PLUS expr %prec MUL
> { $$ = $1 + $3; }
> | expr MUL expr %prec PLUS
> { $$ = $1 * $3; }
> | NUMBER


Why should this be a bug? `yacc' is doing exactly what you tell it to do!


You introduce two operators PLUS and MUL (left assoc.).


With the first use of %prec you tell that PLUS is to adopt the
precedence of MUL. Thus both symbol have equal precedence.
With the second use of %prec you tell that MUL is to adopt the
precedence of PLUS, which has no further effect at this point.


Anyway, the only possible result is what you got. Both operators have _equal_
precedence. Expressions are built from left to right (because of %left)
regardless of which symbol comes first.


The effect you _wanted_ to have can be obtained by interchanging the %left
lines as their order is significant for precedence or by associating equal
precedence with (a pair of) _different_ operator symbols in your grammar.


Good Luck
Klaus
--
= Klaus Thalhofer
= iP-Systems Phone: + 431 96541
= Alter Markt 1-2 FAX: + 431 96524
= D-2300 Kiel 1 email: klaus@ipkima.hanse.de
= GERMANY


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.