[antlr-interest] Selectively suppressing tree output

Trevor John Thompson tijet at mac.com
Fri May 27 10:23:15 PDT 2011


You are getting exactly what you asked for.
"ifStat" recursively calls "body" which dumps the tree for every "stat".
I imagine an output action in "prog" will deliver what you are looking for.

On 2011 May 27, at 09:32, David Smith wrote:

> 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 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address

--
Trevor John Thompson    (425) 246-4023
net: tijet at me.com
Quidquid Latine dictum sit, altum videtur.



More information about the antlr-interest mailing list