[antlr-interest] Possible code generation problem with arguments (ANTLR 3.1)

Johannes Luber jaluber at gmx.de
Sat Aug 16 05:54:25 PDT 2008


Ian Kaplan schrieb:
> 
>   I pass an argument down through the grammar. This is an object that 
> contains a simple symbol table and other book keeping information.  As 
> the parse proceeds objects are added to this container object.  The 
> grammar is shown below.  As you can see, I set backtrack to true since 
> this is a section of the grammar that can't be parsed as I've written 
> the grammar without back tracking.
> 
>   ANTLR apparently generates syntactic predicate delegates or 
> something.  These are shown below as well.  Note that these methods have 
> no argument, unlike the syntax that they were generated from.  They then 
> attempt to call the method generated for the syntax production with an 
> argument that doesn't exist.
> 
>   Is this a bug in ANTLR or a feature?  It should be possible to rewrite 
> the code so that it doesn't use arguments passed downward, but it will 
> be some work to get this done.  So I would be greatful for any insight 
> before I go down this path.  Thanks for your help,
> 
>   Ian

The problem is that ANTLR hoists some predicates into other rules, which 
don't have access to the parameter. Try using scopes or a member 
variable instead.

patternLink[ PatternContainer patternDefintion ]
scope {
PatternContainer patternDef;
}
options{
     backtrack=true;  // Allows parser backtracking
     memoize=true;
}
@init{
patternLink::patternDef = patternDefinition;
}
     : simpleLink
     | rangeLinkSet
     | degreeLinkSet
     ;

The order of the blocks may be different than above, so if ANTLR 
complains reorder them. To access patternDef by any rule called by 
patterLink, use patternLink::patternDef.

Johannes
>  
> 
> patternLink[ PatternContainer patternDef ]
> options{
>     backtrack=true;  // Allows parser backtracking
>     memoize=true;
> }
>     : simpleLink[ patternDef ]
>     | rangeLinkSet[ patternDef ]
>     | degreeLinkSet[ patternDef ]
>     ;
> 
> Generated Java code:
> 
>     // $ANTLR start synpred1_GraphQuery
>     public final void synpred1_GraphQuery_fragment() throws 
> RecognitionException {  
>         // GraphQuery.g:685:7: ( simpleLink[ patternDef ] )
>         // GraphQuery.g:685:7: simpleLink[ patternDef ]
>         {
>         pushFollow(FOLLOW_simpleLink_in_synpred1_GraphQuery1646);
>         simpleLink(patternDef);
> 
>         state._fsp--;
>         if (state.failed) return ;
> 
>         }
>     }
>     // $ANTLR end synpred1_GraphQuery
> 
>     // $ANTLR start synpred2_GraphQuery
>     public final void synpred2_GraphQuery_fragment() throws 
> RecognitionException {  
>         // GraphQuery.g:686:7: ( rangeLinkSet[ patternDef ] )
>         // GraphQuery.g:686:7: rangeLinkSet[ patternDef ]
>         {
>         pushFollow(FOLLOW_rangeLinkSet_in_synpred2_GraphQuery1655);
>         rangeLinkSet(patternDef);
> 
>         state._fsp--;
>         if (state.failed) return ;
> 
>         }
>     }
> 



More information about the antlr-interest mailing list