Sunday, November 14, 2010

Simple Boost Spirit example.

Here's an example of a parser for a really simple domain specific language (DSL) created with Boost's Spirit framework.

Download the full source code

The parser accepts the following input:
new order
*(buy|sell amount stock_name price)
end order

Here are a few examples:
new order
buy 10 ABC 2.50
sell 20 QWE 5.00
end order

new order
buy 10 ABC 2.50
buy 10 ABC 2.50
buy 10 ABC 2.50
sell 20 QWE 5.00
end order

As you can see the input the parser accepts is really simple and I could have probably saved lots of time by writing the parser manually without using any framework. If you download and look at the source code you see it's fairly easy to extend the parser to take much more complicated input.