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

Benjamin Shropshire shro8822 at vandals.uidaho.edu
Wed Feb 27 08:03:35 PST 2008


Gavin Lambert wrote:

> 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.)


I'll check, but IIRC someone said that using += in a tree parser wasn't 
exactly kosher. Maybe I'm miss remembering


More information about the antlr-interest mailing list