Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Advertisements

COMPILER CONSTRUCTION WEEK-2: LANGUAGE DESCRIPTION- SYNTACTIC STRUCTURE:
Semantics Static semantics Dynamic semantics attribute grammars
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.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
ISBN Chapter 7 Expressions and Assignment Statements.
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
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco.
CSC321: Programming Languages7-1 Programming Languages Tucker and Noonan Chapter 7: Semantics 7.1 Motivation 7.2 Expression Semantics 7.3 Program State.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
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 Lecture 07 Expressions and Assignment Statements.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
CS 331, Principles of Programming Languages Chapter 2.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Arithmetic Expressions
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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.
Expressions and Assignment Statements
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
ISBN Chapter 7 Expressions and Assignment Statements.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Languages and Compilers
Dr. Muhammed Al-MulhemICS (Denotational Semantics)
CS 331, Principles of Programming Languages Chapter 2.
Semantics (1).
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Expressions and Assignment Statements
Expressions and Assignment
CS2403 Programming Languages Expressions and Assignment Statements
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.
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Chapter 7 Expressions and Assignment Statements.
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Presentation transcript:

Semantics(1)

2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards developers  A programming language is complete only when its syntax, type system, and semantics are well-defined

3 Symantec(2)  Semantics is a precise definition of the meaning of a syntactically and type-wise correct program

4 Approaches to define Semantics (1)  Operational Semantics Whatever happened when the program compiled is compiled by a compiler C and run on a machine M Operational definition  Abstract Syntax  Interpreter

5 Approaches to define Semantics (2)  Axiomatic Semantics Ascertain about the relationships that remain the same each time the program executes The axiomatic formula: {P} c {Q}  c : command or control structure  P : pre-conditions  Q : post-conditions

6  Denotation Semantics Denotation definition  Abstract syntax  Semantic algebra defining a computational model  Valuation functions The valuation functions map the syntactic constructs to semantic algebra The meaning can be expressed as a collection of functions operating on a program state Approaches to define Semantics (3)

7 Expression Semantics  Expression Semantics includes Operators Operators associativity and precedence Role of different evaluation order Importance of precision

8 Infix Notation  Binary operator is written between its operands  Ambiguous expression  Associativity and Precedence is one way to eliminate the ambiguity

9 Eliminating infix Expression Ambiguity(1)  Polish prefix notations Binary operator is written in front of the two operands Semantics is inherently unambiguous Prefix can be generated by using a prefix walk (preorder traversal) Limitation: some symbol cannot be used for an operation with deferent number of operands  - is cannot be used for both unary and binary minus  One solution using different symbols Example:  the infix: a+b-c*d  the prefix: -+ab*cd

10 Eliminating infix Expression Ambiguity(2)  Polish postfix notations Binary operator follows the two operands Semantics is inherently unambiguous postfix can be generated by using a postfix walk (postorder traversal) Example:  the infix: a+b-c*d  the postfix: ab+cd*-

11 Expression Sort-Circuit Evaluation (1)  Evaluating a Boolean expression from left to right and stop as soon as the truth of the expression can be determined.  The short-circuit definition of A and B if A then B else false  The short-circuit definition of A or B if A then true else B

12 Example Node p = head; while (p != null && p.info != key) p = p.next; if (p == null) // not in list... else // found it... boolean found = false; while (p != null && ! found) { if (p.info == key) found = true; else p = p.next; } while (p !=null){ If (p.inf == key) break; P=p.next; }

13 The Meaning of an Expression  Meaning of expression should depend only on the values of its sub-expressions and the meaning of its operator  Fixed size representation of numbers in computer makes numbers have smallest and largest values Mathematics has unlimited values

14 Example:  (a+b)+c ≠ a+(b+c) for some values of a, b, c  Assume: a = largest possible integer b = 3 c =-5  Left-hand side= error (integer overflow)  Right-hand side = largest integer -2

15 Example  a is semantically undefined  a = 14 Increment i after the 2 nd referencing  a=19 Increment i before the 2 nd referencing  In many languages sub-expression in separate sub-trees may be evaluated in any order i = 2; b = 2; c=5; a = b * i++ + c * i

16 Program State(1)  The state of a program is the collection of all active objects and their current values.  Maps: The pairing of active objects with specific memory locations, and the pairing of active memory locations with their current values

17 Program State(2)  The current statement (portion of an abstract syntax tree) to be executed in a program is interpreted relative to the current state.  The individual steps that occur during a program run can be viewed as a series of state transformations

18 Example  Suppose variables i and j have the values 13 and -1 at some time during the execution  i and j are associated with memory locations 154 and 155 at that time  The current state: Environment {i,154, j,155} Memory {0,undef, … 154,13, 155,-1, …}

19 Program State(3)  The state of a program is a product of its active objects, Their memory locations, and Associative values  Function composed of two maps environment and memory  State = environment x memory  State is like watch widow

20 Assignment Semantics Assignment = Variable target; Expression source  The semantic is as follows Source expression is evaluated in the current state, resulting in a value The resulting value replaces the value of the target variable, resulting in a new state

21 Assignment Semantics(1) Assignment Statement vs. Expression  In most languages, assignment is a statement; cannot appear in an expression.  In C-like languages, assignment is an expression. Example: if (a = 0)... // an error while (*p++ = *q++) ; // strcpy while (ch = getc(fp))...

22 Assignment Semantics(2) Copy vs. Reference Semantics  Copy: a = b; a, b have same value. Changes to either have no effect on other. Used in imperative languages.  Reference a, b point to the same object. A change in object state affects both Used by many object-oriented languages.