help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: semantic actions


From: Bob Rossi
Subject: Re: semantic actions
Date: Thu, 1 Mar 2007 15:25:19 -0500
User-agent: Mutt/1.5.12-2006-07-14

On Tue, Feb 27, 2007 at 07:20:31PM +0100, Hans Aberg wrote:
> On 27 Feb 2007, at 16:51, Bob Rossi wrote:
> 
> >I'm using bison along with it's semantic actions. In particular, I'm
> >using the $$, $1, $2 constructs in order to help me build a parse  
> >tree.
> >
> >In the past, I've place the top level AST type as a global object, and
> >the top level rule would assign it's $$ to that global variable.
> >
> >However, I'm wondering if it's possible, after the parse is done,  
> >to ask
> >bison to give you back the top level's rule $$ variable. That way, I
> >don't have to keep around an extra global variable.
> >
> >Is this question clear enough to be answerable? :)
> 
> Sure. In the past it was not possible. Perhaps if you try the pure  
> parser. I tweaked my own skeleton file so that class parser contains:
> 
> #if !YYLSP_NEEDED
>   int parse(parse_type& p, semantic_type& s);
>   int parse(parse_type& p) {
>               semantic_type s;
>               return parse(p, s);
>   }
> #else
>   int parse(parse_type& p, semantic_type& s = semantic_type(),
>             location_type& l = location_type());
> #endif
> 
> This way, one can get semantic and location values back as arguments.
> 
> In addition, I added some operators:
>   std::istream& operator>>(parse_type& d);
>   friend parser& operator>>(std::istream&, parser&);
>   friend std::istream& operator>>(std::istream&, parse_type&);
> 
> This way, invocation can take place as:
>   std::ifstream ifs(...);
>   semantic_value sv;
>   parser p(...);
>   ...
>   ifs >> p >> sv;  // Read and parse smenatic value
> which I think is intuitive.

Wow, that's a neat trick. I'll keep that in mind!

Bob Rossi




reply via email to

[Prev in Thread] Current Thread [Next in Thread]