Compilers and Factories

A Presentation by Peter Miller

The techniques presented in this paper came about from a project where it was known in advance that at least two tools needed to be based on the same grammar. The requirement was to translate an application-specific language into C, and also produce a pretty-printer. This is often done with two separate grammar files which differ not in syntax but in the semantic code added to each grammar production. Is it possible to have a single grammar file and use it in two different tools?

A technique is presented which separates the grammar from the semantic processing, using simple C++ polymorphism, allowing two or more tools to share the same grammar code. The use of factory methods makes this possible. The implementation of a grammar using these factories is described, as well as some unexpected benefits and simplifications.

Direct link to video