[antlr-interest] Parser/Lexer rules final in generated code / manually implemented rules

Jim Idle jimi at temporal-wave.com
Wed Dec 3 07:55:12 PST 2008


On Wed, 2008-12-03 at 12:45 +0100, Oliver Zeigermann wrote:

> Folks!
> 
> Any idea why parser and lexer rules are final in generated code? If it
> is for performance I doubt it does any significant speed up.
> 
> Anyway, how do I manually implement a certain lexer or parser rule? I
> though of subclassing and overriding the specific rule. Which does not
> work because of the final modifier.
> 
> Any chance to achieve this? Especially, when thinking of first and
> follow computation for prediction code? Maybe one could specify this
> manually?
> 
> Any hints welcome.


Generally you should not have to do this, and Ter has a design for
specific error recovery alts that will make things in the parser a lot
easier. 

The way to gain access to first and follow sets is to call an empty rule
is your alt at the specific place you want the first set and you will
find that that the stacked follow set for the rule is the first set for
the that point in the rule. You can then implement code (better to call
an super method really) that can do things like consume until LA(1) is
in the followset (which is the first set for the point you invoke the
rule). There is a fairly obscure bug with followset caching in 3.1.1,
that is triggered by this though - you get too many things in the
followset, but this is fixed in the development snapshots.

So:

rule : CLASS emptyRule (member emptyRule)* '}' ;

emptyRule

@init
{
   // do your stuff here
}
  :
  ;


I can't think off hand of anything one would need to do by overriding a
rule to be honest. Do you have anything specific in mind? Also,
overriding a rule might result in incorrect predictions or at least,
calling a rule that wasn't really predicted by analysis would need to be
documented all to hell ;-)


Jim

> 
> Cheers
> 
> Oliver
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081203/4543f5c6/attachment.html 


More information about the antlr-interest mailing list