Re: boost::spirit and boost in general for c++ people.

  • From: Kerneels Roos <kerneels@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Fri, 08 Apr 2011 10:03:15 +0200

Ty, I haven't had the time to study your code in depth, but I've got a feeling that a Python implementation would be way more readable and simple. Now how big the bytecode would be and how well it would perform would have to be seen.


Hey! How about a second generation of fruit basket programs -- fruit calculators?

We should also seriously look at fruit basket improved programs based on frameworks, with persistent storage. Anyone for ideas of what to put into such a specification?

Keep well wonderful people!

Kerneels

On 4/8/2011 2:17 AM, Littlefield, Tyler wrote:
I have no clue what your message said since you contradicted yourself three times. "will take you longer less nashing of the teeth." boost::spirit isn't all that dificult, and it was rather quick. "less of a memory hog:" my calculator takes 916 bytes RSS memory. maybe 3k at most, the binary size is under 20 kb when optomised.
On 4/7/2011 6:11 PM, Ken Perry wrote:
Sure there is a better way. You write it to fit the problem. Now better is a subjective term because while the code will look cleaner be better less memory hog. I t will take you long.er and much nashing of teeth. So the
question is what you mean by better.

Ken

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
Sent: Thursday, April 07, 2011 3:20 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: boost::spirit and boost in general for c++ people.

I don't think a better way exists in C++. That's kind of my point.

That's why I think it's so sad.

Ken, thoughts?

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: Thursday, April 07, 2011 12:42 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: boost::spirit and boost in general for c++ people.

Hi Sina,
What I meant when I said I could read the code was simply that I was glad
that I was understanding a lot of it. It would really be
instructive to me if you would help me understand a better way to write it,
because I would love to learn OO correctly the first
time. And I would be greatful if you would be able to do that. I know you
are very busy.

Thanks.

Jim


Jim Homme,
Usability Services,
Phone: 412-544-1810. Skype: jim.homme
Highmark recipients,  Read my accessibility blog. Discuss accessibility
here. Accessibility Wiki: Breaking news and accessibility
advice

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
Sent: Thursday, April 07, 2011 11:22 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: boost::spirit and boost in general for c++ people.

Good god, this is ugly.

Seriously, you guys find that code easy to read?

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Ken Perry
Sent: Thursday, April 07, 2011 10:07 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: boost::spirit and boost in general for c++ people.

Snicker welcome to the future... Hmm how many times have I told you to use
it and you told me no???

Ken

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Littlefield,
Tyler
Sent: Thursday, April 07, 2011 9:09 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: boost::spirit and boost in general for c++ people.

hello all:
Recently I got into using boost a bit and started finding uses for it
all over the place. I would like to encourage those who haven't yet and
use c++ to check it out; it is quite powerful and can eas some of your
work and provide really cool features along the way. For example:
boost::function lets you take callbacks in multiple forms; boost::bind
will let you bind a functor to it's arguments and pass that as your
callback, which means you can bind to a static member function, a member
function (and pass the object with boost::ref), a global function and
you can also use boost::lambda.
There are many more utilities out there; I highly recommend checking out
boost::asio, boost::fusion, boost::function and boost::bind.
In the spirit of convincing you all, I wrote a small calculator program
that does single operations (1+3, 3*5), etc and returns the result.
My next step is to split up my parsing into different rulesets and then
work on generating an AST so that I can handle larger more complex
calculations.
/*
A simple calculator, supports multiplication, division, addition and
subtraction.
*/
#include<boost/spirit/include/qi.hpp>
#include<boost/spirit/include/phoenix_core.hpp>
#include<boost/spirit/include/phoenix_operator.hpp>
#include<boost/spirit/include/phoenix_operator.hpp>
#include<iostream>
#include<string>
#include<boost/fusion/include/adapt_struct.hpp>
#include<boost/fusion/container/vector.hpp>
#include<boost/fusion/container/vector/convert.hpp>
#include<boost/fusion/include/io.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

struct calculation
{
    int a, b;
    char op;
};
BOOST_FUSION_ADAPT_STRUCT(calculation, (int, a) (char, op) (int, b) )

template<typename iterator>
class CParser:public qi::grammar<iterator, calculation(),
qi::ascii::space_type>
{
    qi::rule<iterator, calculation(), qi::ascii::space_type>  crule;
public:
    CParser():CParser::base_type(crule)
    {
      using qi::ascii::char_;
      using qi::int_;

      crule %= (int_>>
                (char_('+')|char_('-')|char_('*')|char_('/'))>>
                int_);
    }
};

int main()
{
    CParser<std::string::iterator>  p;
    std::string str;
    calculation result;

    std::cout<<  "Enter your calculation>";
getline(std::cin, str);

    bool r = phrase_parse(str.begin(), str.end(), p, qi::ascii::space,
result);
    if (r)
      {
        std::cout<<  "a: "<<  result.a<<  " b: "<<  result.b<<  " op:"
<<  result.op<<  std::endl;
        switch(result.op)
          {
          case '+':
std::cout<< "Result: "<< (result.a + result.b)<< std::endl;
            break;
          case '-':
std::cout<< "result: "<< (result.a - result.b)<< std::endl;
            break;
          case '*':
std::cout<< "Result: "<< (result.a * result.b)<< std::endl;
            break;
          case '/':
            if (result.a == 0)
              {
                std::cout<<  "Division by zero error."<<  std::endl;
                break;
              }
std::cout<< "result: "<< (result.a / result.b)<< std::endl;
            break;
          default:
            std::cout<<  "Invalid operation."<<  std::endl;
          }
      }
    else
      {
        std::cout<<  "Invalid input."<<  std::endl;
      }

    return 0;
}

--

Thanks,
Ty

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


This e-mail and any attachments to it are confidential and are intended
solely for use of the individual or entity to whom they are
addressed.  If you have received this e-mail in error, please notify the
sender immediately and then delete it.  If you are not the
intended recipient, you must not keep, use, disclose, copy or distribute
this e-mail without the author's prior permission.  The
views expressed in this e-mail message do not necessarily represent the
views of Highmark Inc., its subsidiaries, or affiliates.
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind





--
Kerneels Roos
Cell: +27 (0)82 309 1998
Skype: cornelis.roos

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: