[antlr-interest] Custom return attributes ignored when matching a list

Bob Adolf rdadolf at gmail.com
Mon May 24 06:29:45 PDT 2010


Hello all,

I seem to be having trouble finding the kosher way for returning  
custom attributes to a rule that matches a list. Easiest to explain  
with an example (in Python, but the behavior exists in java as well):

grammar demo;
options {
   language=Python;
   output=AST;
   ASTLabelType = CommonTree;
}

NUM: ('0'..'9')+ ;
CHAR: ('a'..'z'|'A'..'Z')+ ;
WS: (' '|'\t'|'\r'|'\n')+ { $channel=HIDDEN; } ;

broken_program: (plist+=pair)+ {
   for p in $plist:
     print p.custom
} ;

working_program: pair (working_program)? {
   print $pair.custom
} ;

pair returns [custom]:
   '(' CHAR ',' NUM ')' {
   $custom = [ $CHAR.text, $NUM.text ];
} ;

The driver program is trivial, and the input files look like pairs of  
letters and numbers: "(abc,123) (def,456)" etc. Invoking the  
working_program rule from a driver program works nicely. I get a list  
of all the pairs. Invoking the broken_program rule from a driver  
causes an AttributeError when the top-level rule tries to print  
p.custom. At first, I assumed that I had misunderstood the way  
returned attributes work. After looking at the generated code,  
however, I'm leaning towards a different conclusion.

In the working_program rule, the attribute reference $pair.custom is  
accessing the custom element of pair_return class, and it works. In  
the broken_program rule, the attribute reference p.custom is accessing  
a CommonTree object. The error message "AttributeError: 'CommonTree'  
object has no attribute 'custom'" supports this, and the generated  
code bears it out. The "+=" operator generates the code  
"list_plist.append(plist.tree)" in Python and  
"list_plist.add(plist.getTree());" in Java.

The behavior of single-matching and list-matching seems needlessly  
inconsistent, which makes me think that I'm just missing something  
about the way I'm supposed to write this. Unfortunately, my searches  
on both markmail and in the downloaded samples have turned up no  
grammars that use both return-attributes and list matching in tandem.

Someone out there know the right way to write this idiom?

Thanks in advance!

   -Bob




More information about the antlr-interest mailing list