A Simple Calculator with Ragel and Lemon
When building a compiler, lex/yacc (flex/bison) are ubiquitous. I decided to try and throw together the “Hello World” of parsers (a calculator) using newer lexing and parsing tools, ragel for lexing, and lemon for parsing.
There are examples for both lemon and ragel on their websites. One other example that I found integrating ragel with lemon was Zed Shaw’s earing.
A couple of notes on the integration:
- Ragel is somewhat like a templating engine, in that it embeds the lexical analyzer inside the rest of your source code. What you end up editing is a source file that consists of two distinct, but intertwined parts. First, your ragel finite state machine description, which consists of actions to take on token matches, with the actions taken written in your target language. Second, there is a section of a hybrid between C/C++ (or, whatever your target language is), and ragel directives, essentially saying “init the FSM here”, and “run the FSM here”.
- Ragel has a few variables which you need to define for the parser to use when it is run (const char*-s p, pe, ts, te, int-s cs, act). These variables need to be defined within the scope of ragel’s
%% write init;and%% write exec;, initializing and running the lexer, respectively. Thep,pepointers refer to the beginning and end of the current string to lex, while thets, tepointers refer to the beginning and end of the next lexer matched string. - Lemon takes one
.ygrammar file, and produces one.cparser file. The easiest way to get this integrated so that the ragel lexer can feed the lemon based grammar is to#includethe generated.cparser file at the top of your ragel lexing file.
Put it all together, and you too can make simplecalc, simple ragel/lemon based calculator!

Leave a Reply