[antlr-interest] Mixing Grammars

mzukowski at yci.com mzukowski at yci.com
Fri Jun 14 11:59:11 PDT 2002


> Hi,
> 
> As part of a translation project I am working I wish to create an AST of
the
> target language, by transforming the source AST, using multiple tree
> grammars and user interaction.
> 
> Is it possible to mix grammars in a TreeParser, to allow node creation of
> the target language?
> 
> Has anyone got a good example of how they achieved this type of
translation
> in practice.

Yes, I did exactly this in my AREV to VB translator (see
http://www.codetransform.com/fun_with_antlr.html) What I did was to
create a tree grammar with both the AREV and VB tokens included.  I
achieved this by importing the AREV tokens from the AREV parser and
then introducing all the new VB tokens as "imaginary tokens" (you can
do this in a tokens{} section I believe.)

Since the AREV and VB tokens were disjoint sets it was no problem to
define all the rules for both grammars in the same tree grammar.

For each pass I would subclass that master tree grammar and override
only the rules I needed to.  In development I would run the result of
every pass through the master tree grammar to make sure I didn't
construct and bad trees.  The final pass was a code emitter which only
overrode the VB rules to print them out.

Monty
I provide antlr consulting at www.codetransform.com

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list