[antlr-interest] Working with tree walkers in V3

Mark Bednarczyk voytechs at yahoo.com
Wed Feb 21 03:31:25 PST 2007


I'm having difficulty accessing values from my tree walker. The tree is
built and has hierachy of nodes in it. The walker properly matches on the
various rules and that's where my problems start. I can't seem to get the
grasp on all the special $ prefixed attributes. Everything throws NULL ptrs
exceptions when I access them.

Here is my tree that I feed to my tree walker:
(HEADER_DEF (MODIFIERS public) (ID Ethernet) (SLIST (VAR_DEF (MODIFIERS
public field) (TYPE mac) (ID destination))))

I've gotten it down to a fairely manual way of accessing atleast a return
value.

Is there a better way to access the value of the following rule?

modifier returns [String ret]
@finally {
$ret = $m.text;
}
    :   m='public'
    |   m='protected'
    |   m='private'
    |   m='static'
    |   m='final'
    |   m='synchronized'
    |   m='transient'
    |   m='volatile'
    |	  m='field'
    ;

It would be nice if there was a way to tell the rule to return whatever
value it matched such as:

modifier returns [String ret]
@finally {
$ret = $text; // Will throw NULL Ptr exception
}
    :   'public'
    |   'protected'
    |   'private'
    |   'static'
    |   'final'
    |   'synchronized'
    |   'transient'
    |   'volatile'
    |	  'field'
    ;

Again, this is all in "tree grammar".

Cheers,
mark...




More information about the antlr-interest mailing list