[antlr-interest] parsing rules
Jim Idle
jimi at temporal-wave.com
Thu May 27 07:00:00 PDT 2010
> -----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
More information about the antlr-interest
mailing list