[antlr-interest] Newbie questions

Stas Ostapenko stas.ostapenko at gmail.com
Wed Mar 24 09:31:42 PDT 2010


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.


More information about the antlr-interest mailing list