[antlr-interest] Generating initializers and headers

Arnulf Heller aheller at gmx.at
Tue Jan 29 11:06:32 PST 2008


At 14:02 29.01.2008, you wrote:
>I've been puzzling over this for a few days, and haven't thought of 
>anything useful, so I figured I'd ask the group.
>
>I'm looking to generate C code from antlr/stringtemplate.  I'd like 
>to generate methods in a .c file, method signatures in a .h file, 
>and probably some sort of initialization method in the .c file.  My 
>problem is that any part of the AST could generate code in any of 
>those 3 places.  What is the normal way to handle this?

hi,

look at the ANTLR3 sources (src\org\antlr\codegen\CodeGenerator.java, 
src\org\antlr\codegen\templates\cpp\CPP.stg)

I think this is what you are looking for.

They keep two string templates in a group (that share type 
definitions etc.) and throw the parsing output onto them:

                 headerFileST.setAttribute("actions", actions);
                 outputFileST.setAttribute("actions", actions);
                 headerFileST.setAttribute("buildTemplate", new 
Boolean(grammar.buildTemplate()));
                 outputFileST.setAttribute("buildTemplate", new 
Boolean(grammar.buildTemplate()));
                 headerFileST.setAttribute("buildAST", new 
Boolean(grammar.buildAST()));
                 outputFileST.setAttribute("buildAST", new 
Boolean(grammar.buildAST()));
                 outputFileST.setAttribute("rewriteMode", 
Boolean.valueOf(grammar.rewriteMode()));
                 headerFileST.setAttribute("rewriteMode", 
Boolean.valueOf(grammar.rewriteMode()));
                 outputFileST.setAttribute("backtracking", 
Boolean.valueOf(canBacktrack));
                 headerFileST.setAttribute("backtracking", 
Boolean.valueOf(canBacktrack));

cheers,
arnulf 



More information about the antlr-interest mailing list