[antlr-interest] Variable visibility problem with ANTLR 3code generation

Denis Washington dwashington at gmx.net
Tue Aug 22 01:39:14 PDT 2006


Jim Idle wrote:
> -----Original Message-----
> From: Denis Washington [mailto:dwashington at gmx.net] 
> Sent: Monday, August 21, 2006 3:45 AM
> To: Jim Idle; antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Variable visibility problem with ANTLR 3code generation
>
> Jim Idle wrote:
>   
>> -----Original Message-----
>>   
>>     
> For the following rule:
>
> declarationStatement returns [DeclarationStatement ds]
>     :   var=varOrConstDeclaration
>         {
>             $ds = new DeclarationStatement($var);
>             $ds.setSource($var);
>         }
>     ;
>
> I get from ANTLR (3.0b3):
>
> schlicht.g:240:9: missing attribute access on rule scope: var
>
> Is this a bug? Or do I still understand the $ prefix wrong?
>
>
> If you are not producing a tree, then you cannot assign var to the result of a rule, unless the rule returns[] something. The error is telling you that the rule does not return anything. Or perhaps your rule returns multiple elements, in which case you would need $var.element. If you want $var to be an object then the rule needs to return one and construct it within its action code, rather than return multiple elements. Though it is possible that you mean to access the internally constructed object, it is internal and could change, so you should build your own and return that.
>
> In general, you should consider producing a tree for the first pass, then use the tree to do more complicated things.
>
> Jim
>
>   
Hi Jim,

The rule varOrConstDeclaration is returning something, exactly one 
element, through it's action code. so it should work.

Denis


More information about the antlr-interest mailing list