The order we're tackling the Cool compiler is to build a lexical analyzer for the whole language, then a parser for the whole language, then a static checker for the whole language, then a code generator for the whole language. We're forced to do that because it gives us some kind of order for studying the different parts of the compiler and their underlying theory, instead of studying everything at once. But, in case it's not obvious, let me just mention that no sane person would build a real compiler this way.
How do you build a real compiler?
First you build a compiler for the teeniest tiniest subset of the language that you can possibly test ... maybe it can compile the "hello world" program and nothing else. Probably it can't compile the "hello world" program because that would involve string processing and IO ... but it might compile a procedure that can add two integers. Maybe you make it an interpreter instead of a compiler, and then you add a code generator as a second step.
And then you add some small feature, adding and fixing what you need in every phase of the compiler, from lexical analysis to code generation.
And then another feature. And so on. Add, test, revise, add, test, revise, until you have a complete compiler for the whole language. It doesn't work as a class project in a compilers course, but it's much better than what we are doing for a real compiler, whether it's for a full programming language like Java or for some itsy bitsy language you invent as an interface to some application you are building.
[Thanks Dan for prompting this thought in our after-class discussion.]
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment