Boost logo

Boost :

Subject: Re: [boost] boost::spirit -- semantic values
From: Joel de Guzman (joel_at_[hidden])
Date: 2009-07-02 23:04:23


Anand wrote:
> I couldn't find a way to associate a semantic value with a
> non-terminal. Bison/yacc allows this.
>
> For example, here is a rule in Bison that says an expression can be
> the sum of two subexpressions:
>
> expr: expr '+' expr { $$ = $1 + $3; }
> ;
>
> The action says how to produce the semantic value of the sum
> expression from the values of the two subexpressions.
>
> Having semantic values for non-terminals is useful to build an expression tree.
>
> For, e.g, I can create a new node in the tree by passing in the
> semantic values of child nodes in the action.
> expr: expr '+' expr { $$ = makeNode($1, $3, "+"); }
> ;
>
> In this case the semantic value for each expression is of type Node*.
>
> Spirit provides parse trees and ASTs and also semantic actions that
> take the first last iterator pair. But is there some way I can
> associate a semantic value with each non-terminal and pass in these
> semantic values to the action for the rule.

"classic" Spirit is basically a transduction type parser with
attributes creeping in later in the form of closures, and rule
attributes, etc.

You should look at Spirit-2 instead. Spirit2 is a full
attribute-grammar PEG parser.

I am cross-posting this to the spirit list. Please post further
questions there:

http://www.boost.org/community/groups.html#spirit

Regards,

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk