Boost logo

Boost Users :

Subject: Re: [Boost-users] a question on boost::spirit with a quick program.
From: TONGARI (tongari95_at_[hidden])
Date: 2011-04-07 01:29:26

2011/4/7 Littlefield, Tyler <tyler_at_[hidden]>

> 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

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:

calculation, (int, a) (int, b) (char, op) )
// operandA, operandB, operator

crule %= (int_ >>
             (char_('+')|char_('-')|char_('*')|char_('/')) >>
// 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:

Besides, there's a dedicated Spirit mailing-list, and a good website


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at