boost::spirit -- semantic values

View: New views
2 Messages — Rating Filter:   Alert me  

boost::spirit -- semantic values

by Anand Lakshminath :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Thanks,
Anand
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Re: boost::spirit -- semantic values

by Joel de Guzman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost