[antlr-interest] parsing rules
George Soom
george.soom at siria.cc
Fri May 28 01:12:01 PDT 2010
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
: statement+
;
statement
: assign
| print
;
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>"
----
What do I need to add to get translated result from parser.prog() ?
George
Jim Idle <jimi at temporal-wave.com> wrote on 27 May 2010, 05:00 PM:
Subject: Re: [antlr-interest] parsing rules
>
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of George Soom
>> Sent: Thursday, May 27, 2010 3:52 AM
>> To: antlr-interest at antlr.org
>> Subject: Re: [antlr-interest] parsing rules
>>
>> Thank You,
>>
>> now I understand my mistake. However, can You give me a bit bigger
>> example?
>
>OK. I use this (all the time ;-):
>
>statements
> : s+=statement*
>
> // The list of statements just needs to go on up the chain, but we
> // still use a template in our template group in case we ever
> // wish to adorn statements with something or the target changes
> //
> -> statements(stats={$s})
> ;
>
>Then a template of:
>
>statements(stats) ::= <<
><stats: {<it>}; separator="\n">
>>>
>
>(Sometimes you will need to iterate, sometimes not, depending on what you
>did lower down the chain.
>
>Other constructs are:
>
> | COMMENT
> {
> // Get rid of leading spaces and the ' character, generate
>the comment line
> //
> $st =
>%lineComment(comment={$COMMENT.text.trim().substring(1)});
> }
>
>To use a template within an action.
>
>And:
>
> {
> // Ask the code generator to deal with the assignment
> //
> $st = codeGen.assign($v.st, $v.symbol, $expression.st,
>$expression.symbol, $expression.type);
> }
>
>To call a method that returns a template (or List).
>
>And:
>
> : ^(CALL
>
> id=IDENTIFIER
> {
> $st = %({$id.text})();
>
> f = (Function)localSymbols.lookup($id.text);
> %{$st}.instr=f.getInstruction();
>
> argNum = 0;
> }
>
>...
>
>
>To instantiate a template, the name of which is held in some String.
>
>
>Jim
>
>
>
>
>
>
>
>List: http://www.antlr.org/mailman/listinfo/antlr-interest
>Unsubscribe:
>http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
More information about the antlr-interest
mailing list