Re: Yacc precedence bug

Klaus Thalhofer <>
Mon, 14 Jan 91 11:54:36 MET

          From comp.compilers

Related articles
Yacc precedence bug (S Voogd10 Jan 91 11:20 GMT) (1991-01-10)
Re: Yacc precedence bug megatest! (1991-01-11)
Re: Yacc precedence bug corbett@road.Eng.Sun.COM (1991-01-11)
Re: Yacc precedence bug (Stephen Adams) (1991-01-11)
Re: Yacc precedence bug (Lars Henrik Mathiesen) (1991-01-11)
Re: Yacc precedence bug (Klaus Thalhofer) (1991-01-14)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Klaus Thalhofer <>
Keywords: yacc, parse, debug
Organization: Compilers Central
References: <>
Date: Mon, 14 Jan 91 11:54:36 MET

S. Voogd ( 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; }

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 Thalhofer
= iP-Systems Phone: + 431 96541
= Alter Markt 1-2 FAX: + 431 96524
= D-2300 Kiel 1 email:


Post a followup to this message

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