[antlr-interest] [C Target] Multiple return parameters rule generation error.

Jim Idle jimi at temporal-wave.com
Mon Aug 4 10:35:29 PDT 2008


On Mon, 2008-08-04 at 13:05 -0400, Garry Iglesias wrote:
> Hi,
>  
> Sorry, I've already been reporting this... But it seems it's still
> there. So I 'remind it' because it's important as a standard feature
> of ANTLR (multiple returning parameters) that doesn't work for the C
> target.
> I got a rule with return parameters :
>  
> myProblemRule returns [int i,int bIsRef]
>    :  [...]
>    ; 
>  
> myNoProblemRule returns [int i]
>    :  [...]
>    ; 
>  
> 
>  
> When I use it :
> myOtherRule
>     :  [...]  good=myNoProblemRule  bad=myProblemRule 
>        {
>              int ivar=$good.i;        -> This works...
>              ivar=$bad.i;              -> This generates code that
> doesn't compile...
>              if ($bad.bIsRef)  {      -> This generates code that
> doesn't compile...
>              }
>        }
>     ;
>  
> Note "myBool" is just a typedef from an int...
>  
> So the fact is that ANTLR converts the 'if' into something like :
> 
> if (( result != NULL ? result.bIsRef : NULL )) {  [...]  }
>  

This is fixed for the next beta. There was one instance of attribute
referencing where the template was still using the unnecessary guard
code. Note the following about return values:

1) Typedefs and so on are unknown to the code generator, if there places
where such variables must be initialized, then if they are unrecognized,
they are assigned a NULL. The fix for attribute references is to not
assign any default values to rule references, which is now fixed int he
template;
2) The return type of a rule that is not building trees and has only one
return element is the type of the return element;
3) If the rule returns more than one value, or is building trees then
the return type is a typedef'ed struct containing all attributes
including the tree that was generated by that rule and so on. Hence the
return is a struct and you cannot/should not try to embed structs in the
return types as they are not needed. If you need to return a struct, it
should be a pointer to the struct. If you are trying to return a struct
as a composite of return codes, then you don't need to do that as ANTLR
already does it, so just specify the rule as returning each individual
element.
4) You are responsible for free()ing any pointers to memory that you
allocate within the rule of course;

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080804/468d0c26/attachment.html 


More information about the antlr-interest mailing list