Section 3 with Remik (rz33@cornell.edu) Meeting #11 Notes =========================================================== Administrivia =========================================================== + CS211 Prelim #2 Grades are online =========================================================== Review =========================================================== + Keeping the Compiler Modular =========================================================== Keeping the Compiler Modular =========================================================== Our dream in Software Engineering is to make code reusable. And the only way we can do that with the large programs we write is to make them modular. That means if a part of our program is logically independant from another and PLEASE design & development that way. Under this mantra our compilers take on the commonly accepted form (see BrookShear p.255): | | | Source File | \/ -------------- | StreamIn | | | -------------- | | | Source Code | \/ -------------- | Parser | | | -------------- | | | AST | \/ - - - - - - - | Type This is completely optional Checker | - - - - - - - | | | AST' | \/ - - - - - - - | Optimizer This is completely optional too | - - - - - - - | | | AST'' | \/ -------------- | Code | | Generator | -------------- | | | Object Code | \/ -------------- | StreamOut | | | -------------- | | | Object File | \/ ---------------------------- Key Points about the diagram ---------------------------- Each phase of compilation is in fact independent of another. What binds them is a common intermediate language. For example, the Parser and Type Checker must agree on a contract - they must both use the same kind of Tree. But what about the Code Generator and the StreamOut modules, i.e. the Object Code? Since we're implementing this in Java, our Object Code can be an instance of a String - a baseline data type that all Java classes have access to. But that's all the 2 should know about one another. Because if your partner rewrites the StreamOut class, you definitely wouldn't want any of his/her bugs to affect your prestine, fully tested Code Generator, would you? If I had the following high level compiler code, and modularity was used in developing the supporting classes, then you nor I should need to recompile this function's owner. ----------------------------------------------------- public static void compile(String sourceFilename) { // 1 - open the inpur stream CS211In sourceCode = new CS211In(sourceFilename); // 2 - parse the stream into our intermediate TREE language AST ast = Parser.parse(sourceCode); // 3 - do our Bali specific translation String objectCode = CodeGenerator.codeGen(ast); // 4 - save the results to a file CS211Out.writeToFile(objectCode); } ----------------------------------------------------- And if you're really ambitious, you could even write different plug-in Code Generator modules. One could generate SAM code, another one could generate Java code, etc.