[antlr-interest] action syntax proposal for v3

Sebastian Mies sebastian.mies at gmx.net
Sun Nov 6 02:58:49 PST 2005


Hi Terence,

this would be great!
There will be something like @rule.enter { } and @rule.exit { } I hope
;-)
I think adding some conditional generation would be nice too.
Something like:

@define(niceexceptions)
@if(niceexceptions) {
    @rule.exception { }
}

you could also add parameterization to grammars, like

grammar whatever(niceexceptions, ...)

this would avoid having multiple copies of a grammar, but
reduce refactoring capabilities.
It would also give the grammars a slight aspect-oriented touch.

Sebastian.

TP> Howdy.

TP> While Ric Klaren was here, he and I decided a change in syntax would
TP> be useful for actions so that you can set code into more places in
TP> the output.  In fact, we'll be going further.  I think John Mitchell
TP> suggested that we expose the code gen templates to the user so they
TP> can alter things as they want without overrides and w/o creating a
TP> new output template file for ANTLR.  For example, if you want to  
TP> change how ANTLR generates code for exception handling in rules, you
TP> might want to change the template once rather than cut/paste it  
TP> yourself manually with exception rule clauses.  Perhaps:

TP> @rule.exception {
TP>          reportError(re);
TP>          recover(input,re);
TP> }

TP> where template rule has a region called exception; in  
TP> StringTemplateGroup notation (new for 2.3) you would do something
TP> similar so syntax is consistent.  This may let us avoid an option
TP> that turns off exception handling...you can just make rule.exception
TP> an empty action. :)

TP> This provides flexibility.  In v2 we have named headers for C++ so
TP> you can stick where you need it for C++.  I'm extending that to just
TP> be @name{...}.  Currently in v3 rule init actions are:

TP> rulename
TP> init {
TP>   blort
TP> }
TP>         :       ...
TP>         ;

TP> We'd change this syntax also so @ implies action:

TP> rulename
TP> @init {
TP>   blort
TP> }
TP>         :       ...
TP>         ;

TP> I hate to use a valuable symbol like @ for this but it's consistent
TP> with StringTemplateGroup's syntax, a good thing.

TP> A grammar file might look like this:

TP> grammar t;

TP> @parser.header {
TP> package org.antlr.foo;
TP> }

TP> @lexer.header {
TP> package org.antlr.foo;
TP> }

TP> @parser.members {
TP> int i; // parser needs this
TP> }

TP> @lexer.members {
TP> int j; // lexer needs this, dude
TP> }

TP> a : ID {i++;} ;

TP> ID : 'a'..'z'+ {j++;} ;

TP> It's very explicit but a bit more typing.  What do you think? I like
TP> it.  Tweaks?  Counter proposals?

TP> Ter



-- 
Best regards,
Sebastian                            mailto:sebastian.mies at gmx.net



More information about the antlr-interest mailing list