Re: How to process unary minus for constants by other way than expressions? (bison reduce/reduce conflict)

Sylvain Schmitz <schmitz@i3s.unice.fr>
14 Mar 2007 23:25:04 -0400

          From comp.compilers

Related articles
How to process unary minus for constants by other way than expressions marek@tynska.cuni.cz (Marek Peca) (2007-03-14)
Re: How to process unary minus for constants by other way than express schmitz@i3s.unice.fr (Sylvain Schmitz) (2007-03-14)
| List of all articles for this month |

From: Sylvain Schmitz <schmitz@i3s.unice.fr>
Newsgroups: comp.compilers
Date: 14 Mar 2007 23:25:04 -0400
Organization: Compilers Central
References: 07-03-049
Keywords: parse
Posted-Date: 14 Mar 2007 23:25:04 EDT

Marek Peca wrote:
> [...] then, I changed to
> -----
> expr: NUMBER
> | expr_nonnum
> ;
>
> expr_nonnum: '(' expr ')'
> | '(' expr ')'
> | expr '+' expr
> | expr '-' expr
> | expr '^' expr
> | '-' NUMBER %prec UNNUM
> | '-' expr_nonnum %prec UNEXP
> ;
> -----
>
> but without success.


What about the following? Unary minus has a stronger precedence anyhow.


expr: expr '+' expr
| expr '-' expr
| expr '^' expr
| NUMBER
| expr_unary
;


expr_unary: '-' NUMBER
| '-' expr_unary
                  | '(' expr ')'
;


I am afraid it won't solve all your issues, since "------3" will not
evaluate to a single "3" tree node. I didn't see any variables in your
syntax, and thus I do not quite understand why you should evaluate your
AST several times, as the result ought to remain the same.


On the other hand, if you do have variables in your expressions, then
you could distinguish variable expressions from constant ones in your
grammar with the same kind of trick. Nonetheless, I would favor
evaluating the constant parts of the expression AST over obfuscating
your grammar.


--
Hope that helps,


      Sylvain


Post a followup to this message

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