[antlr-interest] Passing list of tokens to StringTemplate

Gavilan gavilancomun at gmail.com
Mon Jul 14 06:34:31 PDT 2008


Hi,

I am learning antlr with version 3.1b1.

I would like to pass a list of tokens to a template.

Here is a simplified example:

toks.stg
========

group toks;

toks(v) ::= <<

\<toks><v; separator=", ">\</toks>
 >>


toks.g
======

grammar toks;

options { output=template; }

INT: ('+'|'-')? ('0'..'9')+;
WS: (' '|'\t'|'\r'|'\n')+ { skip(); };

toks: t+=INT t+=INT t+=INT -> toks(v={$t});


On input:
-1 0 23

I get:
<toks>[@0,0:1='-1',<4>,1:0], [@1,3:3='0',<4>,1:3], 
[@2,5:6='23',<4>,1:5]</toks>



If I add a new rule:

int_rule: INT -> template(v={$INT.text}) "<v>";

and modify the toks rule to use it:

toks: t+=int_rule t+=int_rule t+=int_rule -> toks(v={$t});

I get the expected output:
<toks>-1, 0, 23</toks>


Is this the best way to do it?

Best Wishes,
James

--
"Be excellent to each other", Bill & Ted


More information about the antlr-interest mailing list