Programming Languages and Compilers CS 421 Elsa L

  • Slides: 55
Download presentation
Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http: //courses.

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

Programming Languages & Compilers Three Main Topics of the Course I III New Programming

Programming Languages & Compilers Three Main Topics of the Course I III New Programming Paradigm Language Semantics 9/25/2020 2

Programming Languages & Compilers I II III New Programming Paradigm Language Translation Language Semantics

Programming Languages & Compilers I II III New Programming Paradigm Language Translation Language Semantics 9/25/2020 3

Programming Languages & Compilers III : Language Semantics Operational Semantics 9/25/2020 Lambda Calculus Axiomatic

Programming Languages & Compilers III : Language Semantics Operational Semantics 9/25/2020 Lambda Calculus Axiomatic Semantics 4

Programming Languages & Compilers Operational Semantics Lambda Calculus CS 422 9/25/2020 Axiomatic Semantics CS

Programming Languages & Compilers Operational Semantics Lambda Calculus CS 422 9/25/2020 Axiomatic Semantics CS 426 CS 477 5

Semantics Expresses the meaning of syntax n Static semantics n Meaning based only on

Semantics Expresses the meaning of syntax n Static semantics n Meaning based only on the form of the expression without executing it n Usually restricted to type checking / type inference n 9/25/2020 6

Dynamic semantics Method of describing meaning of executing a program n Several different types:

Dynamic semantics Method of describing meaning of executing a program n Several different types: n Operational Semantics n Axiomatic Semantics n Denotational Semantics n 9/25/2020 7

Dynamic Semantics Different languages better suited to different types of semantics n Different types

Dynamic Semantics Different languages better suited to different types of semantics n Different types of semantics serve different purposes n 9/25/2020 8

Operational Semantics n n Start with a simple notion of machine Describe how to

Operational Semantics n n Start with a simple notion of machine Describe how to execute (implement) programs of language on virtual machine, by describing how to execute each program statement (ie, following the structure of the program) Meaning of program is how its execution changes the state of the machine Useful as basis for implementations 9/25/2020 9

Axiomatic Semantics Also called Floyd-Hoare Logic n Based on formal logic (first order predicate

Axiomatic Semantics Also called Floyd-Hoare Logic n Based on formal logic (first order predicate calculus) n Axiomatic Semantics is a logical system built from axioms and inference rules n Mainly suited to simple imperative programming languages n 9/25/2020 10

Axiomatic Semantics n n n Used to formally prove a property (post-condition) of the

Axiomatic Semantics n n n Used to formally prove a property (post-condition) of the state (the values of the program variables) after the execution of program, assuming another property (pre-condition) of the state before execution Written : {Precondition} Program {Postcondition} Source of idea of loop invariant 9/25/2020 11

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 Lambda calculus often used as the range of the meaning function Meaning function is compositional: meaning of construct built from meaning of parts Useful for proving properties of programs 9/25/2020 12

Natural Semantics n n n Aka “Big Step Semantics” Provide value for a program

Natural Semantics n n n Aka “Big Step Semantics” Provide value for a program by rules and derivations, similar to type derivations Rule conclusions look like (C, m) m’ or (E, m) v 9/25/2020 13

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 9/25/2020 14

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 9/25/2020 15

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 9/25/2020 (B, m) false (not B, m) true 16

Relations (E, m) U n n (E’, m) V U ~ V = b

Relations (E, m) U n n (E’, m) V U ~ V = b (E ~ E’, m) b By U ~ V = b, we mean does (the meaning of) the relation ~ hold on the meaning of U and V May be specified by a mathematical expression/equation or rules matching U and V 9/25/2020 17

Arithmetic Expressions (E, m) U (E’, m) V U op V = N (E

Arithmetic Expressions (E, m) U (E’, m) V U op V = N (E op E’, m) N where N is the specified value for U op V 9/25/2020 18

Commands Skip: Assignment: Sequencing: 9/25/2020 (skip, m) m (E, m) V (I: : =E,

Commands Skip: Assignment: Sequencing: 9/25/2020 (skip, m) m (E, m) V (I: : =E, m) m[I <-- V ] (C, m) m’ (C’, m’) m’’ (C; C’, m) m’’ 19

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’ 9/25/2020 20

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’’ 9/25/2020 21

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}) ? 9/25/2020 22

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} 9/25/2020 23

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

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, {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} 9/25/2020 24

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} 9/25/2020 25

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} 9/25/2020 26

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} 9/25/2020 27

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

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} 9/25/2020 28

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

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} 9/25/2020 29

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

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} 9/25/2020 30

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

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} 9/25/2020 31

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

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} 9/25/2020 32

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

Example: If Then Else Rule 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} 9/25/2020 33

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

Let in Command (E, m) v (C, m[I<-v]) m’ (let I = E in C, m) m’ ’ Where m’’ (y) = m’ (y) for y I and m’’ (I) = m (I) if m(I) is defined, and m’’ (I) is undefined otherwise 9/25/2020 34

Example (x, {x->5}) 5 (3, {x->5}) 3 (x+3, {x->5}) 8 (5, {x->17}) 5 (x:

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}) ? 9/25/2020 35

Example (x, {x->5}) 5 (3, {x->5}) 3 (x+3, {x->5}) 8 (5, {x->17}) 5 (x:

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} 9/25/2020 36

Comment n n n Simple Imperative Programming Language introduces variables implicitly through assignment The

Comment 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 9/25/2020 37

Interpretation Versus Compilation n A compiler from language L 1 to language L 2

Interpretation Versus Compilation n A compiler from language L 1 to language L 2 is a program that takes an L 1 program and for each piece of code in L 1 generates a piece of code in L 2 of same meaning An interpreter of L 1 in L 2 is an L 2 program that executes the meaning of a given L 1 program Compiler would examine the body of a loop once; an interpreter would examine it every time the loop was executed 9/25/2020 38

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 the language of implementation L 2 (target language) Built incrementally n n n Start with literals Variables Primitive operations Evaluation of expressions Evaluation of commands/declarations 9/25/2020 39

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 eg 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 9/25/2020 40

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) 9/25/2020 41

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 then 9/25/2020 42

Transition Semantics n n n Form of operational semantics Describes how each program construct

Transition Semantics n n n Form of operational semantics Describes how each program construct transforms machine state by transitions Rules look like (C, m) --> (C’, m’) or (C, m) --> m’ C, C’ is code remaining to be executed m, m’ represent the state/store/memory/environment n Partial mapping from identifiers to values n Sometimes m (or C) not needed Indicates exactly one step of computation 9/25/2020 43

Expressions and Values n n C, C’ used for commands; E, E’ for expressions;

Expressions and Values n n C, C’ used for commands; E, E’ for expressions; U, V for values Special class of expressions designated as values n n Eg 2, 3 are values, but 2+3 is only an expression Memory only holds values n 9/25/2020 Other possibilities exist 44

Evaluation Semantics n n n Transitions successfully stops when E/C is a value/memory Evaluation

Evaluation Semantics n n n Transitions successfully stops when E/C is a value/memory Evaluation fails if no transition possible, but not at value/memory Value/memory is the final meaning of original expression/command (in the given state) Coarse semantics: final value / memory More fine grained: whole transition sequence 9/25/2020 45

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 9/25/2020 46

Transitions for Expressions n Numerals are values n Boolean values = {true, false} n

Transitions for Expressions n Numerals are values n Boolean values = {true, false} n Identifiers: (I, m) --> (m(I), m) 9/25/2020 47

Boolean Operations: Operators: (short-circuit) (false & B, m) --> (false, m) (B, m) -->

Boolean Operations: Operators: (short-circuit) (false & B, m) --> (false, m) (B, m) --> (B”, m) (true & B, m) --> (B, m) (B & B’, m) --> (B” & B’, m) n (true or B, m) --> (true, m) (B, m) --> (B”, m) (false or B, m) --> (B, m) (B or B’, m) --> (B” or B’, m) (not true, m) --> (false, m) (not false, m) --> (true, m) 9/25/2020 (B, m) --> (B’, m) (not B, m) --> (not B’, m) 48

Relations (E, m) --> (E’’, m) (E ~ E’, m) --> (E’’~E’, m) (E,

Relations (E, m) --> (E’’, m) (E ~ E’, m) --> (E’’~E’, m) (E, m) --> (E’, m) (V ~ E, m) --> (V~E’, m) (U ~ V, m) --> (true, m) or (false, m) depending on whether U ~ V holds or not 9/25/2020 49

Arithmetic Expressions (E, m) --> (E’’, m) (E op E’, m) --> (E’’ op

Arithmetic Expressions (E, m) --> (E’’, m) (E op E’, m) --> (E’’ op E’, m) (E, m) --> (E’, m) (V op E, m) --> (V op E’, m) (U op V, m) --> (N, m) where N is the specified value for U op V 9/25/2020 50

Commands - in English n n n skip means done evaluating When evaluating an

Commands - in English n n n skip means done evaluating When evaluating an assignment, evaluate the expression first If the expression being assigned is already a value, update the memory with the new value for the identifier When evaluating a sequence, work on the first command in the sequence first If the first command evaluates to a new memory (ie completes), evaluate remainder with new memory 9/25/2020 51

If Then Else Command - in English If the boolean guard in an if_then_else

If Then Else Command - in English If the boolean guard in an if_then_else is true, then evaluate the first branch n If it is false, evaluate the second branch n If the boolean guard is not a value, then start by evaluating it first. n 9/25/2020 53

If Then Else Command (if true then C else C’ fi, m) --> (C,

If Then Else Command (if true then C else C’ fi, m) --> (C, m) (if false then C else C’ fi, m) --> (C’, m) (B, m) --> (B’, m) (if B then C else C’ fi, m) --> (if B’ then C else C’ fi, m) 9/25/2020 54

While Command (while B do C od, m) --> (if B then C; while

While Command (while B do C od, m) --> (if B then C; while B do C od else skip fi, m) In English: Expand a While into a test of the boolean guard, with the true case being to do the body and then try the while loop again, and the false case being to stop. 9/25/2020 55