Skip to content
November 11, 2011 / pjcoup

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. The p,pe pointers refer to the beginning and end of the current string to lex, while the ts, te pointers refer to the beginning and end of the next lexer matched string.
  • Lemon takes one .y grammar file, and produces one .c parser file. The easiest way to get this integrated so that the ragel lexer can feed the lemon based grammar is to #include the generated .c parser 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: