[antlr-interest] ANTLR tree construction problem
Terence Parr
parrt at cs.usfca.edu
Tue Mar 25 12:56:45 PDT 2008
Hi Ken...i just verified that this works in latest.
// Ken.g:3:8: modifier= ( 'protected' | 'public'
| 'private' | 'fragment' )
{
modifier=(Token)input.LT(1); //
#######################
if ( (input.LA(1)>=5 && input.LA(1)<=8) ) {
input.consume();
adaptor.addChild(root_0,
(Object)adaptor.create(modifier
Ter
On Mar 10, 2008, at 7:46 PM, Kenneth Domino wrote:
> 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