[antlr-interest] how to get hold of individual nodes in a tree parser

Gavin Lambert antlr at mirality.co.nz
Wed Feb 27 00:14:04 PST 2008


At 19:30 27/02/2008, Benjamin Shropshire wrote:
 >Jim Idle wrote:
 >> Can't you just do:
 >>
 >> name : ^(NAME (n=name_literal {Console.writeln($n.text);}  )+)
 >>
[...]
 >However in general that wouldn't be enough because I might want 
to
 >use break, continue, arbitrary index manipulation, take a second 
pass
 >through, put the loop in another loop or any number of odd 
things.
 >being able to do "foreach(Blob b in name_literal.Iterator)..." 
would be
 >of huge value.

I could be wrong (I haven't tested it), but doesn't += still work 
in tree parsers?  So:

   name : ^(NAME (x+=name_literal)+) { /* something with $x */ };

I'm not quite sure what the actual type of $x would be (you'd have 
to check the generated code), but I would imagine it should be a 
list of trees.  (Or possibly a tree itself.)



More information about the antlr-interest mailing list