Concepts of Programming Languages

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

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.
CS 355 – Programming Languages
Describing Syntax and Semantics
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
Imperative Programming
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.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Chapter 3 Part II 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,
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.
Dr. M. Al-Mulhem Denotational Semantics 1 Chapter 4 Fundamentals (Denotational Semantics)
Describing Syntax and Semantics
Describing Syntax and Semantics
Propositional Calculus: Boolean Functions and Expressions
Lecture 3 Concepts of Programming Languages
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
CSCI 3370: Principles of Programming Languages Syntax (cont.)
Chapter 3: Describing Syntax and Semantics Sangho Ha
Denotational Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
CprE 185: Intro to Problem Solving (using C)
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 (b) Semantics.
- Who must use language definitions?
Describing Syntax and Semantics
Presentation transcript:

Concepts of Programming Languages Lecturer: Dr Emad Nabil Lecture #9 Dynamic Semantic Ch3 Copyright © 2012 Pearson Education. All rights reserved.

Denotational semantic Static Semantic Dynamic Semantic Operational semantic Denotational semantic Axiomatic semantic Copyright © 2012 Pearson Education. All rights reserved.

Denotational Semantics Denotational semantics is the most rigorous (strict) and most widely known formal method for describing the meaning of programs. Based on recursive function theory The most abstract semantics description method Originally developed by Scott and Strachey (1970) Dana Scott Christopher S. Strachey

Denotational Semantics The meaning of language constructs are defined by only the values of the program's variables

Denotational Semantics * In denotational semantics, mathematical objects are used to represent the meanings of language constructs. * Language entities are converted to these mathematical objects with recursive functions are converted by Recursion Language entities mathematical objects

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

Denotational Semantics: program state The state of a program is the values of all its current variables s = {<i1, v1>, <i2, v2>, …, <in, vn>} Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(ij, s) = vj Math. Map function Var. name (lang. entity) value state Copyright © 2012 Addison-Wesley. All rights reserved.

Example: Binary Numbers syntactic objects Semantic function Mbin objects in N, nonnegative decimal numbers ex 110 The semantic function, named Mbin, maps the syntactic objects, as described in the previous grammar rules, to the objects in N, N is the set of nonnegative decimal numbers. Copyright © 2012 Pearson Education. All rights reserved.

syntax-directed semantics. Syntax Rules The semantic function Mbin syntax-directed semantics.

Example2: Decimal Numbers The syntax rules: <dec_num>  '0' |'1' |'2' |'3' |'4' |'5' |'6' | '7' | '8' | '9' | <dec_num> '0' | <dec_num> '1' | ……………. <dec_num> ‘9’ Semantic rules Mdec ('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9 Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>) Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1 … Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9 Copyright © 2012 Addison-Wesley. All rights reserved.

Expressions Suppose that the only error we consider in expressions is a variable having an undefined value. Map expressions onto Z  {error}: Z is the set in integers We assume expressions are decimal integer numbers, variables, or binary expressions having one arithmetic operator( * or + ) and two operands, each of which can be an expression

Syntax Rules for Expr. VARMAP(ij, s) = vj Mdec(<dec_num>, s)= value ∆= to define mathematical functions => to connect an operand to its associated switch (case) A=123; A=B; A=B+C; Semantic Rules for Expr. The return value is:Z ∪ error. Z is the set of int numbers Copyright © 2012 Pearson Education. All rights reserved.

s = {<i1, v1>, <i2, v2>, …, <in, vn>} * An assignment statement is an expression evaluation plus the setting of the target variable to the expression’s value * the meaning function maps a state to a state. Syntax rules <assign> X = E X A|B|C Semantic rules Returns the new state s’ The comparison here for names, not values s = {<i1, v1>, <i2, v2>, …, <in, vn>} Me(E,s)=value VARMAP(ij, s) = vj Copyright © 2012 Pearson Education. All rights reserved.

Logical Pretest Loops 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 Copyright © 2012 Addison-Wesley. All rights reserved.

Logical Pretest Loops <while> while B do L Semantic rules Syntax rules <while> while B do L we assume that there are two other existing mapping functions: The meaning of the loop is simply the value of the program variables after the statements in the loop have been executed the prescribed number of times. Msl: map statement lists and states to states (or error) Mb: map Boolean expressions to Boolean values (or error) The return value is : s ∪ error.

Do-while in java public static void main(String args[]) { } boolean b; int x=5; do x+=1; }while (b); } error: b is not initialized Copyright © 2012 Pearson Education. All rights reserved.

Do-Until Msl: map statement lists and states to states (or error) Mb: map Boolean expressions to Boolean values (or error) Maps state sets to state sets U {error} Mr(Do L until B,S) ∆= if Mb(B, s) = error then error else if Msl(L, S) = error else s’=Msl(L, s) if Mb(B, s’) = True then S’ else Mr( Do L until B, S’) The return value is : s ∪ error.

Do-While in java Msl: map statement lists and states to states (or error) Mb: map Boolean expressions to Boolean values (or error) Maps state sets to state sets U {error} Mr(Do L until B,S) ∆= if Mb(B, s) = error then error else if Msl(L, S) = error else s’=Msl(L, s) if Mb(B, s’) = False then S’ else Mr(Do L until B, S’) The return value is : s ∪ error.

Java Boolean Expression Example of B If(x==5||y==6) Mb(B, s)∆= if VARMAP(i, s)=undef for some i in B then error else B', where B' is the result of evaluating B after setting each variable i in B to VARMAP(i, s) Mb: map Boolean expressions to Boolean values (T or F) (or error) Copyright © 2012 Pearson Education. All rights reserved.

Mfor(for (<assign1> ; <boolean_expr>; <assign2>) {<stmts>}, S)  Assume the semantics mapping functions Ma, Mb, Me, are Msl given.  Mfor(for (<assign1> ; <boolean_expr>; <assign2>) L, S)  ∆=   If Ma (<assign1>, S) = error     then error     else  S’ = Ma (<assign1>, S) if Mb(<Boolean_expr>, S’) = error          then error         else  if Mb(<Boolean_expr>, S’) = false            then S’             else if Msl( L , S’) = error                then error                else  S’’ = Msl( L, S’) if Ma(<assign2>, S’’) = error                     then error                    else  S’’’ = Ma(<assign2>, S’’) MforHelp(for(<boolean_expr>; <assign2>) L , S’’’)  MforHelp(for (<boolean_expr>; <assign2>) L, S)  ∆=    if Mb(<Boolean_expr>, S) = false           then S    else  S’ = Msl( L, S) S’’ = Ma(<assign2>, S’) MforHelp(for(<boolean_expr>; <assign2>) L , S’’) For loop in Java Copyright © 2012 Pearson Education. All rights reserved.

Evaluation of Denotational Semantics provide an excellent way to describe a language concisely “briefly ”. Provides a rigorous “strict” way to think about programs Can be an aid to language design For example, statements for which the denotational semantic description is complex and difficult may indicate to the designer that such statements may also be difficult for language users to understand and that an alternative design may be in order. Has been used in compiler generation systems. (work started but didn’t completed (Jones,1980; Milos et al., 1984; Bodwin et al., 1982)). Because of its complexity, is little used by language users. Copyright © 2012 Addison-Wesley. All rights reserved.

operational semantics denotational semantics In both the meaning of a program is the values of its variables (the state) the state is changed by algorithmic code (ie. 3 address code) the state is changed by mathematical objects Copyright © 2012 Pearson Education. All rights reserved.

حكمة اليوم لا ينمو الجسد إلا بالطعام و الرياضة و لا ينمو العقل إلا بالمطالعة و التفكير.