150 likes | 291 Views
CSC 415: Translators and Compilers Spring 2009. Midterm Review. Midterm Review: Chapter 1. Context-free Grammar A finite set of terminal symbols A finite set of non-terminal symbols A start symbol A finite se to production rules Aspects of a programming language that need to be specified
E N D
CSC 415: Translators and CompilersSpring 2009 Midterm Review
Midterm Review: Chapter 1 • Context-free Grammar • A finite set of terminal symbols • A finite set of non-terminal symbols • A start symbol • A finite se to production rules • Aspects of a programming language that need to be specified • Syntax: form of programs • Contextual constraints: scope rules and type variables • Semantics: meaning of programs
Midterm Review: Chapter 1 • Language specification • Informal: written in English • Formal: precise notation (BNF, EBNF) • Unambiguous • Consistent • Complete • Context-free language • Syntax tree • Phrase • Sentence
Midterm Review: Chapter 1 • Syntax tree • Terminal node labeled by terminal symbol • Non-terminal nodes labeled b y non-terminal symbol • Abstract Syntax Tree (AST) • Each non-terminal node ius labeled by production rule • Each non-terminal node has exactly one subtree for each subprogram • Does not generate sentences
Midterm Review: Chapter 2 • Translator • Accepts any text expressed in one language (source language) and generates a semantically-equivalent text expressed in another language (target language) • Compiler • Translates from high-level language into low-level language • Interpreter • A program that accepts any program (source program) expressed in a particular language (source language) and runs that source program immediately
Midterm Review: Chapter 2 • Interpretive compiler • Combination of compiler and interpreter • Some of the advantages of each • Portable compiler • Compiled and run on any mainline, without change • Portability measured by proportion of code that remains unchanged • Portability is an economic issue • Bootstrapping • Using the language processor to process itself • Tombstone diagrams
Midterm Review: Chapter 3 • Three phases of compilation • Syntactic analysis • Contextual analysis • Code generation • Single pass compilers • Multi-pass compilers • Compiler design issues • Speed • Space • Modularity • Flexibility • Semantic preserving transformations • Source language properties
Midterm Review: Chapter 4 • Sub-phases of syntactic analysis • Scanning (lexical analysis) • Source program transformed to stream of tokens • Comments and blank spaces between tokens are discarded • Parsing • Source program in form of stream of tokens parsed to determine phrase structure • Parser treats each token as a terminal symbol • Representation of the phrase structure • A data structure representing the source program’s phrase structure • Typically an abstract syntax tree (AST)
Midterm Review: Chapter 4 • Tokens • An atomic symbol of the source program • May consist of several characters • Classified according to kind • All tokens of the same kind can be freely interchanged without affecting the program’s phrase structure • Each token completely described by it’s kind and spelling • Token represented by tuple • Only kind of each token examined by parser • Spelling examined by contextual analyzer and/or code generator
Midterm Review: Chapter 4 • Grammars • Regular expressions • “|” separates alternatives • “*” indicates that the previous item may be repeated zero or more times • “(“ and “)” are grouping parenthesis • e is the empty string • a special string of length 0 • Algebraic properties • Common extensions • Grammar transformations • Left factorization • Elimination of left recursion • Substitution of non-terminal symbols
Midterm Review: Chapter 4 • Structure of compiler • Source code • Lexical analyzer • Parser & semantic analyzer • Intermediate code generation • Optimization • Assembly code generation • Assembly code
Midterm Review: Chapter 4 • Scanning (lexical analysis) • What does it do? • Handles keywords (reserve words • Removes white space (tabs, spaces, new lines) • Removes comments • Perform look ahead • Error handling • Issues • Simpler design • Improve compiler efficiency • Enhance compiler portability
Midterm Review: Chapter 4 • Parsing • Given an unambiguous, context-free grammar • Recognition of input string – sentence in grammar • Parsing an input string – determines its phrase structure • Why is unambiguous important? • Advantages of unambiguous, context-free grammars (see chart 81) • How do you know the syntax of a language is legal? • A legal program can be derived from the start symbol of the grammar
Midterm Review: Chapter 4 • Parsing • Rightmost (replace rightmost non-terminal in each step) and leftmost (replaced leftmost non-terminal in each step) derivation • Bottom-up (reconstructs syntax tree from terminal nodes up toward the root node) and top-down (reconstructs syntax tree from the root node down towards the terminal nodes) • Predictive parsers • LL(1) • Recursive decent
Midterm Review: Chapter 4 • Parsing • Converting EBNF production rules to parsing methods • Error repair