Boost logo

Boost Users :

Subject: Re: [Boost-users] [Proto]: How to use complex grammars in a domain/extension
From: Roland Bock (rbock_at_[hidden])
Date: 2010-09-21 09:19:58


On 09/21/2010 11:55 AM, Thomas Heller wrote:
> <snip>
> Solved the mistery. here is the code, explanation comes afterward:
>
> <snip>
> So, everything works as expected!
>
Thomas,

wow, this was driving me crazy, thanks for solving and explaining in all
the detail! :-)

I am still not sure if this isn't a conceptual problem, though:

The "equation" grammar is perfectly OK. But without or-ing it with the
"addition" grammar, the extension does not allow ANY valid expression to
be created. I wonder if that is a bug or a feature?

Thanks and regards,

Roland

> #include<boost/proto/proto.hpp>
>
> using namespace boost;
>
> typedef proto::terminal<int>::type terminal;
>
> struct addition:
> proto::or_
> <
> proto::plus<addition, addition>,
> proto::terminal<proto::_>
> >
> {};
>
> struct equation:
> proto::or_
> <
> proto::equal_to<addition, addition>
> >
> {};
>
> template<class Expr>
> struct extension;
>
> struct my_domain:
> proto::domain
> <
> proto::pod_generator< extension>,
> // we need both grammars in our domain grammar
> proto::or_<equation, addition>,
> proto::default_domain
> >
> {};
>
>
> template<class Expr>
> struct extension
> {
> BOOST_PROTO_EXTENDS(
> Expr
> , extension<Expr>
> , my_domain
> )
> };
>
> template<typename Grammar, typename Expr>
> void matches(Expr const&)
> {
> std::cout<< std::boolalpha
> << proto::matches<Expr, Grammar>::value<< "\n";
> }
>
> int main()
> {
> extension<terminal> i;
> extension<terminal> j;
>
>
> matches<equation>(i); // 1) false
> matches<equation>(i == j); // 2) true
> matches<equation>(i == i + i); // 3) true
> matches<equation>(i + i == i); // 4) true
> matches<equation>(i + i == i + i); // 5) true
> matches<equation>(i + i); // 6) false
> }
>
>


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