[antlr-interest] how to get hold of individual nodes in a tree parser
Benjamin Shropshire
shro8822 at vandals.uidaho.edu
Tue Feb 26 22:30:03 PST 2008
Jim Idle wrote:
> Can't you just do:
>
> name : ^(NAME (n=name_literal {Console.writeln($n.text);} )+)
>
> Jim
>
>
Um. I guess I can. I didn't known you could stuff action inside the tree.
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 guess I can make that work. It does however lead to another question,
how do I put in code that will run only if the tree matches [it wont run
on say (NAME name_literal not_name_literal) ] but will run befor the
code attached to the name_literal? This is because I need that code to
have side effects as part of setting stuff up for the other parts. It
would be a bit more effort on my part but I might be able to dump the
"only on real match" part. Either way the code would need to declare
variable that are in scope later. (I could swear I've seen that last
part but for the life of me I can't find where.)
Thanks
Benjamin Shropshire
P.S. Where in the manual is your answerer covered? I'm thinking the same
section ought to answer some of my other questions.
>> I'm still stumped
>>
>> Looking at the generated code it looks like the parsed subtrees are
>> discarded (or if I refer to name_literal all but the last one is). Is
>> this the the planned behavior?
>>
>> It seems that "look for a list of foo's and then let me at them" is
>> common enough that there would be some special way to make it easy.
>> Something like "$name_literal.List". Is this not the case?
>>
>> Am I approaching this wrong? I want to do stuff with the list of
>> name_literals. Working with each name_literal by it's self is useless
>> to me.
>>
More information about the antlr-interest
mailing list