Boost logo

Boost :

Subject: Re: [boost] Requesting a review of Metaparse
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2012-01-30 13:31:02


From: Abel Sinkovics
>The library can be used for implementing DSLs in C++, including DSLs
>making C++ template metaprogramming easier (see examples).

I like your compile to native code example:
"
#define LAMBDA(exp) apply_wrap1<function_parser, BOOST_STRING(#exp)>::type

LAMBDA(13) f1;
LAMBDA(2 + 3) f2;
LAMBDA(2 * 2) f3;
LAMBDA( 1+ 2*4-6/2) f4;
LAMBDA(2 * _) f5;

#endif

int main()
{
  using std::cout;
  using std::endl;

  cout
    << f1(11) << endl
    << f2(11) << endl
    << f3(11) << endl
    << f4(11) << endl
    << f5(11) << endl
    << f5(1.1) << endl
    ;
}
"

Right now it is hard coded that the function object generated by apply_wrap1 takes one argument. I'm pretty sure you could build compile time logic to search for the largest _2, _4 type token and select the specialization of the wrapper that matches the number of arguments used in the expression string.

You have a comment:
/*
 * The grammar
 *
 * expression ::= plus_exp
 * plus_exp ::= prod_exp ((plus_token | minus_token) prod_exp)*
 * prod_exp ::= value_exp ((mult_token | div_token) value_exp)*
 * value_exp ::= int_token | '_'
 */

And then implement the grammar using metaparse based metaprogramming, eventually declaring your compile time parser with this:

typedef build_parser<entire_input<expression> > function_parser;

I believe we could apply the metaparse library to itself to get a more natural syntax for declaring a metaparser:

typedef build_parser<entire_input<grammar<rule<_S("expression ::= plus_exp")>,
                                                        rule<_S("plus_exp ::= prod_exp ((plus_token | minus_token) prod_exp)*")>,
                                                        rule<_S("value_exp ((mult_token | div_token) value_exp)*")>,
                                                        rule<_S("value_exp ::= int_token | '_'")>
>,
                                                semantic_action<_S("value_exp"), build_value>,
                                                semantic_action<_S("prod_exp"), build_mult>,
                                                semantic_action<_S("plus_exp"), build_plus>
> > function_parser;

This is essentially what I have in mind when I think of metaparse and constexpr based mpl string used to make metaprogramming easier.

I'd be interested in seeing if you can get the above proposed syntax (or something similar) to compile to demonstrate the metapower of metaparse. A compile time parser for generating compile time parsers.

Regards,
Luke


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