Sunday, October 12, 2014

Implementing Re-entrant Parsers in Bison and Flex

Eric Raymond:

That rebarbative old interface generally broke a lot of rules about program structure and information hiding that we now accept as givens (to be fair, most of those had barely been invented at the time it was written in 1970 and were still pretty novel). It becomes a particular problem if you want to run multiple instances of your generated parser (or, heaven forfend, multiple parsers with different grammars) in the same binary without having them interfere with each other.

But it can be done. I’m going to describe how because (a) it’s difficult to extract from the documentation, and (b) right now (that is, using Bison 3.0.2 and Flex 2.5.35) the interface is in fact slightly broken and there’s a workaround you need to know.

Xcode’s built-in support breaks every few versions, so I recommend creating a Makefile to run your parser generator and adding the generated files to your project. Another advantage to this approach is that you can specify per-file flags such as -Wno-conversion to the compiler to silence warnings that you can’t do anything about.

Comments RSS · Twitter

Leave a Comment