osdir.com
mailing list archive
Mozy Online Backup: 2GB Free. Automatic. Secure.

Subject: Re: new proto transforms in development - msg#00011

List: parsers.spirit.devel

Date: Prev Next Index Thread: Prev Next Index
On 01/29/08 13:07, Eric Niebler wrote:
Larry Evans wrote:
On 01/29/08 10:28, Eric Niebler wrote:
Larry Evans wrote:
I'm trying to figure out how to
add non-terminals, semantic attributes and actions to the
grammar. Do you have any ideas on how to do this?
It would help if you had a specific question about Proto. Can you describe the problems you're running into?
I haven't really tried, but I've struggled before with understanding how to do things with proto;


Take another look. Things are vastly simpler with the new transforms.


[snip]
Eric,

I've been struggling with this for several days now. That attached
file illustrates the problems I'm having. In short, I want
to create a transform from one algebra to another,
So, the first step is to define the terminals in the intial
algebra. That's the.

template<alg_num Num>
struct alg_expr0
: proto::terminal<alg_tag0<Num> >
{};

Next I need to build expressions in this algebra. I tried to do this
first with a unary expression. That's the:

template<alg_num Anum,class Expr0>
struct alg_expr1
: unary_expr<alg_tag1<Anum>,Expr0>
{
//definition of result template and operator() template
//based on corresponding unary_arity struct in
//libs/xpressive/proto/test/examples.cpp.
//(see attachment)
};

However, I'm getting errors shown in the 2nd attachment.

What am I doing wrong?

(BTW, I'm hoping to use what I learn to add attributes
to a modified toy_spirit. The attributes will
just be a transform of the grammar. For example,
the attributes of a SpiritSequence would be
a tuple and that of a SpiritAlternate would be
a variant. Each terminal and non-terminal in
the grammar would have a corresponding
user specified attribute).

TIA.

-regards,
Larry
//Purpose:
// Learn by trial-and-error and reading docs, how to
// use transforms to translate one tree into a
// "corresponding" tree, somewhat like algebraic
// morphisms.

#include <boost/xpressive/proto/proto.hpp>
#include <boost/xpressive/proto/transform.hpp>

//#define DEBUG_PRINT
#ifdef DEBUG_PRINT
#include <boost/utility/type_name_print.hpp>
#include <boost/utility/trace_scope.hpp>
#endif

using namespace boost;
using namespace boost::proto;

enum alg_num
{ alg_0
, alg_1
};
template<alg_num Num>
struct alg_tag0
{
typedef alg_tag0 proto_arg0;//Just guessing
};
template<alg_num Num>
struct alg_expr0
: proto::terminal<alg_tag0<Num> >
{};
template<>
struct alg_expr0<alg_1>
: terminal<alg_tag0<alg_1> >
{
template<typename Sig>
struct result;

template<typename This, typename Expr, typename State, typename Visitor>
struct result<This(Expr, State, Visitor)>
{
typedef alg_expr0<alg_1> type;
};

template<typename Expr, typename State, typename Visitor>
typename result<void(Expr, State, Visitor)>::type
operator ()(Expr const &expr, State const &, Visitor &) const
{
return alg_expr0<alg_1>();
}

};

struct dummy
{};
dummy state;
dummy viztr;

template<alg_num Anum>
void test_nullary(void)
{
typedef alg_expr0<Anum> nullary_type;
typename nullary_type::type const nullary_valu={};
nullary_valu(nullary_valu,state,viztr);
#ifdef DEBUG_PRINT
::utility::trace_scope ts("test_nullary");
mout()<<":Anum="<<Anum<<"\n";
mout()<<":nullary_type=\n";
::utility::type_name_print(nullary_type());
mout()<<":nullary_valu=\n";
::utility::type_name_print(nullary_valu);
mout()<<":nullary_type()(nullary_valu,,)=\n";
::utility::type_name_print(nullary_type()(nullary_valu,state,viztr));
mout()<<":nullary_valu(nullary_valu,,)=\n";
::utility::type_name_print(nullary_valu(nullary_valu,state,viztr));
#endif
}

template<alg_num Num>
struct alg_tag1
{};
template<alg_num Anum,class Expr0>
struct alg_expr1
: unary_expr<alg_tag1<Anum>,Expr0>
{
#if 1
template<typename Sig>
struct result;

template<typename This, typename Expr, typename State, typename Visitor>
struct result<This(Expr, State, Visitor)>
{
/*<< Get the child. >>*/
typedef typename proto::result_of::arg<Expr>::type arg0_expr;

/*<< Apply This to child. >>*/
typedef typename boost::result_of<This(arg0_expr, State,
Visitor)>::type type;
};

template<typename Expr, typename State, typename Visitor>
typename result<alg_expr1<Anum,Expr0>(Expr, State, Visitor)>::type
operator ()(Expr const &expr, State const &, Visitor &) const
{
return typename result<alg_expr1<Anum,Expr0>(Expr, State,
Visitor)>::type();
}
#endif

};
template<alg_num Anum>
void test_unary(void)
{
typedef alg_expr0<Anum> nullary_type;
typename nullary_type::type nullary_valu={};
typedef when< logical_not<nullary_type>, alg_expr1<Anum,nullary_type> >
unary_type;
unary_type unary_valu;
unary_type()(!nullary_valu,state,viztr);
#ifdef DEBUG_PRINT
::utility::trace_scope ts("test_unary");
mout()<<":Anum="<<Anum<<"\n";
mout()<<":unary_type()(!nullary_valu,,)=\n";
::utility::type_name_print(unary_valu(!nullary_valu,state,viztr));
#endif
}
int main(void)
{
// test_nullary<alg_0>();
test_nullary<alg_1>();
test_unary<alg_1>();
return 0;
}



-*- mode: compilation; default-directory:
"~/prog_dev/boost-svn/ro/boost-trunk/sandbox/lje/libs/xpressive/proto/experiments3/"
-*-
Compilation started at Sun Feb 10 13:34:18

bjam transform_experiments.unit-test
warning: Graph library does not contain optional GraphML reader.
note: to enable GraphML support, set EXPAT_INCLUDE and EXPAT_LIBPATH to the
note: directories containing the Expat headers and libraries, respectively.
warning: skipping optional Message Passing Interface (MPI) library.
note: to enable MPI support, add "using mpi ;" to user-config.jam.
note: to suppress this message, pass "--without-mpi" to bjam.
note: otherwise, you can safely ignore this message.
Building Boost.Regex with the optional Unicode/ICU support disabled.
Please refer to the Boost.Regex documentation for more information
(don't panic: this is a strictly optional feature).
...patience...
...found 1267 targets...
...updating 3 targets...
gcc.compile.c++
../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static/transform_experiments.o
transform_experiments.cpp: In instantiation of 'alg_expr1<alg_1,
alg_expr0<alg_1> >::result<alg_expr1<alg_1, alg_expr0<alg_1> >
()(alg_tag0<alg_1>, dummy, dummy)>':
./../../../../../boost/utility/result_of.hpp:69: instantiated from
'boost::detail::result_of_nested_result<alg_expr1<alg_1, alg_expr0<alg_1> >,
alg_expr1<alg_1, alg_expr0<alg_1> > ()(alg_tag0<alg_1>, dummy, dummy)>'
../../../../../../boost/utility/result_of.hpp:35: instantiated from
'boost::detail::result_of_impl<alg_expr1<alg_1, alg_expr0<alg_1> >,
alg_expr1<alg_1, alg_expr0<alg_1> > ()(alg_tag0<alg_1>, dummy, dummy), false>'
../../../../../../boost/utility/result_of.hpp:28: instantiated from
'boost::result_of<alg_expr1<alg_1, alg_expr0<alg_1> > ()(alg_tag0<alg_1>,
dummy, dummy)>'
transform_experiments.cpp:97: instantiated from 'alg_expr1<alg_1,
alg_expr0<alg_1> >::result<alg_expr1<alg_1, alg_expr0<alg_1> >
()(boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::args0<alg_tag0<alg_1> >, 0l>, dummy, dummy)>'
../../../../../../boost/utility/result_of.hpp:69: instantiated from
'boost::detail::result_of_nested_result<alg_expr1<alg_1, alg_expr0<alg_1> >,
alg_expr1<alg_1, alg_expr0<alg_1> >
()(boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::args0<alg_tag0<alg_1> >, 0l>, dummy, dummy)>'
../../../../../../boost/utility/result_of.hpp:35: instantiated from
'boost::detail::result_of_impl<alg_expr1<alg_1, alg_expr0<alg_1> >,
alg_expr1<alg_1, alg_expr0<alg_1> >
()(boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::args0<alg_tag0<alg_1> >, 0l>, dummy, dummy), false>'
../../../../../../boost/utility/result_of.hpp:28: instantiated from
'boost::result_of<alg_expr1<alg_1, alg_expr0<alg_1> >
()(boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::args0<alg_tag0<alg_1> >, 0l>, dummy, dummy)>'
transform_experiments.cpp:97: instantiated from 'alg_expr1<alg_1,
alg_expr0<alg_1> >::result<alg_expr1<alg_1, alg_expr0<alg_1> >
()(boost::proto::exprns_::expr<boost::proto::tag::logical_not,
boost::proto::argsns_::args1<boost::proto::refns_::ref_<boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::args0<alg_tag0<alg_1> >, 0l> > >, 1l>, dummy, dummy)>'
transform_experiments.cpp:116: instantiated from 'void test_unary() [with
alg_num Anum = alg_1]'
transform_experiments.cpp:128: instantiated from here
transform_experiments.cpp:97: error: no type named 'type' in 'struct
boost::result_of<alg_expr1<alg_1, alg_expr0<alg_1> > ()(alg_tag0<alg_1>, dummy,
dummy)>'
transform_experiments.cpp: In function 'void test_unary() [with alg_num Anum =
alg_1]':
transform_experiments.cpp:128: instantiated from here
transform_experiments.cpp:116: error: no match for call to
'(boost::proto::transform::when<boost::proto::op::logical_not<alg_expr0<alg_1>
>, alg_expr1<alg_1, alg_expr0<alg_1> > >) (const
boost::proto::exprns_::expr<boost::proto::tag::logical_not,
boost::proto::argsns_::args1<boost::proto::refns_::ref_<boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::args0<alg_tag0<alg_1> >, 0l> > >, 1l>, dummy&, dummy&)'
transform_experiments.cpp:115: warning: unused variable 'unary_valu'

"/usr/bin/g++-4.1" -ftemplate-depth-128 -O0 -fno-inline -Wall -g
-ftemplate-depth-1024 -DBOOST_ALL_NO_LIB=1 -DBOOST_TEST_NO_AUTO_LINK=1
-I"../../../../../.." -I"../../../../../functional/factory"
-I"../../../../../functional/forward" -I"../../../../../lje"
-I"../../../../../.." -c -o
"../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static/transform_experiments.o"
"transform_experiments.cpp"

...failed gcc.compile.c++
../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static/transform_experiments.o...
...skipped
<p../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static>transform_experiments
for lack of
<p../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static>transform_experiments.o...
..skipped
<p../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static>transform_experiments.passed
for lack of
<p../../../../../../bin.v2/sandbox/lje/libs/xpressive/proto/experiments3/gcc-4.1/debug/link-static>transform_experiments...
...failed updating 1 target...
...skipped 2 targets...

Compilation exited abnormally with code 1 at Sun Feb 10 13:34:21
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/_______________________________________________
Spirit-devel mailing list
Spirit-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/spirit-devel
Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

Re: new proto transforms in development

Eric Niebler wrote: > Larry Evans wrote: >> However, I was looking at toy_spirit2 and was trying to fathom: >> >> return parse_fun(SpiritExpr()(rule, 0, is_case_sensitive)); >> >> So I looked at: >> >> http://boost-sandbox.sourceforge.net/libs/proto/doc/html/boost/proto/control/or_.html >> >> and it showed no or_<...>::operator(). > > It's a Doxygen bug. Any function that has a return type that uses > result_of doesn't get emitted by Doxygen. I guess Dox doesn't expect to > find parens () in a return type. FYI, it looks like the latest Doxygen (1.5.4) has fixed this bug, so all the operator() overloads are showing up in the docs now. -- Eric Niebler Boost Consulting www.boost-consulting.com ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Next Message by Date: click to view message preview

Re: new proto transforms in development

Larry Evans wrote: > Eric, > > I've been struggling with this for several days now. That attached > file illustrates the problems I'm having. In short, I want > to create a transform from one algebra to another, > So, the first step is to define the terminals in the intial > algebra. That's the. > > template<alg_num Num> > struct alg_expr0 > : proto::terminal<alg_tag0<Num> > > {}; OK. > Next I need to build expressions in this algebra. I tried to do this > first with a unary expression. That's the: > > template<alg_num Anum,class Expr0> > struct alg_expr1 > : unary_expr<alg_tag1<Anum>,Expr0> > { > //definition of result template and operator() template > //based on corresponding unary_arity struct in > //libs/xpressive/proto/test/examples.cpp. > //(see attachment) > }; I don't know what this is. You're inheriting from unary_expr, so you want this to be a grammar rule. But you're implementing a nested result<> template and operator() as if this were a transform. Which is it? Proto-3 is unlike Proto-2 in this regard. Grammars are not transforms, and transforms are not grammars. You use when<> to associate a transform with a grammar. > However, I'm getting errors shown in the 2nd attachment. > > What am I doing wrong? I can't really tell what you're trying to do, so I can't say what you're doing wrong. This line: typedef when< logical_not<nullary_type> , alg_expr1<Anum,nullary_type> > unary_type; Seems to suggest to me that you're trying to write a grammar that matches a !x and then returns a unary_expr<SomeTag, X>, is that right? If so, try something like this: // A callable transform that makes expressions // with alg_tag1<Anum> tag type typedef functional::make_expr<alg_tag1<Anum> > _make_alg_expr1; when< // Match logical_not<nullary_type> expressions: logical_not<nullary_type> // Take the nullary_type child, wrap it in an // alg_expr1 node: , _make_alg_expr1(_arg) > Try to avoid defining primitive transforms (with nested result<> templates and operator() overloads). They're harder to work with. You can do pretty much anything you need with high-level lambdas such as the one above. They're concise and powerful. HTH, -- Eric Niebler Boost Consulting www.boost-consulting.com ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Previous Message by Thread: click to view message preview

Re: new proto transforms in development

Eric Niebler wrote: > Larry Evans wrote: >> However, I was looking at toy_spirit2 and was trying to fathom: >> >> return parse_fun(SpiritExpr()(rule, 0, is_case_sensitive)); >> >> So I looked at: >> >> http://boost-sandbox.sourceforge.net/libs/proto/doc/html/boost/proto/control/or_.html >> >> and it showed no or_<...>::operator(). > > It's a Doxygen bug. Any function that has a return type that uses > result_of doesn't get emitted by Doxygen. I guess Dox doesn't expect to > find parens () in a return type. FYI, it looks like the latest Doxygen (1.5.4) has fixed this bug, so all the operator() overloads are showing up in the docs now. -- Eric Niebler Boost Consulting www.boost-consulting.com ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Next Message by Thread: click to view message preview

Re: new proto transforms in development

Larry Evans wrote: > Eric, > > I've been struggling with this for several days now. That attached > file illustrates the problems I'm having. In short, I want > to create a transform from one algebra to another, > So, the first step is to define the terminals in the intial > algebra. That's the. > > template<alg_num Num> > struct alg_expr0 > : proto::terminal<alg_tag0<Num> > > {}; OK. > Next I need to build expressions in this algebra. I tried to do this > first with a unary expression. That's the: > > template<alg_num Anum,class Expr0> > struct alg_expr1 > : unary_expr<alg_tag1<Anum>,Expr0> > { > //definition of result template and operator() template > //based on corresponding unary_arity struct in > //libs/xpressive/proto/test/examples.cpp. > //(see attachment) > }; I don't know what this is. You're inheriting from unary_expr, so you want this to be a grammar rule. But you're implementing a nested result<> template and operator() as if this were a transform. Which is it? Proto-3 is unlike Proto-2 in this regard. Grammars are not transforms, and transforms are not grammars. You use when<> to associate a transform with a grammar. > However, I'm getting errors shown in the 2nd attachment. > > What am I doing wrong? I can't really tell what you're trying to do, so I can't say what you're doing wrong. This line: typedef when< logical_not<nullary_type> , alg_expr1<Anum,nullary_type> > unary_type; Seems to suggest to me that you're trying to write a grammar that matches a !x and then returns a unary_expr<SomeTag, X>, is that right? If so, try something like this: // A callable transform that makes expressions // with alg_tag1<Anum> tag type typedef functional::make_expr<alg_tag1<Anum> > _make_alg_expr1; when< // Match logical_not<nullary_type> expressions: logical_not<nullary_type> // Take the nullary_type child, wrap it in an // alg_expr1 node: , _make_alg_expr1(_arg) > Try to avoid defining primitive transforms (with nested result<> templates and operator() overloads). They're harder to work with. You can do pretty much anything you need with high-level lambdas such as the one above. They're concise and powerful. HTH, -- Eric Niebler Boost Consulting www.boost-consulting.com ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by