34

I've been looking recently at Boost.Spirit (the version included in Boost 1.39), however I'm quite confused from the docs alone. What I'm looking for is for an example of a toy language implemented with Boost.Spirit -- something along the lines of a tiny Javascript or Lua or so, where you basically create an AST and process is. I'd be happy if it just supports function/variable definitions and the basic operators, I just want to see how you would create a normal AST using Boost.Spirit, and how to implement basic rules like identifiers, declarations, etc.

So far, I've tried the calculator example, but it's unclear for me how to implement a larger grammar with Spirit. The mini_c example which is bundled on the other hand looks quite complicated already, and it's not too well documented. Is there some easy to understand guide to Boost.Spirit out there, or a book maybe?

Anteru
  • 19,042
  • 12
  • 77
  • 121
  • If you want to learn about the new Boost Spirit X3, I would recommend [this video](https://www.youtube.com/watch?v=xSBWklPLRvw). The powerpoint slides are found [here](http://ciere.com/cppnow15/using_x3.pdf). – Jaime Ivan Cervantes Jul 07 '17 at 22:38
  • As an engineering challenge, I've implemented BASIC interpreter using Boost.Spirit X3. It's functional enough to run some text-based games from 80-s and other programs. https://github.com/black-square/BASIC-boost.spirit Please take a look if you have a moment. I would be happy to answer any questions or to hear any feedback. Also, it's MIT licensed so feel free to use it in any way you want. – Dmitry Shesterkin Aug 27 '22 at 22:39

5 Answers5

14

An introductory article from CP

A JSON parser implemented using Boost.Spirit from CodeProject

Spirit Application Repository

Modicom
  • 299
  • 2
  • 4
  • 2
    Here's my own take at JSON using Spirit V2 https://github.com/sehe/spirit-v2-json for comparison – sehe Sep 15 '13 at 11:10
  • Here's a nice write-up on Dr. Dobbs: [http://www.drdobbs.com/cpp/the-spirit-parser-library-inline-parsing/184401692](http://www.drdobbs.com/cpp/the-spirit-parser-library-inline-parsing/184401692?pgno=1) – John Leimon May 03 '14 at 04:36
  • I suggest the original boost::spirit docs/tutorials: https://www.boost.org/doc/libs/1_40_0/libs/spirit/doc/html/spirit/qi_and_karma.html#spirit.qi_and_karma.tutorials.quick_start – GRASBOCK Mar 20 '20 at 15:25
8

The Spirit documentation includes examples and tutorials. An example for generating an AST for a mini XML-like language is included. A very useful overview presentation by Joel de Guzman and Hartmut Kaiser is also available.

peter rautek
  • 129
  • 1
  • 4
5

please keep in mind there is a newer boost spirit version with the completely new approach towards generating AST.

varnie
  • 2,523
  • 3
  • 35
  • 42
3

The book The Boost C++ Libraries has a chapter about Spirit which I personally found pretty useful. It's available online, here's a direct link:

Chapter 11. Boost.Spirit

ChristophK
  • 733
  • 7
  • 20
2

Using Spirit 2.3: Qi and Karma

I think this pdf is quite good for beginners.

huron
  • 762
  • 1
  • 5
  • 22