[antlr-interest] parsing rules

Jim Idle jimi at temporal-wave.com
Fri May 28 10:07:38 PDT 2010


I think  you need to work through the examples in the book as everything that you need to do this is in my answer to you below. Other than writing it for you, there is not much more to show you!

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of George Soom
> Sent: Friday, May 28, 2010 1:12 AM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] parsing rules
> 
> Thank You, again
> 
> Sadly this leads me to some serious misunderstandings. My main job is
> computing using
> Maxima and Fortran, so this is all a bit too new for me. A
> example-by-example is all I need.
> Let me present You a complete simple grammar and template:
> ----
> grammar TestGrammar;
> 
> options {
>   output = template;
> }
> 
> prog
> 	: s+=statement+
       ->prog(stats={$s})
> 	;
> 
> statement
> 	: assign { $st = $assign.st; }
> 	| print { $st = $print.st; }
> 	;
> 
> assign
> 	:ID ':=' INT ';' -> assign(x={$ID.text},y={$INT.text})
> 	;
> 
> print
> 	: 'toScreen' '(' STRING ')' ';' -> print(string={$STRING.text})
> 	;
> 
> ID: 'a'..'z' + ;
> INT:'0'..'9' + ;
> STRING: (ID|INT)+ ;
> -----
> 
> and templates:
> 
> ----
> group TestTemplate;
> 
> assign(id,int) ::= "<id> = <int>"
> print(string) ::= "print <string>"
prog(stats) ::= <<
<stats: {<it>}; separator="\n">
>>

> ----
> 
> What do I need to add to get translated result from parser.prog() ?

Get an instance of prog, do a toString on it. It's all in the book and also check the downloadable examples.

Jim





More information about the antlr-interest mailing list