[antlr-interest] Can "returns" return an aggregate?

Bob temporaryemail at comcast.net
Sun May 30 09:59:31 PDT 2010


My target is "C" (wish it was "C++") and because I'm accustomed to byson's
stack items being a user-definable union, e.g.:



  %union {                                                   <-------
declares a union of all rule return types
    void*	node;
    struct ModuleHeader moduleHeader;
  }

  %type	<moduleHeader>	module_ansi_header                 <------ declares
return type of the specific rule

  module_ansi_header
	: TokModule { struct ModuleHeader _mh; $$ = _mh; }   <------ rule
returns a ModuleHeader struct on stack 



it was natural for me to think I could do something similar in Antlar, like
this:

  @members
  {
   struct ModuleHeader { void* a; void* b };
  }


  module_ansi_header returns [struct ModuleHeader rslt]
  scope {
   struct ModuleHeader mh;
  }
    : TokModule { $rslt = mh; }



Which does not work. So I altered the rule:


  module_ansi_header returns [void* a,void* b] 
  scope {
   struct ModuleHeader mh;
  }
    : TokModule { $a = mh.a; $b = mh.b; }



My problem is `ModuleHeader` is undefined in "xxParser.h" because the
inclusion order is incorrect:

  xxParser.c:

   #include "xxParser.h"                 <-------- typedef for rule return
type refers to not-yet-defined `ModuleHeader`
       .....
   struct ModuleHeader { void* a; void* b };  <--- placement of @members
block


Being inexperienced with Antlr, I'm thinking I've just missed something
simple. 

Is there a way to get the "@members" block emitted prior to the inclusion of
the "xxParser.h" file?

Thanks,
Bob


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: Saturday, May 29, 2010 3:19 PM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Can "returns" return an aggregate?

You might have more success if you said what target you were using :-) Also
remember antlr.markmail.com.

The generated routines already return a struct, so you cannot put a struct
in that struct. You need to return a pointer to a struct.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Bob
> Sent: Friday, May 28, 2010 5:06 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Can "returns" return an aggregate?
> 
> Is there a way to return an aggregate ?
> 
> 
> 
> struct A { int m; double n; };
> 
> extern struct A foo(void);
> 
> 
> 
> myrule returns [struct A a]
> 
>     : X
> 
>      { $a = foo(); }
> 
>   ;
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address




List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list