[antlr-interest] Parser - > Tree walker
Jim Idle
jimi at temporal-wave.com
Tue May 22 10:17:50 PDT 2007
Probably the easiest thing to do is change what you are producing for
the unary operators. You probably don't even need unary +:
Add
tokens
{
UNARYNEG;
}
After the options,
Then:
unaryExpression : '+' ue=unaryExpression -> $ue
| '-' ue=unaryExpression -> ^(UNARYNEG $ue)
| unaryExpressionNotPlusMinus
;
...
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of CARLOS MELGAREJO
Sent: Tuesday, May 22, 2007 10:10 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Parser - > Tree walker
Hi all,
I'm trying to create a grammar that recognize simple expressions and
other goodies and generate an evaluation at the end.
First I read "The definitive ANTLR Reference" book(for the version 3)
and I got all exited, and then after a lot of testing examples I was
encourage to start working in my project.
So, making the long story short this is my Lexer-Parser grammar look
like:
grammar Expert;
options {
output=AST;
ASTLabelType=CommonTree;
}
// Starting point
statement : expression ;
expression : conditionalAndExpression ( 'or'^ conditionalAndExpression
)* ;
conditionalAndExpression : exclusiveOrExpression ( 'and'^
exclusiveOrExpression )* ;
exclusiveOrExpression : equalityExpression ( 'xor'^ equalityExpression
)* ;
equalityExpression : relationalExpression (('='^ | '!='^)
relationalExpression)* ;
relationalExpression : additiveExpression ( ('<='^|'>='^|'<'^|'>'^)
additiveExpression )* ;
additiveExpression : multiplicativeExpression ( ('+'^ | '-'^)
multiplicativeExpression )* ;
multiplicativeExpression : unaryExpression ( ( '*'^ | '/'^ )
unaryExpression )* ;
unaryExpression : '+'^ unaryExpression
| '-'^ unaryExpression
| unaryExpressionNotPlusMinus
;
unaryExpressionNotPlusMinus
: 'not'^ unaryExpression
| atom
;
atom
: literal
| log
| Date
| '('! expression ')'!
;
literal
: FloatingPointLiteral
| StringLiteral
;
......................
So far so good, I'm able to create a pretty good AST, the only problem
that I'm facing with this grammar is that I build the following tree
node:
^('-' expr expr) // Arithmetic expression
^('-' expr) // Unary expression.
My problem is that I don't know how to walk this tree, because of the
ambiguity in the rule abode created with '-'.
How I can solve the problem with unary expressions?.
Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070522/977bf883/attachment.html
More information about the antlr-interest
mailing list