[Proto] Detecting series of operator in grammar
How can I write a transform that matches expression like a0+a1+a2+ ... +an (with N +) and turn into a custom node like sum(a0,a1,...,an) Thanks in advance -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
Joel Falcou wrote:
How can I write a transform that matches expression like
a0+a1+a2+ ... +an (with N +)
You write a grammar.
and turn into a custom node like
sum(a0,a1,...,an)
You use proto::functional::flatten to turn the binary + expression tree
into a flat Fusion sequence, and then you use
proto::functional::unpack_expr to turn the Fusion sequence into an
expression node with any tag you want. See below:
#include <iostream>
#include
Eric Niebler a écrit :
You use proto::functional::flatten to turn the binary + expression tree into a flat Fusion sequence, and then you use proto::functional::unpack_expr to turn the Fusion sequence into an expression node with any tag you want. See below:
I was roaming around flatten so guess I only missed the as_vector trick.
I guess this means that this "n-ary" plus expression node can be then
latter be matched by another grammar
defined like (simple version ofc) :
struct some_grammar
: nary_expr
Joel Falcou wrote:
Eric Niebler a écrit :
You use proto::functional::flatten to turn the binary + expression tree into a flat Fusion sequence, and then you use proto::functional::unpack_expr to turn the Fusion sequence into an expression node with any tag you want. See below:
I was roaming around flatten so guess I only missed the as_vector trick. I guess this means that this "n-ary" plus expression node can be then latter be matched by another grammar defined like (simple version ofc) :
struct some_grammar : nary_expr
> {};
Yep. I forgot to mention that you need to be aware of BOOST_PROTO_MAX_ARITY when unpacking sequences into expression nodes. -- Eric Niebler BoostPro Computing http://www.boostpro.com
Eric Niebler a écrit :
I forgot to mention that you need to be aware of BOOST_PROTO_MAX_ARITY when unpacking sequences into expression nodes. Is this symbol safely re-definable ? What's its base value ?
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
Joel Falcou wrote:
Eric Niebler a écrit :
I forgot to mention that you need to be aware of BOOST_PROTO_MAX_ARITY when unpacking sequences into expression nodes. Is this symbol safely re-definable ? What's its base value ?
Yes. Check the docs. -- Eric Niebler BoostPro Computing http://www.boostpro.com
Eric Niebler wrote: <snip>
The only reason I need as_vector above is because currently unpack_expr requires a RandomAccessSequence, and a flattened expression is a ForwardSequence. Fixing unpack_expr to handle ForwardSequences is on my TODO list.
OK, proto::unpack_expr now works with Fusion Forward Sequences, so the ugly as_vector hack in my earlier mail is no longer necessary. I'll merge to release in a few days. -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (2)
-
Eric Niebler
-
Joel Falcou