johnson@p.cs.uiuc.edu (04/10/89)
The object-oriented paradigm works very well for compiler development. My main project is an optimizing compiler for Smalltalk that is written in Smalltalk. The reason that I am writing this compiler is to improve the performance of Smalltalk; presently Smalltalk is a fair bit slower than C and so my compiler is slower. This will presumably change once the compiler is able to compile itself. I wouldn't expect to see any performance degradation if I were using C++ instead of C. Why should I? Why does everybody expect OOP to cause performance degradation? There is no reason at all to make this assumption. It all depends on the implementation of the language, not the paradigm being used. Current implementations of C++ are as efficient as those of C. A number of the students in the compiler course this semester (which I am NOT teaching) are using C++ with YACC. As far as I know, there is no reason why you would need a special version of YACC. The output of YACC is mostly tables, which are independent of the paradigm used. The Smalltalk compiler uses recursive descent parsing, and it is amusing to note how well OOP works with that. Each rule of a grammar is implemented by a procedure in a recursive descent parser, and since a class is a set of procedures, a grammar is easily implemented by a class. Subclassing works perfectly to enhance grammars. We have several variant parsers, each implemented as a subclass of the original. In my opinion, however, OOP has little effect on parsing. The big advantage of OOP is in code optimization. There are lots of complicated data structures in code optimization. Different machines often need different sets of optimizations, so there is often a need for customization. Finally, the only way I know to make a reliable code optimizer is to make it as simple and as easy to understand as possible and then to test it as thoroughly as possible. OOP makes it a lot easier to make programs understandable, because it provides more ways to structure a program and because it encourages the rewriting of programs until they are of high quality.