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
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


2 comments:
"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.
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.
Post a Comment