[antlr-interest] action syntax proposal for v3

Bill Canfield canfield at computer.org
Sun Nov 6 09:58:50 PST 2005


 >
 > This provides flexibility.  In v2 we have named headers for C++ so
 > you can stick where you need it for C++.  I'm extending that to just
 > be @name{...}.  Currently in v3 rule init actions are:
 >
 > rulename
 > init {
 >   blort
 > }
 > 	: 	...
 > 	;
 >
 > We'd change this syntax also so @ implies action:
 >
 > rulename
 > @init {
 >   blort
 > }
 > 	:	...
 > 	;
 >
 > I hate to use a valuable symbol like @ for this but it's consistent
 > with StringTemplateGroup's syntax, a good thing.
 >
Terence wrote:
> @rule.exception {
>          reportError(re);
>          recover(input,re);
> }
> 
> where template rule has a region called exception; in  
> StringTemplateGroup notation (new for 2.3) you would do something  
> similar so syntax is consistent.  This may let us avoid an option  
> that turns off exception handling...you can just make rule.exception  
> an empty action. :)

That sounds OK, but will we have to add empty actions for every region? 
  I'm assuming there would be rule.exception, branch.exception, 
label.exception... anything else?  I prefer "defaultErrorHandler=false" :-).

> A grammar file might look like this:
> 
> grammar t;
> 
> @parser.header {
> package org.antlr.foo;
> }
> 
> @lexer.header {
> package org.antlr.foo;
> }
> 
> @parser.members {
> int i; // parser needs this
> }
> 
> @lexer.members {
> int j; // lexer needs this, dude
> }
> 
> a : ID {i++;} ;
> 
> ID : 'a'..'z'+ {j++;} ;
> 
> It's very explicit but a bit more typing.  What do you think?  I like  
> it.  Tweaks?  Counter proposals?

This part looks very good.  The extra typing is worth it.  I assume the 
@*.members regions are where the member is declared inside the class. 
Is there a region for the definition of member functions?  Right now I 
just put them in a {...} after the grammar options, and they get put in 
  the .cpp file instead of the header.

Bill


More information about the antlr-interest mailing list