[antlr-interest] Selectively suppressing tree output
David Smith
david.smith at cc.gatech.edu
Fri May 27 09:32:52 PDT 2011
I'm trying to parse an if statement of this form:
if a > b
a = b + 1
elseif a < b
b = a - b
c = 42
d = 4
else
b = 4
c = a - b
end
... using the following grammar snippet:
prog: body EOF!
;
body :
( (stat {if($stat.tree!=null)
System.out.println($stat.tree.toStringTree());}
| ifStat) {if($ifStat.tree!=null)
System.out.println($ifStat.tree.toStringTree());} )*
;
stat:
(ID GETS) => ID GETS expr NEWLINE -> ^(GETS ID expr)
| expr NEWLINE -> expr
| NEWLINE ->
;
ifStat
: IF^ expr body
(ELSEIF expr body)*
(ELSE body)?
END
;
... Amazingly enough, it's trying to work but with the following very
odd behavior. the tree output is like this:
(= a (+ b 1))
(= b (- a b))
(= c 42)
(= d 4)
(= b 4)
(= c (- a b))
(if (> a b) (= a (+ b 1)) elseif (< a b) (= b (- a b)) (= c 42) (= d
4) else (= b 4) (= c (- a b)) end)
... It seems to be putting all the body statements into the tree and
then putting them "correctly" into the (if ...) node.
Furthermore, if i have comments after the if statement, the (if ...)
node is repeated for each comment.
Is there a way to suppress unwanted tree output?
DMS
David M. Smith http://www.cc.gatech.edu/fac/David.Smith
Georgia Institute of Technology, College of Computing
Sent from my ASR-33 Teletype
More information about the antlr-interest
mailing list