[antlr-interest] if-else if ast question...
pady
prabha_pady at comcast.net
Mon Dec 29 08:06:14 PST 2008
I have an if-else if grammar as follows ( showing only the main
sections )...
statement
: statementExpression ';' -> statementExpression
| 'if' boolExpression ifStatementBlock (options {k=1;}:'else'
elseStmt=statement)?
-> ^('if' boolExpression ifStatementBlock $elseStmt ?)
| compoundStatement
;
compoundStatement
: '{' statement* '}' -> statement*
;
ifStatementBlock
: statementExpression ';' -> statementExpression
| compoundStatement
;
boolExpression
: '(' expression ')' -> expression
;
...
...
When my input is
if ( field1.oldValue > 4 or
( field2.oldValue == "hello" and field3.newValue != "World" )
) {
loadUsersFromRole(field1.newValue);
} else if ( field2.newValue == "world" ) {
sendEmail("user1");
sendEmail("user2");
} else {
loadData("Hello World");
}
The generated ast is...
(if
(or
(> field1 . oldValue 4)
(and
(== field2 . oldValue "hello")
(!= field3 . newValue "World")
)
)
(loadUsersFromRole field1 . newValue)
(if
(== field2 . newValue "world")
(sendEmail "user1")
(sendEmail "user2")
(loadData "Hello World")
)
)
The problem I see are:
1. The else-if node is inside the "if" node.
2. The else node is inside the "else if" node.
How do I make this as if the 3 nodes appear so that one can walk the tree
and figure out that this is an if/else if/else block ?
Any ideas appreciated.
Thx
-- pady
More information about the antlr-interest
mailing list