a question on boost::spirit with a quick program.

Hello all:
Sorry for all the questions; I am kind of easing into boost and finding
cool things i can do with it, as well as uses for a couple of projects.
Eventually I would like to write a small scripting language using
boost::spirit, just to do it and get the practice using it. As a
starting point, I wrote a quick calculator (that just does small single
operations like x+x, x*x, etc). Now i have two questions. 1) My rule
seems to be off. the first number comes through clearly, but no matter
what I put in, I always get the first number and then a 42.
2) How would I go about extending this to implement order of operations,
parenthases, and longer operations (like 3+3*3)? I'm going to try to
implement a small function table so I can do tan(3), so I assume I will
need nultiple rules, how might that be accomplished?
Any comments and suggestions would be welcome. I have pasted the code
below. I was working from a tutorial, so early appologies if it feels
messy; I plan to clean up the namespace aliases and the using keywords.
/*
A simple calculator, supports multiplication, division, addition and
subtraction.
*/
#include

AMDG On 04/06/2011 03:12 PM, Littlefield, Tyler wrote:
Sorry for all the questions; I am kind of easing into boost and finding cool things i can do with it, as well as uses for a couple of projects. Eventually I would like to write a small scripting language using boost::spirit, just to do it and get the practice using it. As a starting point, I wrote a quick calculator (that just does small single operations like x+x, x*x, etc). Now i have two questions. 1) My rule seems to be off. the first number comes through clearly, but no matter what I put in, I always get the first number and then a 42. 2) How would I go about extending this to implement order of operations, parenthases, and longer operations (like 3+3*3)? I'm going to try to implement a small function table so I can do tan(3), so I assume I will need nultiple rules, how might that be accomplished? Any comments and suggestions would be welcome. I have pasted the code below. I was working from a tutorial, so early appologies if it feels messy; I plan to clean up the namespace aliases and the using keywords.
Spirit used to have a calculator example, but I can't seem to find it in the new docs. Basically you have two choices. You can add semantic actions to evaluate each subexpression as it's parsed, or you can let spirit generate an AST and write a recursive function that walks the tree and evaluates it. In Christ, Steven Watanabe

On 4/7/2011 10:02 AM, Steven Watanabe wrote:
AMDG
On 04/06/2011 03:12 PM, Littlefield, Tyler wrote:
Sorry for all the questions; I am kind of easing into boost and finding cool things i can do with it, as well as uses for a couple of projects. Eventually I would like to write a small scripting language using boost::spirit, just to do it and get the practice using it. As a starting point, I wrote a quick calculator (that just does small single operations like x+x, x*x, etc). Now i have two questions. 1) My rule seems to be off. the first number comes through clearly, but no matter what I put in, I always get the first number and then a 42. 2) How would I go about extending this to implement order of operations, parenthases, and longer operations (like 3+3*3)? I'm going to try to implement a small function table so I can do tan(3), so I assume I will need nultiple rules, how might that be accomplished? Any comments and suggestions would be welcome. I have pasted the code below. I was working from a tutorial, so early appologies if it feels messy; I plan to clean up the namespace aliases and the using keywords.
Spirit used to have a calculator example, but I can't seem to find it in the new docs.
Check out the "compiler_tutorial" directory. It's a progression from the simplest calculator to a mini-c compiler and a virtual machine. I intend to write a series of articles using these examples. Regards, -- Joel de Guzman http://www.boostpro.com http://boost-spirit.com

2011/4/7 Littlefield, Tyler
Hello all: Sorry for all the questions; I am kind of easing into boost and finding cool things i can do with it, as well as uses for a couple of projects. Eventually I would like to write a small scripting language using boost::spirit, just to do it and get the practice using it. As a starting point, I wrote a quick calculator (that just does small single operations like x+x, x*x, etc).
You can find the calculator examples in your BOOST_ROOT/libs/spirit/example/qi Now i have two questions. 1) My rule seems to be off. the first number comes
through clearly, but no matter what I put in, I always get the first number and then a 42.
Obvoiusly, it's because you messed up the order of members of 'calculation' struct and how the attributes are propogated to it. Let's take a look: BOOST_FUSION_ADAPT_STRUCT( calculation, (int, a) (int, b) (char, op) ) // operandA, operandB, operator crule %= (int_ >> (char_('+')|char_('-')|char_('*')|char_('/')) >> int_); // operandA, operator, operandB Noticed the order? So just change it into: BOOST_FUSION_ADAPT_STRUCT(calculation, (int, a)(char, op)(int, b)) then it'll work as expected. BTW, you used "cin >> str;" to get the line, it can extract the string before any space, you may want to use "getline(cin, str)" here. 2) How would I go about extending this to implement order of operations,
parenthases, and longer operations (like 3+3*3)?
Take a look at those examples.
I'm going to try to implement a small function table so I can do tan(3), so I assume I will need nultiple rules, how might that be accomplished?
You may want to use nabialek-trick: http://boost-spirit.com/home/articles/qi-example/nabialek-trick/ Besides, there's a dedicated Spirit mailing-list, and a good website (above). HTH
participants (4)
-
Joel de Guzman
-
Littlefield, Tyler
-
Steven Watanabe
-
TONGARI