Hi Stas. I recommend that you get a hold of a copy of one or both of Terence Parr's ANTLR books, which are very helpful in working with ANTLR! Much of how to use the syntax and other details of ANTLR are not very accessible through the freely available documentation, so it very quickly pays off to buy the books (or borrow them from a library).
The standard way to do, what you want, is to separate the processing of the language into (1) a parsing step that builds the AST, and (2) a processing step that walks the tree and generates the files you need. The advantage of splitting the processing into two parts are that you get some separation of concerns (syntax and semantics) that can be very helpful when dealing with code generation (especially to multiple targets as you need to do). The first step is implemented using a grammar like the one you have included. ANTLR has a rewriting syntax and some additional sugar for actually building the tree. You can see an example of the technique here: http://www.antlr.org/wiki/display/ANTLR3/Expression+evaluator The second step can be implemented in a number of ways. The most effective way in ANTLR is to use a tree grammar, which is a grammar that operates on the AST your parser produces. The tree grammar will contain semantic actions (=code) that generates code using any method you choose. You can probably wrap the code generation actions of your tree grammar in an interface to easily build more code generation targets, reusing the tree grammar. Rune -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Stas Ostapenko Sent: 24. marts 2010 17:32 To: [email protected] Subject: [antlr-interest] Newbie questions Hi ! I'm ANTLR newbie and need some suggestions about the way ANTLR works. I have very simple language construct. For example struct { Integer i; String str1; Integer x; Integer y; String z; } I want to generate a couple of files from this structure. First of all - appropriate POJO with getters and setters. At this moment I have working grammar and generated lexer/parser using antlr3-maven-plugin. It looks like this (the most interesting part): /* entry point */ main :(WS* myStruct)*; myStruct returns [String structName] : 'struct ' IDENT '{' variable* '}' {$structName = $IDENT.text;} ; type returns [String typeName] : 'Integer' {$typeName = "Integer ";} |'String' {$typeName = "String ";} ; variable returns [String variableName] : type WS* IDENT WS*';' {$variableName = $IDENT.text;} ; Using the following code I can access the name of the structure. But how to get variables with their types ?? CommonTokenStream tokens = new CommonTokenStream(lexer); SampleParser parser = new SampleParser(tokens); SampleParser.myStruct_return msr = parser.myStruct(); System.out.println("struct name from code : "+msr.structName); I see that StringTemplate is used as template engine. Could I use Velocity together with ANTLR ? I have some ready-made templates in Velocity, so it will be cool to just reuse them. Since I need to use the same source for generation of different sorts of output files I need some kind of reuse of AST or something else. Any ideas how it's could be achieved ? I'm lost a little, please help me to get out. Thanks ! Best regards, Stas. List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
