Spirit with columns of matrices

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Spirit with columns of matrices

Ben Goodrich
Hi,

I have a task for which Spirit may be very helpful. But my task seems a
bit different from the ones envisioned in the documentation and on this
mailing list. Basically, I just need to manipulate numbers, and the
numbers are already in memory. All I need to do is parse a potentially
complicated expression that specifies how the numbers should be
manipulated. For inspiriation, I have been looking at the syntax for
code
(http://www.oreillynet.com/network/2003/05/06/examples/calculatorexample.html)
that implements a calculator with Spirit, but it is not quite sufficient.

If someone could give me a little guidance or link to a more relevant
example (or tell me if it is impossible!) that would be a big help.
Also, if anyone has any experience using Spirit with C++ code called by
R that might be even better.

Here goes: I have a Matrix container class, which has numbers arranged
in rows and columns. I need to select columns and apply the || binary
operator (for example), which is overloaded to produce 1 - (1 - left) *
(1 - right). So, in hard-coded syntax, (where P is a Matrix), this works
fine:

Matrix<double> example;
example = P(_,1) || P(_,2);

so that example is a Matrix with one column that is the result of
applying the || operator to the first and second columns of P.

But, in order to make it general, I need to let the user submit an
expression at run time that may involve different columns, different
operations, and / or more terms [example =
P(_,1)||P(_,2)||P(_,3)...||P(_,17);] This is where Spirit (hopefully)
comes in. But to continue with a slightly more complicated example, I
may need to properly parse a string like

"P(_,1) || (P(_,2)||P(_,3))"

so that it recognizes the column specified by each P(_,#) chunk,
recognizes the operation between each chunk, and applies the operation
correctly. The calculator example seems to have something like this,
where the extra sets of parenthesis can force the order of operations to
be something other than strictly left to right and also looks up
variables. In this example, the extra parentheses are unnecessary but
mathematically valid, although I could come up with an example where the
parentheses were mathematically necessary if a different operation were
used.

I don't have any previous experience with Spirit, but I can't come up
with any ideas as to how I would write a grammar to recognize substrings
like P(_,#) that have parentheses in them and also recognize parentheses
that just change the order of operations. But if anyone has an idea or
an example, I would greatly appreciate it.

Thanks,
Ben


-------------------------------------------------------
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
|  
Report Content as Inappropriate

Re: Spirit with columns of matrices

Joel de Guzman-2
Hi Ben,

Ben Goodrich wrote:

> Hi,
>
> I have a task for which Spirit may be very helpful. But my task seems a
> bit different from the ones envisioned in the documentation and on this
> mailing list. Basically, I just need to manipulate numbers, and the
> numbers are already in memory. All I need to do is parse a potentially
> complicated expression that specifies how the numbers should be
> manipulated. For inspiriation, I have been looking at the syntax for
> code
> (http://www.oreillynet.com/network/2003/05/06/examples/calculatorexample.html)
> that implements a calculator with Spirit, but it is not quite sufficient.
>
> If someone could give me a little guidance or link to a more relevant
> example (or tell me if it is impossible!) that would be a big help.
> Also, if anyone has any experience using Spirit with C++ code called by
> R that might be even better.

Have you seen the Spirit applications repository?
http://spirit.sourceforge.net/repository/applications/show_contents.php

> Here goes: I have a Matrix container class, which has numbers arranged
> in rows and columns. I need to select columns and apply the || binary
> operator (for example), which is overloaded to produce 1 - (1 - left) *
> (1 - right). So, in hard-coded syntax, (where P is a Matrix), this works
> fine:
>
> Matrix<double> example;
> example = P(_,1) || P(_,2);

What's the single underscore? Is that C++ code?

> so that example is a Matrix with one column that is the result of
> applying the || operator to the first and second columns of P.
>
> But, in order to make it general, I need to let the user submit an
> expression at run time that may involve different columns, different
> operations, and / or more terms [example =
> P(_,1)||P(_,2)||P(_,3)...||P(_,17);] This is where Spirit (hopefully)
> comes in. But to continue with a slightly more complicated example, I
> may need to properly parse a string like
>
> "P(_,1) || (P(_,2)||P(_,3))"
>
> so that it recognizes the column specified by each P(_,#) chunk,

What's the # ?

> recognizes the operation between each chunk, and applies the operation
> correctly. The calculator example seems to have something like this,
> where the extra sets of parenthesis can force the order of operations to
> be something other than strictly left to right and also looks up
> variables. In this example, the extra parentheses are unnecessary but
> mathematically valid, although I could come up with an example where the
> parentheses were mathematically necessary if a different operation were
> used.
>
> I don't have any previous experience with Spirit,

How about parsing in general?

> but I can't come up
> with any ideas as to how I would write a grammar to recognize substrings
> like P(_,#) that have parentheses in them and also recognize parentheses
> that just change the order of operations. But if anyone has an idea or
> an example, I would greatly appreciate it.

If your problem can be solved by a parser, then certainly, Spirit can
too. First, you'll have to formalize your grammar in terms of EBNF.
When you have your grammar, writing a Spirit parser will be more or
less straightforward. The next step would then be to connect your
parser to semantic actions which ultimately links your parser to
your data and manipulates them appropriately.

Regards,
--
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
Loading...