[antlr-interest] Conjuring tokens out of thin air in rewrite rules?

Szczepan Hołyszewski rulatir at wp.pl
Wed Aug 13 17:03:45 PDT 2008


For the life of me, I can't guess how to insert a token of a particular type 
AND value in a rewrite rule. My parser grammar has this rule for the list of 
array dimension sizes:

dimensionsList
	: assingExp (','! assignExp)*
	| (',' dims+=assignExp)* -> ( /* my problem here */ ($dims)*)
	;

Note the second rule allows omitting the first dimension, but I want this to 
be semantically equivalent to the first dimension specified as 0 (which will 
in turn mean dynamic size). I want to prepend the resulting token sequence 
with a token of type DEC (defined by lexer grammar) and text value "0". In 
other words, I want this input:

	, 2, 3, 4

to yield identical AST as this input:

	0, 2, 3, 4

Can this be done at all without manipulating the AST directly with Java in a 
semantic action? If not, how to write such a semantic action?

Regards,
Szczepan Holyszewski



More information about the antlr-interest mailing list