PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
Axiomatic Semantics Dr. M Al-Mulhem ICS
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Software Verification Bertrand Meyer Chair of Software Engineering Lecture 2: Axiomatic semantics.
Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Syntax Specification and BNF © Allan C. Milne Abertay University v
1 Formal Semantics of Programming Languages “Program testing can be used to show the presence of bugs, but never to show their absence!” --Dijkstra.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
CS 363 Comparative Programming Languages Semantics.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Advanced programming language theory. week 2. Attribute grammars and semantics.
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Lecture 2: Axiomatic semantics
Programming Languages 2nd edition Tucker and Noonan
Denotational Semantics (Denotational Semantics)
Semantics In Text: Chapter 3.
Predicate Transformers
Chapter 3 Describing Syntax and Semantics.
PZ03D - Program verification
PZ03D - Program verification
Program correctness Axiomatic semantics
Lecture 2: Axiomatic semantics
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010

PSUCS322 HM 2 Agenda Meta-Language Definition: Formal Semantics Denotational Semantics A Toy Language Sample Semantics Axiomatic Semantics Rules of Program Reasoning Operational Semantics

PSUCS322 HM 3 Meta-Language Meta-Language: is a language used to define (or reason about) languages. E.g. English can be an informal meta- language to reason about Java. Or BNF can be used as a meta-language to specify rules for Java Reminder of BNF: Backus-Naur Form (AKA BNF, AKA Backus-Normal Form) is a tool to specify syntax rules for a language. It is a quadruple { s, N, T, P }, with s being a start symbol, N a set of non-terminals, T a set of terminal symbols, and P a set of productions. Meta-Symbols typically are [ ], |, and ::= or → Other meta-languages can specify the meaning of a language whose syntax is understood

PSUCS322 HM 4 Definition Formal Semantics Semantics: the study of meaning of language, including programming languages. A common approach defining semantics is the informal use of “natural language”, e.g. English, in which case the natural language is the meta-language to specify some language. Or the approach to specify meaning is formal, using “concrete implementation” of a computer language. Formal semantics should define meaning precisely, completely, and concisely. Denotational Semantics: Describe meaning of a programming languages by constructing mathematical objects (called denotations) that in turn defined the meanings of expressions in the language Axiomatic Semantics: Define the meaning of a command in a program by describing its effect on assertions about the program state. The assertions are logical statements, i.e. predicates with variables, where the variables define program state. See Toni Hoare’s correctness proofs for programming languages Operational Semantics: Giving semantics in terms of the actual steps executed by some simple, precisely-defined abstract computer. See lambda calculus

PSUCS322 HM 5 Denotational Semantics Example: Define semantics for binary numbers, BNF as meta-language Grammar: B → 0 | 1 | B 0 | B 1// defines syntax Semantic Domain: N = { 0, 1, 2,...} Semantic Function: F : B = N F[0] = 0 F[1] = 1 F[B0] = 2 × F[B]// × defines multiply F[B1] = 2 × F[B] + 1// + defines addition For a concreted number 1101, we have F[1101] = 2 × F[110] + 1 = 2 × ( 2 × F[11] ) + 1 = 2 × ( 2 × ( 2 × F[1] + 1 ) ) + 1 = 2 × ( 2 × ( 2 × ) ) + 1 = 13

PSUCS322 HM 6 A Toy Language P → read x; S; write E// start symbol is P S → S; S | x := E | to E do S | (S) // non-terminals: P S E E → 0 | x | y | succ E// meta-symbol → produce A Sample Program: // English comment read x;// a string of terminals y := x;// terminals ; ( ) 0 x y to x do y := succ y;// and write read write y Below we use BNF to specify syntax, and common sense as meta-language to grasp semantics Meta-symbols are | and →

PSUCS322 HM 7 Sample: Semantics E → 0 | 1 | − E | not E | E + E | E = E | (E) | id | num | procedure S S → null | id := E | call E | S; S | begin S end | if E then S else S | while E do S P → program (id); S. Semantic Domains: B boolean values, generated by not and = I integer values; generated by E - and + V = B+I//basic values S = id →V+P//program states P = S→S // procedures

PSUCS322 HM 8 Axiomatic Semantics Observation: All properties of a program and all consequences of executing it in any environment can in principle, be derived from the source program text Idea: Reasoning about programs is based on axioms and rules of inference. Notation: {P} S {Q} “If the assertion P (precondition) is true before initiation of a program S, then the assertion Q (post-condition) will be true on its completion.” If there is no precondition, assume=postulate: {true} S {Q} If the claim can be proven in the formal system: ⊢ {P} S {Q}

PSUCS322 HM 9 Axioms of Computer Arithmetic Basic Axioms: A1: x + y = y + x// commutativity for + A2: x × y = y × x// commutativity for * A3: (x + y) + z = x + (y + z)// associativity A4: (x × y) × z = x × (y × z) A5: x × (y + z) = x × y + x × z A6: y ≤ x ⇒ (x − y) + y = x A7: x + 0 = x// null element A8: x × 0 = 0 A9: x × 1 = x Supplementary Axioms: (System Dependent!) A10 I : ¬ ∃ x ∀ y (y ≤ x) // infinite arithmetic A10 F : ∀ x (x ≤ max) // finite arithmetic A11 S : ¬ ∃ x (x = max + 1) // strict interpretation A11 B : max + 1 = max // firm boundary A11 M : max + 1 = 0 // modulo arithmetic

PSUCS322 HM 10 Sample: Axiomatic Theorem A theorem is a statement that can be derived from axioms. Sample Theorem: ⊢ y ≤ r ⇒ r + y × q = (r − y) + y × (1 + q) Proof: (r − y) + y × (1 + q) = (r − y) + (y × 1 + y × q)// A5 = (r − y) + (y + y × q)// A9 = ((r − y) + y) + y × q// A3 = r + y × q provided y ≤ r// A6 Note: An infinite number of theorems can be derived from a finite set of axioms

PSUCS322 HM 11 Rules of Program Reasoning Axiom of Assignment: D0: {P(expr)} x := expr {P(x)} The precondition P(expr) is obtained from the post-condition P(x) by substituting expr for all occurrences of x. Rule of Consequence: D1: If {P}S{Q} and Q ⇒ R then {P}S{R}. D2: If {P}S{Q} and R ⇒ P then {R}S{Q}. Rule of Composition: D3: If {P}S1{Q} and {Q}S2{R} then {P}S1; S2{R}. Rule of Iteration: D4: If {P ∧ B}S{P} then {P} while B do S{P ∧ ¬ B}. Rule of Selection: D5: If {P ∧ B}S1{Q} and {P ∧ ¬ B}S2{Q} then {P} if B then S1 else S2{Q}.

PSUCS322 HM 12 Sample: Axiomatic Reasoning Finding the quotient q and remainder r for x/y. Program: r := x; q := 0; while y ≤ r do (r := r − y; q := 1 + q) Theorem: {true} S { ¬ y ≤ r ∧ x = r + y × q} Proof: 1. true ⇒ x = x + y × 0 Lemma1 2. (x = r + y × q) ∧ y ≤ r ⇒ x = (r − y) + y × (1 + q) Lemma2 3. {x = x + y × 0} r := x {x = r + y × 0} D0 4. {x = r + y × 0} q := 0 {x = r + y × q} D0 5. {true} r := x {x = r + y × 0} D2,1,3 6. {true} r := x; q := 0 {x = r + y × q} D3,4,5 7. {x = (r − y) + y × (1 + q)} r := r − y {x = r + y × (1 + q)} D0 8. {x = r + y × (1 + q)} q := 1 + q {x = r + y × q} D0 9. {x = (r − y) + y × (1 + q)} r := r − y; q := 1 + q {x = r + y × q} D3,7,8 10. {(x = r + y × q) ∧ y ≤ r} r := r − y; q := 1 + q {x = r + y × q} D2,2,9 11. {x = r + y × q} while y ≤ r do (r := r − y; q := 1 + q) { ¬ y ≤ r ∧ x = r + y × q} D4, {true} r := x; q := 0; while y ≤ r do (r := r − y; q := 1 + q) { ¬ y ≤ r ∧ x = r + y × q} D3,6,11

PSUCS322 HM 13 Operational Semantics Operational semantics for a programming language describe how any particular valid program in the language is interpreted as a sequence of computational steps. This sequence then is the meaning of the program. Example: (Fn x => x + 2) ( ) -> (Fn x => x + 2) (5 + 5) -> (Fn x => x + 2) (10) -> > 12 Here computation is carried out by transforming (rewriting) the program text. Each -> is an atomic “step” of computation, corresponding a semantic rule precisely defined for the involved operation.