# Re: Implicit operator precedence

## SM Ryan <wyrmwif@tsoft.com>6 Jun 2004 15:12:12 -0400

From comp.compilers

Related articles
Implicit operator precedence sam@cleanstick.org (2004-05-30)
Re: Implicit operator precedence wyrmwif@tsoft.com (SM Ryan) (2004-06-06)
Re: Implicit operator precedence haberg@matematik.su.se (Hans Aberg) (2004-06-15)
| List of all articles for this month |

 From: SM Ryan Newsgroups: comp.compilers Date: 6 Jun 2004 15:12:12 -0400 Organization: Quick STOP Groceries References: 04-05-102 Keywords: parse Posted-Date: 06 Jun 2004 15:12:12 EDT

# When it came to implementing operator precedence, as a quick hack i
# worked out that instead of all the %left DIV MUL business, i could
# simply put the rules in order and resolve shift/reduce conflicts by
# the number of the rule. it's easier to explain through code:

Or you could use an archaic hack that doesn't involve introducing
ambiguities.

# expr: NUM { \$\$=\$1; }
# | SUB expr { \$\$=-\$2; }
# | expr EXP expr { \$\$=pow(\$1, \$3); }
# | expr DIV expr { \$\$=\$1/\$3; }
# | expr MUL expr { \$\$=\$1*\$3; }
# | expr ADD expr { \$\$=\$1+\$3; }
# | expr SUB expr { \$\$=\$1-\$3; }
# | OPEN expr CLOSE { \$\$=\$2; }
# ;

expr ::= expr1 | expr ADD expr1 | expr SUB expr1
expr1 ::= expr2 | expr1 MUL expr2 | expr1 DIV expr2
expr2 ::= expr3 | SUB expr2
expr3 ::= expr4 | expr4 EXP expr3
expr4 ::= NUM | OPEN expr CLOSE

--
SM Ryan http://www.rawbw.com/~wyrmwif/

Post a followup to this message