[antlr-interest] ANTLR tree construction problem

Kenneth Domino kenneth.domino at domemtech.com
Mon Mar 10 19:46:23 PDT 2008


Hi,

I'm working with the example grammar ANTLRv3.g
(http://fisheye2.cenqua.com/browse/antlr-examples/java/ANTLR/ANTLRv3.g?r=4288)
and it looks like either the label handling in the generated parser is
not correct.  This seems to be exposed with modifiers (e.g.,
"fragment"), where the code uses the label "modifier" for a Token from
a set of string literals in the grammar.  The code in question is:

rule
scope {
 String name;
}
 : DOC_COMMENT?
  ( modifier=('protected'|'public'|'private'|'fragment') )?
  id {$rule::name = $id.text;}
  '!'?
  ( arg=ARG_ACTION )?
  ( 'returns' rt=ARG_ACTION  )?
  throwsSpec? optionsSpec? ruleScopeSpec? ruleAction*
  ':' altList ';'
  exceptionGroup?
     -> ^( RULE id {modifier!=null?adaptor.create(modifier):null} ^(ARG
$arg)? ^(RET $rt)?
        optionsSpec? ruleScopeSpec? ruleAction*
        altList
        exceptionGroup?
        EOR["EOR"]
      )
 ;

I think the rule is ok.  The problem seems to be in the code generated
for the expression:

"(modifier=('protected'|'public'|'private'|'fragment') )?"

There is no code emitted to set label "modifier", which is used later
on for tree construction.  As a result, modifier in the generated code
below is always null, and addChild() always receives null:

"adaptor.addChild(root_1, modifier!=null?adaptor.create(modifier):null);".

(That code was generated for
"{modifier!=null?adaptor.create(modifier):null}" in the grammar, and
seems fine.)

Other instances of labels (e.g., "g='grammar'...") seem to work fine.

You can use ANTLRWorks1.1.7 to verify the problem.  I'm not sure what
happens when addChild() tries to insert a null child, but ANTLRWorks
does not display anything for the modifier.  Nor does the example tree
parsing grammar recognize anything. I also checked this against the
2-27-2008 build, and it is still a problem.  Fortunately, I have a
work around that is trivial.

Ken Domino



More information about the antlr-interest mailing list