class: center, middle # Syntax-Directed Translation _CMPU 331 - Compilers_ --- # Recap: Stages of Compilation 1. Lexical Analysis 2. Parsing 3. Semantic Analysis 4. Optimization 5. Code Generation ![multistage pipeline](/~cs331/images/lectures/multistage.png) (source: _Language Implementation Patterns_ by Terence Parr) --- # Recap: Semantic Actions * Blocks of code run by the parser * Do something with the results of each production (parsing rule): * Construct a parse tree * Construct an AST * Directly execute the operations (interpreter) * Either semantic actions or (more commonly) separate passes over the AST: * Type checking * Other semantic analysis (identifiers declared, functions defined, etc) * Optimizations * Generate intermediate or machine code --- # Syntax-Directed Translation * When semantic actions do code generation, we call it _syntax-directed translation_ * Entire compiler is lexer, parser, and semantic actions * Don't build any internal model (AST or IR) * More difficult to implement semantic analysis, optimization, and code generation * Works for simple languages, and good first introduction to code generation