Presentation is loading. Please wait.

Presentation is loading. Please wait.

9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.

Similar presentations


Presentation on theme: "9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox."— Presentation transcript:

1 9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

2 9/18/2015 2 Background for Unification Terms made from constructors and variables (for the simple first order case) Constructors may be applied to arguments (other terms) to make new terms Variables and constructors with no arguments are base cases Constructors applied to different number of arguments (arity) considered different Substitution of terms for variables

3 9/18/2015 3 Simple Implementation Background type term = Variable of string | Const of (string * term list) let rec subst var_name residue term = match term with Variable name -> if var_name = name then residue else term | Const (c, tys) -> Const (c, List.map (subst var_name residue) tys);;

4 9/18/2015 4 Unification Problem Given a set of pairs of terms (“equations”) {(s 1, t 1 ), (s 2, t 2 ), …, (s n, t n )} (the unification problem) does there exist a substitution  (the unification solution) of terms for variables such that  (s i ) =  (t i ), for all i = 1, …, n?

5 9/18/2015 5 Uses for Unification Type Inference and type checking Pattern matching as in OCAML Can use a simplified version of algorithm Logic Programming - Prolog Simple parsing

6 9/18/2015 6 Unification Algorithm Let S = {(s 1 = t 1 ), (s 2 = t 2 ), …, (s n = t n )} be a unification problem. Case S = { }: Unif(S) = Identity function (i.e., no substitution) Case S = {(s, t)}  S’: Four main steps

7 9/18/2015 7 Unification Algorithm Delete: if s = t (they are the same term) then Unif(S) = Unif(S’) Decompose: if s = f(q 1, …, q m ) and t =f(r 1, …, r m ) (same f, same m!), then Unif(S) = Unif({(q 1, r 1 ), …, (q m, r m )}  S’) Orient: if t = x is a variable, and s is not a variable, Unif(S) = Unif ({(x = s)}  S’)

8 9/18/2015 8 Unification Algorithm Eliminate: if s = x is a variable, and x does not occur in t (the occurs check), then Let  = {x  t} Let  = Unif(  (S’)) Unif(S) = {x   (t)} o  Note: {x  a} o {y  b} = {y  ({x  a}(b))} o {x  a} if y not in a

9 9/18/2015 9 Tricks for Efficient Unification Don’t return substitution, rather do it incrementally Make substitution be constant time Requires implementation of terms to use mutable structures (or possibly lazy structures) We won’t discuss these

10 9/18/2015 10 Example x,y,z variables, f,g constructors Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = ?

11 9/18/2015 11 Example x,y,z variables, f,g constructors S = {(f(x) = f(g(f(z),y))), (g(y,y) = x)} is nonempty Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = ?

12 9/18/2015 12 Example x,y,z variables, f,g constructors Pick a pair: (g(y,y) = x) Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = ?

13 9/18/2015 13 Example x,y,z variables, f,g constructors Pick a pair: (g(y,y)) = x) Orient: (x = g(y,y)) Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} by Orient

14 9/18/2015 14 Example x,y,z variables, f,g constructors Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

15 9/18/2015 15 Example x,y,z variables, f,g constructors {(f(x) = f(g(f(z),y))), (x = g(y,y))} is non- empty Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

16 9/18/2015 16 Example x,y,z variables, f,g constructors Pick a pair: (x = g(y,y)) Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

17 9/18/2015 17 Example x,y,z variables, f,g constructors Pick a pair: (x = g(y,y)) Eliminate x with substitution {x  g(y,y)} Check: x not in g(y,y). Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = ?

18 9/18/2015 18 Example x,y,z variables, f,g constructors Pick a pair: (x = g(y,y)) Eliminate x with substitution {x  g(y,y)} Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)}

19 9/18/2015 19 Example x,y,z variables, f,g constructors Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = ?

20 9/18/2015 20 Example x,y,z variables, f,g constructors {(f(g(y,y)) = f(g(f(z),y)))} is non-empty Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = ?

21 9/18/2015 21 Example x,y,z variables, f,g constructors Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = ?

22 9/18/2015 22 Example x,y,z variables, f,g constructors Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Decompose:(f(g(y,y)) = f(g(f(z),y))) becomes {(g(y,y) = g(f(z),y))} Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x  g(y,y)} = Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)}

23 9/18/2015 23 Example x,y,z variables, f,g constructors {(g(y,y) = g(f(z),y))} is non-empty Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)} = ?

24 9/18/2015 24 Example x,y,z variables, f,g constructors Pick a pair: (g(y,y) = g(f(z),y)) Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)} = ?

25 9/18/2015 25 Example x,y,z variables, f,g constructors Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Decompose: (g(y,y)) = g(f(z),y)) becomes {(y = f(z)); (y = y)} Unify {(g(y,y) = g(f(z),y))} o {x  g(y,y)} = Unify {(y = f(z)); (y = y)} o {x  g(y,y)}

26 9/18/2015 26 Example x,y,z variables, f,g constructors Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = ?

27 9/18/2015 27 Example x,y,z variables, f,g constructors {(y = f(z)); (y = y)} o {x  g(y,y) is non- empty Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = ?

28 9/18/2015 28 Example x,y,z variables, f,g constructors Pick a pair: (y = f(z)) Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = ?

29 9/18/2015 29 Example x,y,z variables, f,g constructors Pick a pair: (y = f(z)) Eliminate y with {y  f(z)} Unify {(y = f(z)); (y = y)} o {x  g(y,y)} = Unify {(f(z) = f(z))} o {y  f(z)} o {x  g(y,y)}= Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))}

30 9/18/2015 30 Example x,y,z variables, f,g constructors Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = ?

31 9/18/2015 31 Example x,y,z variables, f,g constructors {(f(z) = f(z))} is non-empty Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = ?

32 9/18/2015 32 Example x,y,z variables, f,g constructors Pick a pair: (f(z) = f(z)) Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = ?

33 9/18/2015 33 Example x,y,z variables, f,g constructors Pick a pair: (f(z) = f(z)) Delete Unify {(f(z) = f(z))} o {y  f(z); x  g(f(z), f(z))} = Unify {} o {y  f(z); x  g(f(z), f(z))}

34 9/18/2015 34 Example x,y,z variables, f,g constructors Unify {} o {y  f(z); x  g(f(z), f(z))} = ?

35 9/18/2015 35 Example x,y,z variables, f,g constructors {} is empty Unify {} = identity function Unify {} o {y  f(z); x  g(f(z), f(z))} = {y  f(z); x  g(f(z), f(z))}

36 9/18/2015 36 Example Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = {y  f(z); x  g(f(z), f(z))} f( x ) = f(g(f(z), y ))  f(g(f(z), f(z))) = f(g(f(z), f(z))). g( y, y ) = x.  g(f(z),f(z)) = g(f(z), f(z)).

37 9/18/2015 37 Example of Failure: Decompose Unify{(f(x,g(y)) = f(h(y),x))} Decompose: (f(x,g(y)) = f(h(y),x)) = Unify {(x = h(y)), (g(y) = x)} Orient: (g(y) = x) = Unify {(x = h(y)), (x = g(y))} Eliminate: (x = h(y)) Unify {(h(y), g(y))} o {x  h(y)} No rule to apply! Decompose fails!

38 9/18/2015 38 Example of Failure: Occurs Check Unify{(f(x,g(x)) = f(h(x),x))} Decompose: (f(x,g(x)) = f(h(x),x)) = Unify {(x = h(x)), (g(x) = x)} Orient: (g(y) = x) = Unify {(x = h(x)), (x = g(x))} No rules apply.

39 Major Phases of a Compiler Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Symbol Table Translate Intermediate Representation Modified from “Modern Compiler Implementation in ML”, by Andrew Appel Instruction Selection Optimized Machine-Specific Assembly Language Optimize Unoptimized Machine- Specific Assembly Language Emit code Assembler Relocatable Object Code Assembly Language Linker Machine Code Optimize Optimized IR

40 9/18/2015 40 Meta-discourse Language Syntax and Semantics Syntax - Regular Expressions, DFSAs and NDFSAs - Grammars Semantics - Natural Semantics - Transition Semantics

41 9/18/2015 41 Language Syntax Syntax is the description of which strings of symbols are meaningful expressions in a language It takes more than syntax to understand a language; need meaning (semantics) too Syntax is the entry point

42 9/18/2015 42 Syntax of English Language Pattern 1 Pattern 2

43 9/18/2015 43 Elements of Syntax Character set – previously always ASCII, now often 64 character sets Keywords – usually reserved Special constants – cannot be assigned to Identifiers – can be assigned to Operator symbols Delimiters (parenthesis, braces, brackets) Blanks (aka white space)

44 9/18/2015 44 Elements of Syntax Expressions if... then begin... ;... end else begin... ;... end Type expressions typexpr 1 -> typexpr 2 Declarations (in functional languages) let pattern 1 = expr 1 in expr Statements (in imperative languages) a = b + c Subprograms let pattern 1 = let rec inner = … in expr

45 9/18/2015 45 Elements of Syntax Modules Interfaces Classes (for object-oriented languages)

46 9/18/2015 46 Lexing and Parsing Converting strings to abstract syntax trees done in two phases Lexing: Converting string (or streams of characters) into lists (or streams) of tokens (the “words” of the language) Specification Technique: Regular Expressions Parsing: Convert a list of tokens into an abstract syntax tree Specification Technique: BNF Grammars

47 9/18/2015 47 Formal Language Descriptions Regular expressions, regular grammars, finite state automata Context-free grammars, BNF grammars, syntax diagrams Whole family more of grammars and automata – covered in automata theory

48 9/18/2015 48 Grammars Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs


Download ppt "9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox."

Similar presentations


Ads by Google