CS 363 Comparative Programming Languages 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.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
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.
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.
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
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.
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
Dr. Muhammed Al-MulhemICS (Denotational Semantics)
CCSB 314 Programming Language Department of Software Engineering College of IT Universiti Tenaga Nasional 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.
Lecture 3 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Dr. M. Al-Mulhem Denotational Semantics 1 Chapter 4 Fundamentals (Denotational Semantics)
Advanced programming language theory. week 2. Attribute grammars and semantics.
Concepts of Programming Languages
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.
Chapter 3: Describing Syntax and Semantics Sangho Ha
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.
- Who must use language definitions?
Describing Syntax and Semantics
Presentation transcript:

CS 363 Comparative Programming Languages Semantics

Semantics - the meaning of the expressions, statements, and program units

Attribute Grammars (AGs) (Knuth, 1968) CFGs cannot describe all of the syntax of programming languages –Example: correct type usage, scope, … Additions to CFGs to carry semantic info along through parse trees – tie semantics to syntax Primary value of AGs: –Static semantics specification –Compiler design (static semantics checking)

Attribute Grammars Def: An attribute grammar is a CFG G = (S, N, T, P) with the following additions: –For each grammar symbol x there is a set A(x) of attribute values –Each production has a set of functions that define certain attributes of the nonterminals in the rule

Expression Grammar SyntaxSemantics E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  identifier F.val = lookup(identifier) F  num F.val = value(num) F  ( E ) F.val = E.val

Annotating the parse tree SyntaxSemantics E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  identifier F.val = lookup(identifier) F  num F.val = value(num) F  ( E ) F.val = E.val E E + T F Num = 4 T T * F Ident = lookup(x) = 3 Num = 2 F Val = 2 Val = 3 Val = 4 Val = 2 Val = 6 Input: 2 * x + 4 Val = 6 Val = 10 Val = 4

Annotating the parse tree E E + T T * F Num = 4 T Num = 3 Num = 2 F Val = Input: * 4 F Val = SyntaxSemantics E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  identifier F.val = lookup(identifier) F  num F.val = value(num) F  ( E ) F.val = E.val

Annotating the parse tree E E + T T * F Num = 4 T Num = 3 Num = 2 F Val = 2 Val = 12 Val = 2 Val = 14 Input: * 4 F Val = 4 Val = 3 SyntaxSemantics E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  identifier F.val = lookup(identifier) F  num F.val = value(num) F  ( E ) F.val = E.val

Another Semantics SyntaxSemantics E  E 1 + T if E 1.t == INT and T.t == INT E.t = INT E  T E.t = T.t T  T 1 * F if T 1.t == INT and F.t == INT T.t = INT T  F T.t = F.t F  identifier F.t = lookuptype(identifier) F  num F.t = INT F  ( E ) F.t = E.t E E + T F Num = 4 T T * F Ident = lookup(x) = INT Num = 2 F t = Input: 2 * x + 4 t =

Another Semantics SyntaxSemantics E  E 1 + T if E 1.t == INT and T.t == INT E.t = INT E  T E.t = T.t T  T 1 * F if T 1.t == INT and F.t == INT T.t = INT T  F T.t = F.t F  identifier F.t = lookuptype(identifier) F  num F.t = INT F  ( E ) F.t = E.t E E + T F Num = 4 T T * F Ident = lookup(x) = INT Num = 2 F t = int Input: 2 * x + 4 t = int

Another Semantics SyntaxSemantics E  E 1 + T if E 1.t == INT and T.t == INT E.t = INT E  T E.t = T.t T  T 1 * F if T 1.t == INT and F.t == INT T.t = INT T  F T.t = F.t F  identifier F.t = lookuptype(identifier) F  num F.t = INT F  ( E ) F.t = E.t E E + T F Num = 4 T T * F Ident = lookup(x) = string Num = 2 F t = int t = string t = t = int t = Input: 2 * x + 4 t = ERROR!! t =

Synthesized Attributes Synthesized attributes – the value of a synthesized attribute for a node is computed using only information associated with the node’s children (or the lexical analyzer for leaf nodes). Example: ProductionSemantic Rules A  B C D A.a := B.b + C.e

Inherited Attributes Inherited attributes – if an attribute is not synthesized, it is inherited. Example: ProductionSemantic Rules A  B C D B.b := A.a + C.b

Attribute Grammars How are attribute values computed? –If all attributes are inherited, the tree could be decorated in top-down order. –If all attributes are synthesized, the tree could be decorated in bottom-up order. –In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used.

Semantics There is no single widely acceptable notation or formalism for describing semantics Operational Semantics Axiomatic Semantics Denotational Semantics

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

Operational Semantics To use operational semantics for a high-level language, a virtual machine is needed The process: –Build a translator (translates source code to the machine code of an idealized computer) –Build a simulator for the idealized computer Evaluation of operational semantics: –Good if used informally (language manuals, etc.) –Extremely complex if used formally (e.g., VDL)

Example for i = 1, 10 do body end for i = 1; loop: if (i > 10) goto endloop body i= i + 1 goto loop endloop:

Axiomatic Semantics Based on formal logic (predicate calculus) Original purpose: formal program verification Approach: Define axioms or inference rules for each statement type in the language (to allow transformations of expressions to other expressions) The expressions are called assertions

Axiomatic Semantics 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

Axiomatic Semantics Pre-post form: {P} statement {Q} An example: a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0}

Axiomatic Semantics Program proof process: The postcondition for the whole 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 spec, the program is correct. {pre} program {post}

Axiomatic Semantics Axiom for assignment statement (x = E): {Q x->E } x = E {Q} Apply this to: {???} a = b + 1 {a > 1} {a>1 a  b+1 } = {b+1 > 1} = {b > 0} {b > 0} a = b + 1 { a > 1}

Assignment Statements { ?? } c := 5 { } { } b := b + 5 { } { } b := b + 5 { a > b - 5 }

Assignment Statements { ?? } c := 5 { } { } b := b + 5 { } { a > b } b := b + 5 { a > b - 5 }

Sequence of Assignments { ?? } c := 5 { } { } b := b + 5 { a > b } { a > b } b := b + 5 { a > b - 5 }

Sequence of Assignments { ?? } c := 5 { } { a > b + 5 } b := b + 5 { a > b } { a > b } b := b + 5 { a > b - 5 }

Axiomatic Semantics An inference rule for sequences S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is:

Sequence of Assignments { ?? } c := 5 { a > b + 5 } { a > b + 5 } b := b + 5 { a > b } { a > b } b := b + 5 { a > b - 5 } Can apply sequence rule { a > b + 5 } b := b + 5; b := b + 5 { a > b - 5 }

Sequence of Assignments {} c := 5 { a > b + 5 } { a > b + 5} b := b + 5; b := b + 5 { a > b - 5 } Assignment axiom and sequence rule: {a > b + 5} c := 5; b := b + 5; b := b + 5 { a > b - 5 }

Conditional Statements { ?? } if (x > 0) y = y – 1 else y = y + 1 {y > 0} Inference Rule: {B and P} S1 {Q}, {~B and P}S2{Q} {P} if B then S1 else S2 {Q}

Conditional Statements {} if (x > 0) y = y – 1 else y = y + 1 {y > 0} Assignment axiom: {(x > 0) and (y > 1)} y = y – 1 {y > 0} {(x -1) y = y + 1 {y > 0}

Axiomatic Semantics The Rule of Consequence: Since we know: {(x -1)}y = y + 1 {y > 0}, {y > 1}  {y > -1} we know: {(x 1)}y = y + 1{y > 0}

Conditional Statements {y > 1} if (x > 0) y = y – 1 else y = y + 1 {y > 0} {(x > 0) and (y > 1)} y = y – 1 {y > 0} {(x 1) y = y + 1 {y > 0}

Conditional Semantics Other conditionals have rules as well: {B and P} S1 {Q}, {~B and P}  Q {P} if B then S1 {Q}

Loop example { P } while y <> x do y = y + 1 end {y = x} What should P be? What happens to P as the loop executes? It seems like P and ~(y<>x) should tell us something.

Axiomatic Semantics An inference rule for logical pretest loops {P} while B do S end {Q} the inference rule is: where I is the loop invariant (the inductive hypothesis)

Axiomatic Semantics Loop invariant I must meet the following conditions: –P => I (the loop invariant must be true initially) –{I} B {I} (evaluation of the loop test 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 (this can be difficult to prove)

Loop example { P } while y <> x do y = y + 1 end {y = x} use I = P = {y <= x} Need to show –P => I –{I} B {I} –{I and B} S {I} –(I and (not B)) => Q –The loop terminates

Loop example { n >= 0} count = n; fact = 1; while count <> 0 do fact = fact * count count = count – 1 end {fact = n!} I = {fact=(count+1) * …* n AND count >= 0} Need to show –P => I –{I} B {I} –{I and B} S {I} –(I and (not B)) => Q –The loop terminates

Axiomatic Semantics 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 Can be tricky – not automatically derivable from the loop

Semantics 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

Semantics Denotational Semantics –Based on recursive function theory –The most abstract semantics description method –Originally developed by Scott and Strachey (1970)

Denotational Semantics The process of building a denotational spec for a language (not necessarily easy): –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

Semantics The difference between denotational and operational semantics: In operational semantics, the state changes are defined by coded algorithms; in denotational semantics, they are defined by rigorous mathematical functions

Denotational Semantics The state of a program is the values of all its current variables s = {,, …, } Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(i j, s) = v j

Semantics Decimal Numbers –The following denotational semantics description maps decimal numbers as strings of symbols into numeric values

Semantics  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

Semantics Expressions –Map expressions onto Z  {error} –We assume expressions are decimal numbers, variables, or binary expressions having one arithmetic operator and two operands, each of which can be an expression

Semantics The meaning of the loop is the value of the program variables after the statements in the loop have been executed the prescribed number of times, assuming there have been no errors In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions Recursion, when compared to iteration, is easier to describe with mathematical rigor

Semantics 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