Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

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.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
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.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Simple Example {i = 0} j := i * i {j < 100} Can we ‘verify’ this triple? Only if we know the semantics of assignment.
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.
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
CS 355 – Programming Languages
Describing Syntax and 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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Operational Semantics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
ISBN Chapter 3 Describing Syntax and Semantics.
Semantics “Semantics” has to do with the meaning of a program. We will consider two types of semantics: –Static semantics: semantics which can be enforced.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Describing Syntax and Semantics
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
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,
Languages and Compilers
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
CCSB 314 Programming Language Department of Software Engineering College of IT Universiti Tenaga Nasional Chapter 3 Describing Syntax and Semantics.
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.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
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
Describing Syntax and Semantics
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Mathematical Structures for Computer Science Chapter 1
Lecture 5 Floyd-Hoare Style Verification
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Describing Syntax and Semantics
Chapter 3 (b) Semantics.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
COP4020 Programming Languages
Presentation transcript:

Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type specifications Dynamic semantics – briefed as semantics, meaning There is no single widely acceptable notation or formalism for describing semantics Will describe several of these methods that have been developed –Operational semantics –Axiomatic semantics –Denotational semantics 3-47

Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement To use operational semantics for a high- level language, a real or a virtual machine is needed 3-48

Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics A pure interpreter for any programming language can be constructed in software. It has problems –The detailed characteristics of the particular computer would make actions difficult to understand –Such a semantic definition would be machine- dependent 3-49

Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics (continued) A better alternative: A complete computer simulation – a low-level virtual machine The process: –Build a translator (translates source code to the machine code of an idealized computer) –Build a simulator for the idealized computer 3-50

Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics (continued) L interpreter low-level code virtual machine hardware The state changes in the virtual machine brought about by executing the code that results from translating a given statement in the high-level language defines the meaning of that statement. In effect, this approach describes the meaning of high-level language statements in terms of statements in a simpler, lower-level language. 3-51

Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics (continued) E.g. C StatementOperational Semantics for (expr; expr2; expr3) expr1; {loop: if expr2 ==0 goto out… }expr3; goto loop out: … 3-52

Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Axiomatic Semantics Used to prove the correctness of programs In a proof, each statement of a program is both preceded and followed by a logical expression that specifies constraints on program variables The logical expressions are called assertions E.g. {x>10} sum = 2*x + 1 {sum>1} 3-53

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics (continued) An assertion before a statement (a precondition) states the relationships and constraints among variables that are true at that point in execution An assertion following a statement is a postcondition A weakest precondition is the least restrictive precondition that will guarantee the postcondition. E.g. {x>10}, {x>50}, {x>100} are all valid preconditions. The weakest of all is {x>0} 3-54

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics (continued) If the weakest precondition can be computed from the given postcondition for each statement of a language, then correctness proofs can be constructed for programs in that language. The proof is begun by using the desired results of the program’s execution as the postcondition of the last statement of the program. This postcondition, along with the last statement is used to compute the weakest precondition for the last statement. This precondition is then used as the postcondition for the second last statement. This process continues until the beginning of the program is reached. At that point, the precondition of the first statement states the condition under which the program will compute the desired results. If the condition is implied by the input specification of the program, the program has been verified to be correct. 3-55

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics (continued) (precondition) – match with input? 1 st statement (postcondition/precondition) 2 nd statement (postcondition/precondition) 3 rd statement (postcondition/precondition) … last statement (postcondition/result) 3-56

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics Form Pre-, post form: {P} statement {Q} An example –a = b + 1 {a > 1} –One possible precondition: {b > 10} –Weakest precondition: {b > 0} 3-57

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics: Assignment Statements Let x = E be a general assignment statement and Q be its postcondition. Its precondition P is defined by the axiom P = Q x->E which means that P is computed as Q with all instances of x replaced by E E.g. a = b/2 – 1 {a<10} The weakest precondition is computed by b / 2 – 1 < 10 b <

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics: Assignment Statements An axiom for assignment statements {Q x->E } x = E {Q} Prove the following statement is correct: {x>5} x = x – 3 { x>0} The computed preconditon is x > 3. x > 5 implies x >

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics: Assignment Statements An inference rule called “The Rule of Consequence” General form: S1, S2, …, Sn S The form here: E.g. {x>3}x=x-3{x>0}, {x>5}=>{x>3}, {x>0}=>{x>0} {x>5}x=x-3{x>0} If S1, S2, …, and Sn are true, then the truth of S can be inferred. (=> Means “implies”) 3-60