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

Terence Parr parrt at cs.usfca.edu
Wed Aug 13 17:13:00 PDT 2008


Try ^(DECL["0"] $dims*)

Ter
On Aug 13, 2008, at 5:03 PM, Szczepan Hołyszewski wrote:

> 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