[antlr-interest] Language-Neutral Actions

Alexey Demakov demakov at ispras.ru
Sun Apr 17 23:41:43 PDT 2005


From: "Scott Stanchfield" <scott at javadude.com>

> I'd really like to see language-neutral actions in ANTLR 3.
> 
> The basic idea is that all action code is simply calls to behavior methods
> in some other object.
>
> Depending on what target language is specified, different "glue" code is
> formed to contact that other object.
> 
> This would allow a grammar to be targeted to pretty much any language.
> 
> Some old parser generators I used did things like
> 
>   foo: fee fie fo @doSomething(fee,fie,fo);
> 
> which would allow you to target it to multiple languages. The parser
> generator would change @doSomething(...) into whatever was appropriate for
> the target language.
> 
> I don't know that we'd want to use the same syntax (I like the {...} for
> actions), but we could have a grammar option that specifies language-netrual
> actions and all {...} code is assumed neutral.

The main question is: how to bind action code to some place in grammar?
There are too many places to write @doSomething(fee,fie,fo) to provide
all possible callbacks - who knows what will be enough for all possible target languages?
Other way - to have aspect-like notation to specify certain place in grammar.
Not very easy to maintain...

What is the main purposes of action code in grammar? I think it is
1. Tree building.
2. Semantic predicates to handle difficult cases.
(If I not miss something important) All other code should be separated from grammar
for better decomposition and easer maintenance.
So, for the first case ANTLR already provides language-independed notation.
And I expect, that in ANTLR 3 will be improved interface to interact with tree factories
so I can use heterohenous trees :) We've discussed such interface here some months ago.
The second case, semantic predicates, in my opinion is (and should be) rare case,
the last way to use. And for them is possible to specify language-independed action
like @doSomething(...) or even as
options { language = java   } { /* java code   */ }
options { language = csharp } { /* csharp code */ }
The last way can be used and for all other places of action code,
but it can make grammar file to large and unreadable.

Regards,
Alexey

-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com




More information about the antlr-interest mailing list