Chapter 3 Part II 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.
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.
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.
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.
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.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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.
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.
Semantics In Text: Chapter 3.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
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.
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.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
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
Describing Syntax and Semantics
Describing Syntax and Semantics
Semantics In Text: Chapter 3.
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 (b) Semantics.
Presentation transcript:

Chapter 3 Part II Describing Syntax and Semantics

Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax

Copyright © 2012 Addison-Wesley. All rights reserved.1-3 Static Semantics Context-free grammars (CFGs) cannot be used to fully describe all legal forms for a programming language. –In cases where it is inefficient –In cases where it is impossible

Copyright © 2012 Addison-Wesley. All rights reserved.1-4 Attribute Grammars Attribute grammars are –a formal approach to describing and checking the correctness of the static semantics rules of a programming language. –context-free grammars to which have been added attributes, computation functions, and predicate functions.

Copyright © 2012 Addison-Wesley. All rights reserved.1-5 Attribute Grammars : Definition An attribute grammar is a context-free grammar with the following additions: –For each grammar symbol x there is a set A(x) of attribute values –Each rule has a set of functions that define certain attributes of the nonterminals in the rule –Each rule has a (possibly empty) set of predicates to check for attribute consistency. More formally, –Let X 0  X 1... X n be a rule –Functions of the form S(X 0 ) = f(A(X 1 ),..., A(X n )) define synthesized attributes –Functions of the form I(X j ) = f(A(X 0 ),..., A(X n )), for i <= j <= n, define inherited attributes –Initially, there are intrinsic attributes on the leaves

Copyright © 2012 Addison-Wesley. All rights reserved.1-6 Attribute Grammars: Definition Inherited attributes pass semantic information down and across the tree. Synthesized attributes pass semantic information up a parse tree. Initially, there are intrinsic attributes on the leaves whose values are determined outside the parse tree.

Copyright © 2012 Addison-Wesley. All rights reserved.1-7 Attribute Grammars Example 1: An attributed grammar for the syntax and static semantics for the Ada rule which states that the name on the end of an Ada procedure must match the procedure’s name. Syntax Rule: -> procedure [1] end [2]; Predicate: [1].string == [2].string

Copyright © 2012 Addison-Wesley. All rights reserved.1-8 Attribute Grammars Example 2: An attributed grammar for the syntax and static semantics of an assignment statement. The syntax of the grammar is shown by the grammar: -> = -> + | -> A | B | C The only variable names are A, B, and C. The right side of the assignment can be either a variable or an expression in the form of a variable added to another variable. The static semantics of the grammar are: The variables can be of two types: int or real. When there are two variables on the right side of an assignment, they need not be the same type The type of the expression when the two types are not the same is real. When they are the same the type of the expression is the type of the two variables. The type of the lhs must match the type of the rhs.

Copyright © 2007 Addison-Wesley. All rights reserved.1-9

Copyright © 2007 Addison-Wesley. All rights reserved.1-10

Copyright © 2007 Addison-Wesley. All rights reserved.1-11 Decorating the parse tree - computing the values of the attributes 1..actual_type  lookup (A)(R4) 2..expected_type  <var.actual_type (R1) 3. [2].actual_type  lookup (A) (R4) 4. [3].actual_type  lookup (B) (R4) 5..actual_type  either int or real (R2) 6..expected_type ==.actual_type 7. True or False (R2) Attribute Grammars : continued

Copyright © 2007 Addison-Wesley. All rights reserved.1-12 Attribute Grammars : continued Fully attributed parse tree where A is type real and B is type int Predicate:.expected_type ==.actual_type TRUE

Copyright © 2012 Addison-Wesley. All rights reserved.1-13 Attribute Grammars Adding the attributes, their functions and predicates to the grammar of to describe the static semantics: -> = -> + | -> A | B | C Attributes for nonterminals actual_type : a synthesized attribute for and used to store the type of int or real. – - intrinsically determined – - determined from the actual types of the child(ren) expected_type : an inherited attribute for used to store the expected type of int or real as determined by the type of the variable on the LHS of the assignment statement

Attribute Grammars : Evaluated Every compiler must be written to check the static semantic rules of a programming language whether formally done or not. Copyright © 2012 Pearson Education. All rights reserved.1-14

Copyright © 2012 Addison-Wesley. All rights reserved.1-15 Dynamic Semantics There is no single widely acceptable notation or formalism for describing dynamic semantics. Three example methods for describing dynamic semantics. –Operational –Denotational –Axiomatic How can these be useful? –By programmers –By compiler writers –By programming language designers –When correctness proofs are wanted –For compiler generators

Operational Semantics –Describe the meaning of a program by executing its statements on a machine, either simulated or actual. Uses of operational semantics : - Language manuals and textbooks - Teaching programming languages Two levels of uses: Natural & Structural Evaluation - Good if used informally - Can be extremely complex if used formally Copyright © 2012 Addison-Wesley. All rights reserved.1-16

Operational Semantics Examples of the form of a few statements –Ident = var bin_op var –Ident = un_op var Example of usage for the description of the semantics of the C for statement: for (expre1; expre2; expre3) { … } Meaning expr1; Loop: if expre2 == 0 goto Out … expre3; goto Loop Out: Copyright © 2012 Pearson Education. All rights reserved.1-17

Denotational Semantics 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 © 2012 Addison-Wesley. All rights reserved.1-18  '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') M dec ('0') = 0, M dec ('1') = 1, …, M dec ('9') = 9 M dec ( '0') = 10 * M dec ( ) M dec ( '1’) = 10 * M dec ( ) + 1 … M dec ( '9') = 10 * M dec ( ) + 9

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 Because of its complexity, it are of little use to language users Copyright © 2012 Addison-Wesley. All rights reserved.1-19

Copyright © 2012 Addison-Wesley. All rights reserved.1-20 Axiomatic Semantics Based on formal logic (predicate calculus) Original purpose: formal program verification Axioms or inference rules are defined for each statement type in the language (to allow transformations of logic expressions into more formal logic expressions) The logic expressions are called assertions

Copyright © 2012 Addison-Wesley. All rights reserved.1-21 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

Copyright © 2012 Addison-Wesley. All rights reserved.1-22 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} An example –sum = 2 * x + 1 {sum > 1} –One possible precondition: {x > 10} –Weakest precondition: {x > 0}

Copyright © 2012 Addison-Wesley. All rights reserved.1-23 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. {P1} statement1 {Q1} {P2} statement2 {Q2} {P3} statement3 {Q3}

Copyright © 2012 Addison-Wesley. All rights reserved.1-24 Axiomatic Semantics: Assignment Typical notation for the axiomatic semantics of a given statement form is: {P} S {Q} An axiom for assignment statements: (x = E): {Q x->E } x = E {Q} Example: a = b / 2 – 1 { a < 10 } a < 10, where a = b / b / 2 – 1 < 10 b / 2 < 11 b < 22 P is {b < 22} Example: x = 2 * y – 3 {x > 25} 2 * y – 3 > 25 2 * y > 28 y > 14 P is {y > 14}

Copyright © 2012 Addison-Wesley. All rights reserved.1-25 Axiomatic Semantics: Assignment Example 3: x = x + y -3 {x > 10} Calculate P x + y – 3 > 10 x + y > 13 y > 13 – x P {y > 13 – x}

Copyright © 2012 Addison-Wesley. All rights reserved.1-26 Axiomatic Semantics: Sequences An inference rule for sequences of the form S1; S2 {P1} S1 {P2} {P2} S2 {P3}

Copyright © 2012 Addison-Wesley. All rights reserved.1-27 Axiomatic Semantics Sequences Example: y = 3 * x + 1; x = y + 3; { x < 10 } Start with: {p} x = y + 3; { x < 10 } To find P: y + 3 < 10 y < 7 Result: {y < 7} x = y + 3; { x < 10 } Final Result is { X < 2} y = 3 * x + 1; {y < 7} x = y + 3; { x < 10 } Next find P for first statement: {P} y = 3 * x + 1; {y < 7} To find P: y = 3 * x + 1; 3 * x + 1 < 7 3 * x < 6 x < 2 Result: {x < 2} y = 3 * x + 1; { y < 7}

Axiomatic Semantics: Selection An inference rules for selection - if B then S1 else S2 {B and P} S1 {Q}, {(not B) and P} S2 {Q} {P} if B then S1 else S2 {Q} Copyright © 2012 Addison-Wesley. All rights reserved.1-28

Axiomatic Semantics: Selection {B and P} S1 {Q}, {(not B) and P} S2 {Q} {P} if B then S1 else S2 {Q} Example: if x > 0 then y = y -1 else y = y + 1 {y > 0 } Apply to then clause: y – 1 > 0 yields P = {y>1} Apply to else clause: y + 1 > 0 yields P = {y>-1} Because y>1 => y>-1, we use {y > 1} as the precondition for the entire statement. Copyright © 2012 Addison-Wesley. All rights reserved.1-29

Copyright © 2012 Addison-Wesley. All rights reserved.1-30 Axiomatic Semantics: Loops An inference rule for logical pretest loops {P} while B do S end {Q} where I is the loop invariant (the inductive hypothesis)

Copyright © 2012 Addison-Wesley. All rights reserved.1-31 Axiomatic Semantics: Axioms 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, Q is implied –The loop terminates -- can be difficult to prove

Copyright © 2012 Addison-Wesley. All rights reserved.1-32 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 © 2012 Addison-Wesley. All rights reserved.1-33 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 © 2012 Addison-Wesley. All rights reserved.1-34 Summary BNF and context-free grammars are equivalent meta-languages –Well-suited for describing the syntax of programming languages An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language Three primary methods of semantics description –Operation, axiomatic, denotational

Copyright © 2012 Addison-Wesley. All rights reserved.1-35 Summary BNF and context-free grammars are equivalent meta-languages –Well-suited for describing the syntax of programming languages An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language Three primary methods of semantics description –Operation, axiomatic, denotational