[antlr-interest] How to use one tree grammar to generate C# and Java?

Terence Parr parrt at cs.usfca.edu
Thu Sep 4 10:56:04 PDT 2008


On Sep 4, 2008, at 4:19 AM, Fridi wrote:

> Hi all,
>
> i have a build a tree parser with ANTLR and the C# sources are
> generated. At runtime, the generated sources do the evaluation.
> Of course by setting the language option I can generate Java as well.
> But in my rules, I have to change all data types, imports / usings and
> included sources.
> I could copy the tree grammar and make one for C#, and the other for
> Java, but that means maintaining 2 grammars.
> Is there a way - maybe using stringtemplate - to use 1 grammar that
> either emits C# or Java code?

Sure. ANTLR itself Does that...you have a StringTemplateGroup pointer  
that refers to one of two groups.  ANTLR's -> foo(...) notation loads  
template foo from the appropriate group. no fuss no muss.
Ter


More information about the antlr-interest mailing list