Programming Languages and Compilers CS 421 William Mansky

  • Slides: 37
Download presentation
Programming Languages and Compilers (CS 421) William Mansky http: //courses. engr. illinois. edu/cs 421/

Programming Languages and Compilers (CS 421) William Mansky http: //courses. engr. illinois. edu/cs 421/ Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, Elsa Gunter, and Dennis Griffith 10/30/2021 1

Compilers: Big Picture n n We want to turn strings (code) into computer -readable

Compilers: Big Picture n n We want to turn strings (code) into computer -readable instructions Done in phases Turn strings into abstract syntax trees (lex and parse) Translate abstract syntax trees into executable instructions (interpret or compile) 10/30/2021 2

Semantics Syntax gives the shape/structure of a language n Semantics assigns meaning to syntax

Semantics Syntax gives the shape/structure of a language n Semantics assigns meaning to syntax n Static semantics: what does the text of a program mean? n n n e. g. type checking/type inference Dynamic semantics: what does a program do when it executes? 10/30/2021 3

Dynamic semantics n Several different types: Operational Semantics n Axiomatic Semantics n Denotational Semantics

Dynamic semantics n Several different types: Operational Semantics n Axiomatic Semantics n Denotational Semantics n Different languages better expressed by different kinds of semantics n Different kinds of semantics serve different purposes n 10/30/2021 4

Operational Semantics n n Start with a simple notion of machine (state) Describe how

Operational Semantics n n Start with a simple notion of machine (state) Describe how to execute programs of language by describing how to execute each program statement (i. e. , following the structure of the program) Meaning of program is how its execution changes the state Useful as basis for implementations 10/30/2021 5

Axiomatic Semantics n n n Axiomatic Semantics is a logical system built from axioms

Axiomatic Semantics n n n Axiomatic Semantics is a logical system built from axioms and inference rules Common example is Floyd-Hoare Logic Assume precondition on state, prove postcondition based on statement n E. g. {x = 0} y = x + 1 {x = 0, y = 1} 10/30/2021 6

Denotational Semantics n n Construct a function M assigning a mathematical meaning to each

Denotational Semantics n n Construct a function M assigning a mathematical meaning to each program construct Meaning function is compositional: meaning of construct built from meaning of parts n One possible kind of “meaning” is lambda calculus n Useful for proving properties of programs 10/30/2021 7

Natural Semantics A. k. a. “Big-Step Semantics” n Provide value for a program by

Natural Semantics A. k. a. “Big-Step Semantics” n Provide value for a program by rules and derivations, similar to type derivations n Rule conclusions look like (C, m) m’ or ( E, m ) v where v is a value, m is a map from variables to values (“environment”) n 10/30/2021 8

Simple Imperative Programming Language n n n I Identifiers N Numerals B : :

Simple Imperative Programming Language n n n I Identifiers N Numerals B : : = true | false | B & B | B or B | not B |E<E|E=E E : : = N | I | E + E | E * E | E - E | - E C : : = skip | C; C | I : = E | if B then C else C fi | while B do C od 10/30/2021 9

Natural Semantics of Atomic Expressions Identifiers: (I, m) m(I) n Numerals are values: (N,

Natural Semantics of Atomic Expressions Identifiers: (I, m) m(I) n Numerals are values: (N, m) N n Booleans: (true, m) true (false, m) false n 10/30/2021 10

Booleans: (B, m) false (B, m) true (B’, m) b (B & B’, m)

Booleans: (B, m) false (B, m) true (B’, m) b (B & B’, m) false (B & B’, m) b (B, m) true (B or B’, m) true (B, m) false (B’, m) b (B or B’, m) b (B, m) true (not B, m) false 10/30/2021 (B, m) false (not B, m) true 11

Relations ( E, m ) v n n (E’, m) v’ v ~ v’

Relations ( E, m ) v n n (E’, m) v’ v ~ v’ = b (E ~ E’, m) b b is true if the relation ~ holds on the meaning of v and v’, false otherwise May be specified by a mathematical expression/equation or rules matching v and v’ 10/30/2021 12

Arithmetic Expressions (E, m) v (E’, m) v’ v op v’ = n (E

Arithmetic Expressions (E, m) v (E’, m) v’ v op v’ = n (E op E’, m) n where n is the specified value for v op v’ 10/30/2021 13

Commands Skip: Assignment: Sequencing: 10/30/2021 (skip, m) m (E, m) v (I : =

Commands Skip: Assignment: Sequencing: 10/30/2021 (skip, m) m (E, m) v (I : = E, m) m[I ← v ] (C, m) m’ (C’, m’) m’’ (C; C’, m) m’’ 14

If Then Else Command (B, m) true (C, m) m’ (if B then C

If Then Else Command (B, m) true (C, m) m’ (if B then C else C’ fi, m) m’ (B, m) false (C’, m) m’ (if B then C else C’ fi, m) m’ 10/30/2021 15

While Command (B, m) false (while B do C od, m) m (B, m)

While Command (B, m) false (while B do C od, m) m (B, m) true (C, m) m’ (while B do C od, m’) m” (while B do C od, m) m” 10/30/2021 16

Example: If Then Else Rule (2, {x->7}) 2 (3, {x->7}) 3 (2+3, {x->7}) 5

Example: If Then Else Rule (2, {x->7}) 2 (3, {x->7}) 3 (2+3, {x->7}) 5 (x, {x->7}) 7 (5, {x->7}) 5 (y: = 2 + 3, {x-> 7} (x > 5, {x -> 7}) true {x- >7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? 10/30/2021 17

Example: If Then Else Rule (2, {x->7}) 2 (3, {x->7}) 3 (2+3, {x->7}) 5

Example: If Then Else Rule (2, {x->7}) 2 (3, {x->7}) 3 (2+3, {x->7}) 5 (x, {x->7}) 7 (5, {x->7}) 5 (y: = 2 + 3, {x-> 7} (x > 5, {x 7}) ? {x- >7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 18

Example: Arith Relation (2, {x->7}) 2 (3, {x->7}) 3 (2+3, {x->7}) 5 (x, {x

Example: Arith Relation (2, {x->7}) 2 (3, {x->7}) 3 (2+3, {x->7}) 5 (x, {x 7}) ? (5, {x 7}) ? (y: = 2 + 3, {7} (x > 5, {x 7}) ? {x- >7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 19

Example: Identifier(s) (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true (2+3,

Example: Identifier(s) (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true (2+3, {x->7}) 5 (x, {x 7}) 7 (5, {x 7}) 5(y: = 2 + 3, {x-> 7} (x > 5, {x 7}) ? {x- >7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 20

Example: Arith Relation (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true

Example: Arith Relation (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true (2+3, {x->7}) 5 (x, {x 7}) 7 (5, {x 7}) 5(y: = 2 + 3, {x-> 7} (x > 5, {x 7}) true {x- >7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 21

Example: If Then Else Rule (2, {x->7}) 2 (3, {x->7}) 3 7 > 5

Example: If Then Else Rule (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true (2+3, {x->7}) 5 (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true ? . (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 22

Example: Assignment (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true (2

Example: Assignment (2, {x->7}) 2 (3, {x->7}) 3 7 > 5 = true (2 + 3, {x 7}) ? (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true ? {x- >7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 23

Example: Arith Op (2, {x 7}) ? (3, {x 7}) ? 7 > 5

Example: Arith Op (2, {x 7}) ? (3, {x 7}) ? 7 > 5 = true (2 + 3, {x 7}) ? (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true ? . (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 24

Example: Numerals 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 > 5

Example: Numerals 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 > 5 = true (2 + 3, {x 7}) ? (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true ? {x->7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 25

Example: Arith Op 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 >

Example: Arith Op 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 > 5 = true (2 + 3, {x 7}) 5 (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true ? {x->7, y->5} (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 26

Example: Assignment 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 > 5

Example: Assignment 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 > 5 = true (2 + 3, {x 7}) 5 (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true {x 7, y 5} } (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) ? {x->7, y->5} 10/30/2021 27

Example: If Then Else 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7

Example: If Then Else 2+3=5 (2, {x 7}) 2 (3, {x 7}) 3 7 > 5 = true (2 + 3, {x 7}) 5 (x, {x 7}) 7 (5, {x 7}) 5 (y : = 2 + 3, {x 7}) (x > 5, {x 7}) true {x 7, y 5} } (if x > 5 then y : = 2 + 3 else y : = 3 + 4 fi, {x 7}) {x 7, y 5} } {x->7, y->5} 10/30/2021 28

Let in Command (E, m) v (C, m[I ← v]) m’ (let I =

Let in Command (E, m) v (C, m[I ← v]) m’ (let I = E in C, m) m’ [I ← m(I)] n n n First evaluate value for I Then evaluate C given that value for I Finally, restore old value for I 10/30/2021 29

Example (x, {x 5}) 5 (3, {x 5}) 3 (x+3, {x 5}) 8 (5,

Example (x, {x 5}) 5 (3, {x 5}) 3 (x+3, {x 5}) 8 (5, {x 17}) 5 (x: =x+3, {x 5}) {x 8} (let x = 5 in (x : = x+3), {x 17}) ? 10/30/2021 30

Example (x, {x 5}) 5 (3, {x 5}) 3 (x+3, {x 5}) 8 (5,

Example (x, {x 5}) 5 (3, {x 5}) 3 (x+3, {x 5}) 8 (5, {x 17}) 5 (x: =x+3, {x 5}) {x 8} (let x = 5 in (x : = x+3), {x 17}) {x 17} 10/30/2021 31

Notes on Let n n n Simple Imperative Programming Language introduces variables implicitly through

Notes on Let n n n Simple Imperative Programming Language introduces variables implicitly through assignment The let-in command introduces scoped variables explictly Clash of constructs apparent in awkward semantics 10/30/2021 32

Running Code n n A compiler for language L 1 takes an L 1

Running Code n n A compiler for language L 1 takes an L 1 program and generates code in a target language L 2 (e. g. , assembly) with same meaning An interpreter of L 1 executes the meaning of a given L 1 program directly Compiling tends to be more efficient Compiler examines the body of a loop once; interpreter examines it every time the loop is executed 10/30/2021 33

Interpreter n n An interpreter represents the operational semantics of a language L 1

Interpreter n n An interpreter represents the operational semantics of a language L 1 (source language) in some implementation language L 2 (target language) Built incrementally n n n Start with literals Variables Primitive operations Evaluation of expressions Evaluation of commands/declarations 10/30/2021 34

Interpreter n Takes abstract syntax trees as input n n One procedure for each

Interpreter n Takes abstract syntax trees as input n n One procedure for each syntactic category (nonterminal) n n n In simple cases could be just strings E. g. one for expressions, another for commands If natural semantics used, tells how to compute final value from code If transition semantics used, tells how to compute next “state” n To get final value, put in a loop 10/30/2021 35

Natural Semantics Example n n compute_exp (Var(v), m) = look_up v m compute_exp (Int(n),

Natural Semantics Example n n compute_exp (Var(v), m) = look_up v m compute_exp (Int(n), _) = Num (n) … compute_com(If. Exp(b, c 1, c 2), m) = if compute_exp (b, m) = Bool(true) then compute_com (c 1, m) else compute_com (c 2, m) 10/30/2021 36

Natural Semantics Example n n n compute_com(While(b, c), m) = if compute_exp (b, m)

Natural Semantics Example n n n compute_com(While(b, c), m) = if compute_exp (b, m) = Bool(false) then m else compute_com (While(b, c), compute_com(c, m)) May fail to terminate/exceed stack limits Returns no useful information on failure 10/30/2021 37