Programming Languages and Compilers CS 421 Sasa Misailovic
- Slides: 70
Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https: //courses. engr. illinois. edu/cs 421/fa 2017/CS 421 A Based on slides by Elsa Gunter, which were inspired by earlier slides by Mattox Beckman, Vikram Adve, and Gul Agha 6/18/2021 1
Major Phases of a Pico. ML Interpreter Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Environment Translate Intermediate Representation (CPS) Analyze + Transform Optimized IR (CPS) Interpreter Execution Program Run
Semantics n Expresses the meaning of syntax n Static semantics Meaning based only on the form of the expression without executing it n Usually restricted to type checking / type inference n 6/18/2021 3
Dynamic semantics n n Method of describing meaning of executing a program Several different types: n Operational Semantics n Axiomatic Semantics n Denotational Semantics Different languages better suited to different types of semantics Different types of semantics serve different purposes 4
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 6/18/2021 5
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 6/18/2021 6
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 6/18/2021 7
Axiomatic Semantics 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} 6/18/2021 8
Natural Semantics (“Big-step Semantics”) n n n Aka Structural Operational Semantics, aka “Big Step Semantics” Provide value for a program by rules and derivations, similar to type derivations Rule conclusions look like (C, m) m’ “Evaluating a command C in the state m results in the new state m’ ” or (E, m) v 9 “Evaluating an expression E in the state m results in the value
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 6/18/2021 10
Expressions n n n Identifiers: (k, m) m(k) Numerals are values: (N, m) N Booleans: (true, m) true (false , m) false 6/18/2021 11
Booleans: (B, m) false (B & B’, m) false (B, m) true (B’, m) b (B & B’, m) b (B, m) true (B or B’, m) true (B, m) true (not B, m) false 6/18/2021 (B, m) false (B’, m) b (B or B’, m) b (B, m) false (not B, m) true 12
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 6/18/2021 13
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 6/18/2021 14
Commands Skip: Assignment: Sequencing: 6/18/2021 (skip, m) m (E, m) V (k : =E, m) m [k <-- V ] (C, m) m’ (C’, m’ ) m’’ (C; C’, m) m’’ 15
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’ 6/18/2021 16
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}) ? 6/18/2021 17
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} 6/18/2021 18
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} 6/18/2021 19
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} 6/18/2021 20
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} 6/18/2021 21
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} 6/18/2021 22
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} 6/18/2021 23
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} 6/18/2021 24
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} 6/18/2021 25
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} 6/18/2021 26
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} 6/18/2021 27
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} 6/18/2021 28
While Command (B, m) false (while B do C od, m) m 1 2 3 (B, m) true (C, m) m’ (while B do C od, m’ ) m’’ (while B do C od, m) m’’
Example: While Rule 1 (x > 5, {x->7}) true 2 3 (x > 5, {x->2}) false while x > 5 do x : = x-5 od; (x : = x-5, {x->7}) {x->2} {x -> 2}) {x>2} (while x > 5 do x : = x-5 od, {x -> 7}) {x->2} 30
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’’ The rule assumes the loop terminates! 6/18/2021 31
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’’ The rule assumes the loop terminates! ? ? ? 32
Let’s Try Adding Let in Command… (E, m) v (C, m[k<-v]) m’ (let k = E in C, m) m’’ Where m’’(y) = m’(y) for y k and if m(k) is defined, m’’(k) = m(k) or otherwise m’’(k) is undefined 6/18/2021 33
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}) ? 6/18/2021 34
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} Recall: Where m’’(y) = m’(y) for y k and m’’(k) = m(k) if m(k) is defined, and m’’(k) is undefined otherwise 6/18/2021 35
Comment on Language Design 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 – a question for language designers! 6/18/2021 36
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 6/18/2021 37
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 6/18/2021 38
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 6/18/2021 39
Natural Semantics Interpreter Implementation n Identifiers: (k, m) m(k) Numerals are values: (N, m) N n Conditionals: 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) 6/18/2021 40
Natural Semantics Interpreter Implementation n Loop: 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)) n May fail to terminate - exceed stack limits n Returns no useful information then 6/18/2021 41
Transition Semantics (“Small-step 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 6/18/2021 42
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 6/18/2021 Other possibilities exist 43
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 6/18/2021 44
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 6/18/2021 45
Transitions for Expressions n Numerals are values n Boolean values = {true, false} n Identifiers: (k, m) --> (m(k), m) 6/18/2021 46
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 (mathematical) “U op V” 6/18/2021 47
Boolean Operations: Operators: (short-circuit) (false & B, m) --> (false, m) (true & B, m) --> (B, m) m) n (B, m) --> (B”, m) (B & B’, m) --> (B” & B’, (true or B, m) --> (true, m) (false or B, m) --> (B, m) B’, m) (B, m) --> (B”, m) (B or B’, m) --> (B” or (not true, m) --> (false, m) (not false, m) --> (true, m) 6/18/2021 (B, m) --> (B’, m) (not B, m) --> (not B’, 48
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 6/18/2021 49
Commands - in English n n n skip means we’re 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 (i. e. it completes), evaluate remainder with the new memory 6/18/2021 50
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 6/18/2021 52
If Then Else Command n Base Cases: (if true then C else C’ fi, m) --> (C, m) (if false then C else C’ fi, m) --> (C’, m) n Recursive Case: (B, m) --> (B’, m) (if B then C else C’ fi, m) --> (if B’ then C else C’ fi, m) 53
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 check of the boolean guard, with the true case being to execute the body and then try the while loop again, and the false case being to stop. 6/18/2021 54
Example Evaluation n First step: (if x > 5 then y: = 2 + 3 else y: =3 + 4 fi, {x -> 7}) --> ? 6/18/2021 55
Example Evaluation n First step: (x > 5, {x -> 7}) --> ? (if x > 5 then y: = 2 + 3 else y: =3 + 4 fi, {x -> 7}) --> ? 6/18/2021 56
Example Evaluation n First step: (x, {x -> 7}) --> (7, {x -> 7}) (x > 5, {x -> 7}) --> ? (if x > 5 then y: = 2 + 3 else y: =3 + 4 fi, {x -> 7}) --> ? 6/18/2021 57
Example Evaluation n First step: (x, {x -> 7}) --> (7, {x -> 7}) (x > 5, {x -> 7}) --> (7 > 5, {x -> 7}) (if x > 5 then y: = 2 + 3 else y: =3 + 4 fi, {x -> 7}) --> ? 6/18/2021 58
Example Evaluation First step: (x, {x -> 7}) --> (7, {x -> 7}) (x > 5, {x -> 7}) --> (7 > 5, {x -> 7}) (if x > 5 then y: = 2 + 3 else y: =3 + 4 fi, {x -> 7}) --> (if 7 > 5 then y: =2 + 3 else y: =3 + 4 fi, {x -> 7}) n 6/18/2021 59
Example Evaluation n n Second Step: (7 > 5, {x -> 7}) --> (true, {x -> 7}) (if 7 > 5 then y: =2 + 3 else y: =3 + 4 fi, {x -> 7}) --> (if true then y: =2 + 3 else y: =3 + 4 fi, {x -> 7}) Third Step: (if true then y: =2 + 3 else y: =3 + 4 fi, {x -> 7}) -->(y: =2+3, {x->7}) 6/18/2021 60
Example Evaluation n • Fourth Step: (2+3, {x-> 7}) --> (5, {x -> 7}) (y: =2+3, {x->7}) --> (y: =5, {x->7}) Fifth Step: (y: =5, {x->7}) --> {y -> 5, x -> 7} 6/18/2021 61
Example Evaluation n Bottom Line: (if x > 5 then y: = 2 + 3 else y: =3 + 4 fi, {x -> 7}) --> (if 7 > 5 then y: =2 + 3 else y: =3 + 4 fi, {x -> 7}) -->(if true then y: =2 + 3 else y: =3 + 4 fi, {x -> 7}) -->(y: =2+3, {x -> 7}) --> (y: =5, {x -> 7}) 6/18/2021 62
Adding Local Declarations n n Add to expressions: E : : = … | let x = E in E’ | fun x -> E | E E’ n n Recall: fun x -> E is a value Could handle local binding using state, but have assumption that evaluating expressions does not alter the environment We will use substitution here instead Notation: E [ E’ / x ] means replace all free occurrence of x by E’ in E 6/18/2021 63
Calling Conventions (Common Strategies) n n n Call by value: First evaluate the argument, then use its value Call by name: Refer to the computation by its name; evaluate every time it is called Call by need (lazy evaluation): Refer to the computation by its name, but once evaluated, store (“memoize”) the result for future reuse
Call-by-value (Eager Evaluation) (let k = V in E, m) --> (E [V/ k], m) (E, m) --> (E’’, m) (let k = E in E’, m) --> (let k = E’’ in E’) ((fun k -> E ) V, m) --> (E [V / k ], m) (E’, m) --> (E’’, m) ((fun k -> E) E’, m) --> ((fun k -> E) E’’, m) 6/18/2021 65
Call-by-name n (let k = E in E’, m) --> (E’ [E / k ], m) n ((fun k -> E’ ) E, m) --> (E’ [E / k ], m) Question: Does it make a difference? n It can depending on the language n 6/18/2021 66
Transition Semantics Evaluation n A sequence of transitions: trees of justification for each step (C 1, m 1) --> (C 2, m 2) --> (C 3, m 3) --> … --> m n Definition: let -->* be the transitive closure of --> i. e. , the smallest transitive relation containing -->
Church-Rosser Property n n n Church-Rosser Property: If E-->* E 1 and E-->* E 2, if there exists a value V such that E 1 -->* V, then E 2 -->* V Also called confluence or diamond property Example: (consider + as a function E= 2 + 3 + 4 E 1 = 5 + 4 E 2= 2 + 7 V =9 6/18/2021 68
Does Church-Rosser Property always Hold? n n n No. Languages with side-effects tend not be Church-Rosser with the combination of call-byname and call-by-value Benefit of Church-Rosser: can check equality of terms by evaluating them (but particular evaluation strategy might not always terminate!) Alonzo Church and Barkley Rosser proved in 1936 the -calculus does have it n -calculus Coming up next! 6/18/2021 69
Major Phases of a Pico. ML Interpreter Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Environment Translate Intermediate Representation (CPS) Analyze + Transform Optimized IR (CPS) Interpreter Execution Program Run
- Elsa gunter uiuc
- Cs 421 programming languages and compilers
- Sasa misailovic
- Sasa misailovic
- Pros and cons of compilers and interpreters
- Finding and understanding bugs in c compilers
- Symbol table implementation in lex and yacc
- If an error occurs what interpreter do
- Real-time systems and programming languages
- Advantages of application software
- Real time programming language
- C++ binarymove
- Cousins of compiler
- Compilers book
- 4 functions of a compiler
- Front end vs back end compiler
- Real time example of multithreading in java
- Programming languages levels
- Introduction to programming languages
- Plc programming languages
- Procedural programming languages
- Comparative programming languages
- Alternative programming languages
- Types of programming languages
- Transmission programming languages
- Cse 340 principles of programming languages
- Integral data type example
- Xenia programming languages
- Mainstream programming languages
- Vineeth kashyap
- Programming languages
- Programming languages
- Programming languages
- Programming languages
- Language
- Brief history of programming languages
- Taxonomy of programming languages
- High level languages
- If programming languages were cars
- Reasons for studying concepts of programming languages
- Cornell programming languages
- Low level programming language
- Middle level programming languages
- Programming languages flowchart
- Iat 265
- Storage management in programming languages
- Sasa gogalova
- Sasa gogalova
- Solutio ammoniae spirituosa anisata adalah
- Una frase completa
- Sifonoglife
- Berapakah yang harus ditabung dinda ke bank
- Morska sasa
- Pelagia noctiluca
- Sasa kurilensis water
- Katarina susman
- Sasa stojanovic etf
- Saša peršoh
- Sasa malkov
- Mol oddelek za kulturo
- Shinichi sasa
- Sasa toner
- Saša divjak
- Sasa atanaskovic
- Cirkus šaša tomáša
- Saša ilijić
- Saša kadivec
- Sasa sunscreen
- 421 could not create socket
- 421 rule
- Ist 421