Re: Q: writing YACC rules

Chris Dodd <>
1 Sep 1998 22:17:01 -0400

          From comp.compilers

Related articles
Q: writing YACC rules (Bert Aerts) (1998-08-24)
Re: Q: writing YACC rules (Chris Clark USG) (1998-08-30)
Re: Q: writing YACC rules (1998-08-31)
Re: Q: writing YACC rules (Scott Stanchfield) (1998-08-31)
Re: Q: writing YACC rules (Chris Dodd) (1998-09-01)
| List of all articles for this month |

From: Chris Dodd <>
Newsgroups: comp.compilers
Date: 1 Sep 1998 22:17:01 -0400
Organization: Reservoir Labs
References: 98-08-178 98-08-196 98-08-204
Keywords: yacc, comment

> [You can do this in yacc by assigning precedence to tokens you use
> only in precedence declarations, and using those in %prec lines, for
> example:
> %left addprec mulprec
> ...
> expr: expr PLUS expr %addprec |
> expr TIMES expr %mulprec ;
> -John]

Unfortunately, this doesn't work -- at least for shift/reduce
conflicts as in the above example. To resolve these, yacc
compares the precedence of the TOKEN to be SHIFTed with the
precedence of the RULE to be REDUCED. If the tokens have no
precedence (only the rules, as above), then there's no resolution.

The problem in yacc is that there's no way of setting the
precedence of a token without also setting the precedence of
every rule that uses the token. Thus, precedence declarations
to resolve one conflict can easily end up hiding another one.
The only way I know of to get around this is to write a rule
for each token, and never use tokens directly in any other
rule. A `solution' that's probably worse than the original

Chris Dodd
[Oh, right. Oops. -John]

Post a followup to this message

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