ISBN 0-321-33025-0 Chapter 3 Describing Syntax and Semantics.

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
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Dynamic semantics Precisely specify the meanings of programs. Why? –programmers need to understand the meanings of programs they read –programmers need.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
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.
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.
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
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
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational 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
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Describing Syntax and Semantics
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Semantics In Text: Chapter 3.
Predicate Transformers
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 (b) Semantics.
Programming Languages and Compilers (CS 421)
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

ISBN Chapter 3 Describing Syntax and Semantics

Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 3 Topics – Part II Describing the Meanings of Programs: Dynamic Semantics

Copyright © 2006 Addison-Wesley. All rights reserved.3 Semantics How do we describe the “meaning” of a program? Dynamic semantics or semantics is concerned with accurately describing the execution behaviour of a language Why do we care? –English descriptions are often incomplete and ambiguous –Compiler writers must implement the language description accurately –Programmers want the same behaviour on different platforms There is no single widely acceptable notation or formalism for describing semantics Entire books have been dedicated to various semantic notations!

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 At the highest level, we’re interested in the final result (natural operational semantics) At the lowest level, look at a translated version to determine precise meaning of a single statement (structural operational semantics)

Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics Example C Statement for (expr1; expr2; expr3) {... } Operational Statements expr1; loop: if expr2 == 0 goto out... expr3; goto loop out:... Human reader is virtual computer, assumed to be able to correctly “execute” the instructions and recognize the effects. Note that language is intermediate level, not machine language.

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

Copyright © 2006 Addison-Wesley. All rights reserved. Evaluation of Operational Semantics Good if used informally (language manuals, etc.) Extremely complex if used formally (e.g., Vienna Definition Language), it was used for describing semantics of PL/I. Can lead to circularities, because statements of high-level language are described in statements of lower-level language These problems can be avoided with formalisms based on logic or mathematics

Copyright © 2006 Addison-Wesley. All rights reserved.8 Axiomatic Semantics Based on formal logic (predicate calculus) Original purpose: formal program verification Correctness proofs specify constraints on program variables When proofs can be constructed, they show that a program performs the computation described by its specification

Copyright © 2006 Addison-Wesley. All rights reserved.9 Axiomatic Semantics (continued) Logical expressions used in axiomatic semantics are called assertions. 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

Copyright © 2006 Addison-Wesley. All rights reserved.10 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} precondition postcondition

Copyright © 2006 Addison-Wesley. All rights reserved.11 Program Proof Process The postcondition for the entire program is the desired result –Work back through the program to the first statement. If the precondition on the first statement is the same as the program specification, the program is correct. An axiom is a logical statement that is assumed to be true. An inference rule is a method of inferring the truth of one assertion on the basis of the value of other assertions.

Copyright © 2006 Addison-Wesley. All rights reserved. Program Proof Process (cont) To use axiomatic semantics with a given programming language (either for correctness proofs or for formal semantic specification), must have either an axiom or an inference rule for each kind of statement in the language The following rules assume that expressions do not have side effects

Copyright © 2006 Addison-Wesley. All rights reserved.13 Axiomatic Semantics: Assignment An axiom for assignment statements (x = E): {Q x->E } x = E {Q} Example 1 –a = b/2 – 1 {a < 10} –means b/2-1 must be < 10, or b < 22 is precondition Example 2 –x = x + y – 3 { x > 10} –means x + y – 3 > 10, so y > 13 – x –OK for variable to be on both sides Q is constraint on x replace x by E in Q

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics - Consequence {x > 3} x = x – 3 { x > 0} –Using assignment axiom, x = x – 3 {x > 0} produces precondition of { x > 3 } which “proves” this statement What about {x > 5} x = x – 3 { x > 0 } ?? The Rule of Consequence:

Copyright © 2006 Addison-Wesley. All rights reserved.15 Axiomatic Semantics: Sequences S1; S2; … {P1} S1 {P2} {P2} S2 {P3}

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics: Selection if B then S1 else S2 Must be proven for both true and false conditions Example: if (x > 0) y = y-1 else y = y + 1 Assume Q is {y > 0} then P is {y > 1} else P is {y > -1} Since {y > 1} => {y > -1} use {y > 1} one Pone Q

Copyright © 2006 Addison-Wesley. All rights reserved.17 Axiomatic Semantics: Pretest Loops {P} while B do S end {Q} Number of iterations not always known. Use a loop invariant I and induction.

Copyright © 2006 Addison-Wesley. All rights reserved.18 Axiomatic Semantics: Loops Characteristics of the loop invariant: I must meet the following conditions: –P => I -- the loop invariant must be true initially –{I} B {I} -- evaluation of the Boolean must not change the validity of I –{I and B} S {I} -- I is not changed by executing the body of the loop –(I and (not B)) => Q -- if I is true and B is false, is implied –The loop terminates

Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics: Loop Example while y <> x do y = y + 1 { y = x} Run through the loop a few times to find weakest precondition 1 st : wp (y = y + 1, {y = x} } = {y + 1 = x or y = x – 1} 2 nd : wp (y = y + 1, {y = x} } = {y + 1 = x or y = x – 2} 3 rd : wp (y = y + 1, {y = x} } = {y + 1 = x or y = x – 3} So we see that {y < x} will suffice for 1 or more iterations. Combined with {y = x} for 0 iterations we have { y <= x } for loop invariant. I can also be used as the precondition. NOTE: text walks through four conditions for I assignment equality test

Copyright © 2006 Addison-Wesley. All rights reserved.20 Loop Invariant The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition Finding loop invariant can be difficult. If loop termination can be shown, axiomatic description is called total correctness. If other conditions can be met but termination is not guaranteed, called partial correctness.

Copyright © 2006 Addison-Wesley. All rights reserved.21 Evaluation of Axiomatic Semantics Developing axioms or inference rules for all of the statements in a language is difficult It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers Its usefulness in describing the meaning of a programming language is limited for language users or compiler writers

Copyright © 2006 Addison-Wesley. All rights reserved.22 Denotational Semantics Based on recursive function theory The most abstract semantics description method Originally developed by Scott and Strachey (1970)

Copyright © 2006 Addison-Wesley. All rights reserved.23 Denotational Semantics (continued) The process of building a denotational specification for a language: Define a mathematical object for each language entity –Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects The meaning of language constructs are defined by only the values of the program's variables

Copyright © 2006 Addison-Wesley. All rights reserved.24 Denotation Semantics vs Operational Semantics In operational semantics, the state changes are defined by coded algorithms In denotational semantics, the state changes are defined by rigorous mathematical functions

Copyright © 2006 Addison-Wesley. All rights reserved.25 Evaluation of Denotational Semantics Can be used to prove the correctness of programs Provides a rigorous way to think about programs Can be an aid to language design Has been used in compiler generation systems (but no useful compilers generated) Because of its complexity, they are of little use to language users