[antlr-interest] [Tree grammar] Mismatch tree node

Mark Volkmann r.mark.volkmann at gmail.com
Sun Feb 10 13:43:00 PST 2008


On Feb 10, 2008 3:11 PM, Jakub Lambrych <lambrych at gmail.com> wrote:
> Hello Everybody!
>
> I would like to ask You whether exists in Tree Grammar a posibility of
> not walking through the whole AST tree (in my pass I'm only interested
> in data stored on the first level in the tree).

I haven't done this before, but I suspect the answer is to skip
writing a tree parser grammar and instead write Java code using the
ANTLR API to directly process the AST returned by the executing the
generated parser method for your start rule. If you want to try this
approach then you'll find this API documentation very useful.

http://antlr.org/api/

Call getTree() on the object returned by the start rule method. In the
default case, that returns a CommonTree object. I'm looking that the
Java API documentation. CommonTree inherits from BaseTree. BaseTree
has the methods getChildCount() and getChild(int i). That should get
you started.

> I'll give a working example:
>
> I only want to read the "label" staff
>
> <code>
> operation
>         : ^(OPERATION ^(LABEL label?) IDENTIFIER ^(ARGUMENTS
> arguments*))
>         ;
>
> label   : a=ETIQUETA {System.out.println($a.text);}
>         ;
>
> </code>
> but Anlr forces me to implement whole "walking" (implementing
> "arguments" rule, tha depends on another rule etc. etc.)
>
> My question is, whether exists a solution with what I could define the
> tree grammar more or less like:
> <code>
> operation: ^(OPERATION ^(LABEL label?) IDENTIFIER ARGUMENTS;
> label   : a=ETIQUETA {System.out.println($a.text);}
>         ;
> </code>
>
>
> Of course in the example above i get Mismathed tree node error : down
> expected <UP>.
>
> I would be very very grateful for the answer as I'm sitting few hours
> already trying to fix it somehow.
>
> Once again thanx,
> Jakub Lambrych
> --
> OpenPGP Public Key: http://lambrych.info/Jakub_Lambrych.asc
>



-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list