[antlr-interest] 2nd and Subsequent References in Action to Non-Terminals Generate Incorrect Code

Kay Roepke kroepke at classdump.org
Wed Nov 22 10:42:37 PST 2006


On 22. Nov 2006, at 18:32 , Randall R Schulz wrote:

> It appears that within any given parser rule only the first
> alternative's action can successfully refer to (non-assigned)
> non-terminals.
>
> E.g., this parser rule:
>
> functionTerm
> returns [ Term ft ]
>   : plainTerm     { $ft = $plainTerm.pt; }
>   | definedTerm     { $ft = $definedTerm.dt; }
>   ;
>
> generates correct code for the reference in the action to  
> $plainTerm.pt
> but simply emits $definedTerm.dt for the reference in the second
> alternative.

ouch. I added a minimal test case and the bug report: <http:// 
www.antlr.org:8888/browse/ANTLR-46>

but the problem seems to be worse:

grammar MissingAttributeRef;

rule1 returns [ Object obj ]
	:	rule2 { $obj = $rule2.obj2; }
	|	rule3 { $obj = $rule3.obj3; }
	|	rule4 { $obj = $rule4.obj4; }
	;
	
rule2 returns [ Object obj2 ]
	:	foo='foo' { $obj2 = $foo.text; }
	;
	
rule3 returns [ Object obj3 ]
	:	bar='bar' { $obj3 = $bar.text; }
	;
	
rule4 returns [ Object obj4 ]
	:	baz='baz' { $obj4 = $baz.text; }
	;
	
gives:

ANTLR Parser Generator  Version 3.0b5 (November 15, 2006)  1989-2006
error(116): MissingAttributeRef.g:0:0: unknown attribute for rule  
rule2: obj2
error(114): MissingAttributeRef.g:10:14: attribute is not a token,  
parameter, or return value: obj2
error(114): MissingAttributeRef.g:14:14: attribute is not a token,  
parameter, or return value: obj3
error(114): MissingAttributeRef.g:18:14: attribute is not a token,  
parameter, or return value: obj4

That's <http://www.antlr.org:8888/browse/ANTLR-47>. Sigh.

Thanks for the report!

-k

-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list