














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of programming language implementation, including the roles of programming languages, compilers, interpreters, and the objectives of compilers. It covers the concepts of lexical analysis, context-free grammar, parse trees, top-down and bottom-up parsing, and context-sensitive analysis. The document also discusses intermediate representation, optimization, data-flow analysis, and program optimizations.
Typology: Exams
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Instructing machines what to do Communicate between computers and programmers
FORTRAN, Pascal, C, C++, Java, Lisp, Scheme, ML, …
Front end
‘f’ ‘o’ ‘r’ ‘(’ `w’ ‘=’ ‘1’ ‘;’ ‘w’ ‘<’ ‘1’ ‘0’ ‘0’ ‘;’ ‘w’…
“for” “(“ “w” “=“ “1” “;” “w” “<“ “100” “;” “w”…
forStmt: “for” “(” expr1 “;” expr2 “;” expr3 “)” stmt expr1 : localVar(w) “=” integer(1) expr2 : localVar(w) “<” integer(100) expr3: localVar(w) “=” expr expr4: localVar(w) “*” integer(2) stmt: “;”
Lexical analysis/Scanning
Each token has a “type” and an optional “value”
Alphabet ∑: the set of characters that make up tokens A regular expression over ∑ could be the empty string, a symbol s ∈ ∑, or (α), αß, α | ß, or α*, where α and ß are regular expressions.
Include an alphabet ∑, a set of states S (including a start state s0 and a set of final states F), and a transition function δ DFA δ: S * ∑ S; NFA δ: S * ∑ power(S)
Describing and recognizing an input language From R.E to NFA to DFA Examples: comments, identifiers, integers, floating point numbers, ……
cs5363 7 Parse trees and abstract syntax trees
Parent: left-hand of production; children: right-hand of production
some program has multiple parse trees Rewrite an ambiguous grammar: identify source of ambiguity, restrict the applicability of some productions Standard rewrite for defining associativity and precedence of operators
Operators and keywords do not appear as leaves Chains of single productions may be collapsed Parse tree: e e e e 5
e
20
Abstract syntax tree:
Top-down and bottom-up parsing
Recursive descent parsing and LL(k) predictive parsers Transformation to grammars: eliminate left-recursion and Left- factoring Build LL(1) parsers: compute First for each production and Follow for each non-terminal
Equivalent to the reverse of a right-most derivation Right-sentential forms and their handles Shift-reduce parsing and LR(k) parsers The meaning of LR(1) items; building DFA for handle pruning; canonical LR(1) collection How to build LR(1) parse table and how to interpret LR(1) table
Context-sensitive analysis
Associate a collection of attributes with each grammar symbol Define actions to evaluate attribute values during parsing
Dependences in attribute evaluation Annotated parse tree and attribute dependence graph Bottom-up parsing and L-attribute evaluation Translation scheme: define attribute evaluation within the parsing of grammar symbols
Basic types and compound types Types of variables and expressions Type environment (symbol table) Type system, type checking and type conversion Compile-time vs. runtime type checking Type checking and type inference
Variation of IR
Mid end --- improving the code int j = 0, k; while (j < 500) { j = j + 1; k = j * 8; a[k] = 0; } int k = 0; while (k < 4000) { k = k + 8; a[k] = 0; } Original code Improved code
Data flow analysis: where data are defined and used Dependence analysis: when operations can be reordered
Redundancy elimination Improve data movement and instruction parallelization
Data-flow analysis
Forward vs. backward flow problem Define domain of analysis; build the control-flow graph Define a set of data-flow equations at each basic block Evaluate local data-flow sets at each basic block Iteratively modify result at each basic block until reaching a fixed point Traversal order of basic blocks: (reverse) postorder Example: available expression analysis, live variable analysis, reaching definition analysis, dominator analysis
Two rules that must be satisfied Insertion of ∅ functions; rewrite from SSA to normal code Computing dominance relations and dominance frontiers
Program optimizations
Simulate runtime evaluation of instruction sequence Use an integer number to unique identify each runtime value Map each expression to a value number Scope of optimization: local, EBB, dominator based
Find available expressions at the entry of each basic block Remove expressions that are redundant
Back end --- code generation
Machine code generation
A type that determines the size/meaning of its value A location to store its value (e.place)
Non-local variables or variables that might be aliased
Mixed type expressions --- implicit type conversion Arithmetic vs. boolean expressions; short-circuit translation
Register allocation and assignment
Reserve a few registers for memory access Efficiently utilize the rest of general-purpose registers
Construct global live ranges Build interference graph Coalesce live ranges to eliminate register copying Rank all live ranges based on spilling cost Color the interference graph