parse out a pointer, return a ValType reference

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

parse out a pointer, return a ValType reference

Chadderack
Hello,

My name is Chad Lehman... I'm new to this list.

I've created a scripting language that evaluates arithmetic expressions at
runtime.

The form is like this:

[00000000]/2 + (3 * [00000001]/5)

where the bracketed numbers are actually pointers to "ValueType" objects
("ValueType" is the template type of the parsing function)

template<typename ValueType>
void getAttribute(ValueType &v, const char *attr_name);

-------------------------------------------------------

I studied Joel's "calculator" grammar, and based pretty much
everything on that, except that the "factor" rule also includes
this "pointer to object reference" parser.

I can parse out the hex value no problem (with Joel's existing hex parsers),
and I've created an actor "ptr_assign_a()" that takes a ValueType reference,
and assigns the value of the "pointee" (object pointed-at by the parsed-out
pointer) to it:

__int64 ptr;
ValueType temp;

e.g. pointer_parser[ptr_assign_a(temp,ptr)] //  evaluates "temp =
*((ValueType *)ptr);

--------------------------------------------------------

Getting to the problem here...

...as you know (& realize), a calculator parser is recursive...
that makes extracting the values into temporaries (and then
keeping track of them) nearly impossible.

I've decided to use Joel's "closures" to provide each "rule" in
the grammar with a local "ValueType" variable.

The problem becomes... since the pointer_parser yeilds a
"real value", instead of a reference to a ValueType object,
I can't use assignments like this:

[var(temp) = arg1]

The value of arg1, after "pointer_parser" would be, essentially,
an address... not a reference to a ValueType object.

---------------------------------------------------------

I tried to create an "object_p" object parser, of sorts, by
deriving from "uint_parser", passing in an extra template
parameter "RefType", and then decorating the return value of
"parse()" as:

return *((RefType
*)(boost::spirit::impl::contiguous_parser_parse<result_t>(impl_t(), scan,
scan)));

----------------------------------------------------------

That doesn't work, because the actor implementation doesn't
compile when I write that...

It complains:

c:\projects\boost_1_31_0\boost\spirit\core\composite\sequence.hpp(54) :
error C2440: 'initializing' : cannot convert from 'long' to
'boost::spirit::match<boost::spirit::nil_t>'

----------------------------------------------------------

I realize the parser won't score a "hit" unless the parsed-out
type matches the type the actor expects... in fact... it doesn't
compile!

....

I'm actually a little swamped by all the layers of abstraction
in the spirit code... and don't quite understand if it's even
possible to create a "object_ref_p<ValueType>" parser, or the like...

I'm left to cumbersome actor chains, like this for addition:

ValueType temp, temp2;

// I've defined an actor "add_a()"

'+' >> _term[phoenix::var(temp) = _term.val][phoenix::var(temp2) =
_expression.val][add_a(temp2,temp)][_expression.val = phoenix::var(temp2)]

----------------------------------------------------------

I'd love to be able to do something like:

'+' >> _term[_expression.val += arg1]

but again, the pointer_parser (in the "factor" rule) doesn't
return type "ValueType"... it returns a pointer value.



Thank you for your attention... and any possible help.
Something that could parse out a pointer, and return a ValueType
reference to the pointee would be valuable to many more people
than just me, I think.

Thanks again.


Chad Lehman



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: parse out a pointer, return a ValType reference

Chadderack
[SOLVED]

Nevermind, all... seems I didn't quite understand what closure variables did
for us. I didn't realize that an embedded rule's closure variable could be
picked up in the outer rule's "phoenix::arg1"...

[example]

// imagine an actor "assign_ptr_p()" that takes two arguments; a
ReferenceType, and a Pointer type...
// it will do this assignment:

// RefType r = *((RefType *)PtrType p)

struct calc_closure : boost::spirit::closure<calc_closure, unsigned long>
{
    member1 val;
};

std::string str;
unsigned long value = 10, newvalue = 0, finalval = 0,ptr = 0;
boost::spirit::rule<calc_closure::context_t> first, second, third;

str = "[" + &value + "]";

first = '[' >> uint_p[assign_a(ptr)][assign_ptr_a(newvalue,ptr)][first.val =
phoenix::var(newvalue)] >> ']';

second = first[second.val = phoenix::arg1];

third = second[third.val = phoenix::arg1];

parse(str.c_str(), third[phoenix::var(finalval) = phoenix::arg1], space_p);

-------------------------------------------------------------------------

In the above scenario, I didn't realize that you could assign "newvalue" to
"first.val" in the "first" rule, and that
that value could be passed back simply by using "phoenix::arg1".


That really "rocks", Mr. Joel Guzman :-)

Thanks again.



Chad


> Hello,
>
> My name is Chad Lehman... I'm new to this list.
>
> I've created a scripting language that evaluates arithmetic expressions at
> runtime.
>
> The form is like this:
>
> [00000000]/2 + (3 * [00000001]/5)
>
> where the bracketed numbers are actually pointers to "ValueType" objects
> ("ValueType" is the template type of the parsing function)
>
> template<typename ValueType>
> void getAttribute(ValueType &v, const char *attr_name);
>
> -------------------------------------------------------
>
> I studied Joel's "calculator" grammar, and based pretty much
> everything on that, except that the "factor" rule also includes
> this "pointer to object reference" parser.
>
> I can parse out the hex value no problem (with Joel's existing hex
parsers),
> and I've created an actor "ptr_assign_a()" that takes a ValueType
reference,
> and assigns the value of the "pointee" (object pointed-at by the
parsed-out

> pointer) to it:
>
> __int64 ptr;
> ValueType temp;
>
> e.g. pointer_parser[ptr_assign_a(temp,ptr)] //  evaluates "temp =
> *((ValueType *)ptr);
>
> --------------------------------------------------------
>
> Getting to the problem here...
>
> ...as you know (& realize), a calculator parser is recursive...
> that makes extracting the values into temporaries (and then
> keeping track of them) nearly impossible.
>
> I've decided to use Joel's "closures" to provide each "rule" in
> the grammar with a local "ValueType" variable.
>
> The problem becomes... since the pointer_parser yeilds a
> "real value", instead of a reference to a ValueType object,
> I can't use assignments like this:
>
> [var(temp) = arg1]
>
> The value of arg1, after "pointer_parser" would be, essentially,
> an address... not a reference to a ValueType object.
>
> ---------------------------------------------------------
>
> I tried to create an "object_p" object parser, of sorts, by
> deriving from "uint_parser", passing in an extra template
> parameter "RefType", and then decorating the return value of
> "parse()" as:
>
> return *((RefType
> *)(boost::spirit::impl::contiguous_parser_parse<result_t>(impl_t(), scan,
> scan)));
>
> ----------------------------------------------------------
>
> That doesn't work, because the actor implementation doesn't
> compile when I write that...
>
> It complains:
>
> c:\projects\boost_1_31_0\boost\spirit\core\composite\sequence.hpp(54) :
> error C2440: 'initializing' : cannot convert from 'long' to
> 'boost::spirit::match<boost::spirit::nil_t>'
>
> ----------------------------------------------------------
>
> I realize the parser won't score a "hit" unless the parsed-out
> type matches the type the actor expects... in fact... it doesn't
> compile!
>
> ....
>
> I'm actually a little swamped by all the layers of abstraction
> in the spirit code... and don't quite understand if it's even
> possible to create a "object_ref_p<ValueType>" parser, or the like...
>
> I'm left to cumbersome actor chains, like this for addition:
>
> ValueType temp, temp2;
>
> // I've defined an actor "add_a()"
>
> '+' >> _term[phoenix::var(temp) = _term.val][phoenix::var(temp2) =
> _expression.val][add_a(temp2,temp)][_expression.val = phoenix::var(temp2)]
>
> ----------------------------------------------------------
>
> I'd love to be able to do something like:
>
> '+' >> _term[_expression.val += arg1]
>
> but again, the pointer_parser (in the "factor" rule) doesn't
> return type "ValueType"... it returns a pointer value.
>
>
>
> Thank you for your attention... and any possible help.
> Something that could parse out a pointer, and return a ValueType
> reference to the pointee would be valuable to many more people
> than just me, I think.
>
> Thanks again.
>
>
> Chad Lehman



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: parse out a pointer, return a ValType reference

Joel de Guzman-2
Chadderack wrote:
> [SOLVED]

Ohh! Just as I was about to look into it :) Nice to hear!

------------------
>
> In the above scenario, I didn't realize that you could assign "newvalue" to
> "first.val" in the "first" rule, and that
> that value could be passed back simply by using "phoenix::arg1".
>
>
> That really "rocks", Mr. Joel Guzman :-)

Thanks! Wait till you see Phoenix-2. This will be released very soon
now, thanks to Dan Marsden and Daniel Wallin who helped a lot in
the implementation.

Cheers!
--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general