.\" .\" UCSD p-System cross compiler .\" Copyright (C) 2006, 2007, 2010, 2012 Peter Miller .\" .ad l .hy 0 Compilers and Factories, Abstract

Compilers and Factories

By Peter Miller
<pmiller@opensource.org.au>

Abstract

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.

Assumed Knowledge

The reader is expected to be aware of Yacc grammars and understand basic OO concepts as implemented in C++.

Open Source Projects

There are two open source (GPL) projects which are using the techniques in this paper:

Biographical Details

Peter Miller have been a code cutter since he was 14, and built his first computer with a soldering iron, it was a kit from Dick Smith. Since then he has been coding for a little over 30 years, and have (so far) avoided the management trap and still codes professionally. He have been giving away code since before there was a GPL, and once he came across it he has used the GPL for all of his "non commercial" output ever since. He has 25 years experience with C and 15 years experience with C++.

Previous Speaking Experience

Peter Miller has presented papers at AUUG conferences and chapter meetings. He has given talks at SLUG meetings. He has organised a few CodeCon weekends for "camping and coding". .\" vim: set ts=8 sw=4 et :