Tuesday, 14 October 2008

How to Create a Math Calculator in C, the right Way

Here is a small PoC on how to build a calculator with a lexer (re2c) and a parser generator (lemon) in C. It supports the four basic mathematical operations and parentheses.

Thanks to MacVicar for his help.

I'm not sure if it works on other platforms beside Linux, as that's all I have (not!), but hey, feel free to improve it! :-)

calc-0.1

9 comments:

  1. "The right way"? The easiest way to do it properly is probably using the Shunting-Yard algorithm to transform it into a Reverse Polish notation, and then use another simple algorithm to compute the value of a RPN expression.

    ReplyDelete
  2. It's thought for those aiming at developing it further and eventually turn it into a scripting language.

    The calculator by itself is only an example.

    I wrote it with re2c and lemon because there were people complaining on the re2c mailing list that there's no such example.

    ReplyDelete
  3. It seems the link to the source code is no longer valid. :(

    It's hard to find lemon/re2c examples, so if you could update the link, it'd be great!

    ReplyDelete
  4. please update the link ?

    ReplyDelete
  5. can you fix the broken link ?

    ReplyDelete
  6. still broken link

    ReplyDelete
  7. I've lost the sources, but I'll try to put together a new version in the coming weeks, whenever I'll find some spare time.

    ReplyDelete
  8. sources:
    http://www.daniweb.com/forums/thread150222.html

    ReplyDelete
  9. Hi,

    Thanks for sharing knowledge.

    The link to the "calc-0.1" (http://tools.yap-project.com/counter/click.php?id=6) is broken.
    Could you please fix it?

    Regards
    Z.

    ReplyDelete