Path: utzoo!utgpu!water!watmath!clyde!att!ucbvax!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.unix.wizards Subject: Re: Trouble with %prec in yacc Message-ID: <661@goofy.megatest.UUCP> Date: 14 Jul 88 20:48:08 GMT References: <237@h-three.UUCP> Organization: Megatest Corporation, San Jose, Ca Lines: 76 From article <237@h-three.UUCP>, by ned@h-three.UUCP (ned): ... > /* start of doesn't work */ > %token NAME MULT ADD > %left PREC_ADD > %left PREC_MULT > %start expr > > %% > > expr : expr MULT expr %prec PREC_MULT > | expr ADD expr %prec PREC_ADD > | NAME > ; > /* end of doesn't work */ > > doesn't (and compiles with 4 shift/reduce conflicts). Anyone know > what I'm doing wrong? Thanks in advance. > > -- Ned Robie I'm confused also. It looks to me as though the above should give ADD and MULT the same precedence. (That's probably not what you intended.) The PREC_ADD and PREC_MULT productions both have higher priorities than ADD and MULT. So reductions should always be prefered to shifts, right? I don't see why there should be shift/reduce conflicts. Here is part of the y.output file: 5: shift/reduce conflict (shift 3, red'n 1) on MULT 5: shift/reduce conflict (shift 4, red'n 1) on ADD state 5 expr : expr_MULT expr expr : expr MULT expr_ (1) expr : expr_ADD expr MULT shift 3 ADD shift 4 . reduce 1 I don't get it. Why does it not reduce by expr <= expr MULT expr on either ADD or MULT, each of which has a lower priority than PREC_MULT? Looks like a bug to me. Here's the way %prec is commonly used: %token NAME %left '+' '-' %left '*' '/' %start expr %% expr : expr mulop expr %prec '*' | expr addop expr %prec '+' | NAME ; mulop : '*' | '/' ; addop : '+' | '-' ;