Boost logo

Boost Users :

Subject: Re: [Boost-users] [Xpressive] Nested patterns and sub-matches' semantic values
From: Eric Niebler (eniebler_at_[hidden])
Date: 2013-05-30 15:47:11


On 13-05-30 08:35 AM, Simon wrote:
> Hi,
> For one part of a large project, I need to parse math and turn the
> "mathematical objects" into a C++ abstraction of them. Is it possible
> that a nested pattern hold semantic values within itself and can be
> queried for that value somehow? As if a match were a function of its
> sub-patterns' matches, if that makes sense...
>
> Code might speak better than my english:
>
> class math_number {...};
> class math_addition {...};
> class math_arglist {...};
> class math_funcdef {...};
>
> sregex integer = (s1= +_d)
> [ return math_number( as<int>(s1) ) ];
>
> sregex addition = (s1= integer) >> '+' >> (s2= integer)
> [ return math_addition( as<math_number>(s1), as<math_number>(s2) ) ];
>
> sregex arglist = (s1= identifier) >> *( ',' >> (s2= arglist))
> [ std::vector<math_id> ids( as<math_arglist>(s2).begin(),
> as<math_arglist>(s2).end() );
> ids.push_back(as<math_id>(s1));
> return ids; ]
>
> sregex funcdef = ((s1= identifier) >> '(' >> (s2= arglist) >> ")=" >>
> (s3= addition))
> [ return math_funcdef( as<math_id>(s1), as<math_arglist>(s2),
> as<math_addition>(s3) ) ];
>
> I just want to avoid the funcdef above to be responsible for the
> construction of the various math_* objects the funcdef will hold (some
> of which will be complex to construct). And if I really must store the
> objects in an external stack, how should the parent-objects find their
> child-objects?

It really sounds to me that this problem would be better addressed by
Boost.Spirit and its attributed parsers.

-- 
Eric Niebler
Boost.org

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net