[antlr-interest] action syntax proposal for v3
Terence Parr
parrt at cs.usfca.edu
Sat Nov 5 17:14:54 PST 2005
Howdy.
While Ric Klaren was here, he and I decided a change in syntax would
be useful for actions so that you can set code into more places in
the output. In fact, we'll be going further. I think John Mitchell
suggested that we expose the code gen templates to the user so they
can alter things as they want without overrides and w/o creating a
new output template file for ANTLR. For example, if you want to
change how ANTLR generates code for exception handling in rules, you
might want to change the template once rather than cut/paste it
yourself manually with exception rule clauses. Perhaps:
@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. :)
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.
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?
Ter
More information about the antlr-interest
mailing list