Today in CS362: Translating Assignments and Expressions Admin * Candy * CS puzzle: Self-generating programs. * Daren's talk today! Go or be a polyomino. * Upcoming Fridays: the 15th is a "pause for breath"; the 22nd is no class, the 29th is cancelled for Thanksgiving. * How are your projects coming? + Sam reflects on Emily's suggestion Overview * Where we are and where we're going (I hope) * Designing a symbol table interface * Translating assignments + Associated problems * Translating expressions + Simple: variables, constants + Compound: addition Past, Present, and Future Goal: Translate from Pascal (or a subset) to assembler (or a simulation thereof) Stream of characters (source code) Lexer Stream of tokens Parser Parse tree for syntactically correct programs Type checker [BUILDS AND USES SYMBOL TABLE; you build] Parse tree for syntactically and type correct programs Code generator [Talk about; two weeks; you build] "Intermediate Assembly Code" (infinite registers, simple ops) Register analyzer [Talk about; one week; you may build] "Intermediate Assembly Code" (fixed registers, simple ops) Optimizer [Talk about; one week] "Better Intermediate Assembly Code" Naive Translator Real Assembly Code Optimizer "Better" Real Assembly Code Assembler Machine Code Linker Executable Side question: Should the type checker change the parse tree at all? * It might add information to individual nodes (e.g., type) * It might be able to simplify the tree * It might extend the tree type r: real; i: integer; .. r := i + 2; Instead of Exp id(i) PLUS constant(2) Use Coerce-Integer-to-Real Exp id(i) PLUS constant(2) Observation: Symbol tables are used by more than one phase of the compiler (type checking, code generation), we'd like a good interface. /** * A description of what we expect symbol tables to do. * * @author CSC362.2002F */ public interface SymbolTable { /** * Given a symbol, get *all* of the information associated * with the symbol (type, memory location for variables, * frame size for functions, ...). * * @exception NoSuchElementException * if s is not in the table */ public Type lookupType(Symbol s) throws NoSuchElementException; public MemoryLocation lookupMemloc(Symbol s) throws NoSuchElementException, AttributeNotPresentException, AttributeShouldNotBePresentException /** * Enter a symbol and attribute in the table. * * If the symbol is already in the table in the current * scope, ... why would we care? A form of error checking: * a symbol shouldn't be defined twice in the same scope. */ public void enterType(Symbol s, Type type) throws AlreadySetException; public void enterMemLoc(Symbol s, MemoryLocation loc); throws AlreadySetException; /** * Start a new scope. */ public void startScope(); /** * End a scope, forgetting about all declarations in the * current scope. * * @exception Exception * If there has not been a corresponding call to startScope. */ public void endScope() throws Exception; } // interface SymbolTable