Semantics (1).

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:
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.
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.
Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.
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.
Expressions, Evaluation and Assignments
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.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
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.
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
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
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
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.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Languages and Compilers
CS 331, Principles of Programming Languages Chapter 2.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics.
Dr. M. Al-Mulhem Denotational Semantics 1 Chapter 4 Fundamentals (Denotational Semantics)
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.
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)

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

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

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 Axiomatic Semantics Axiomatize the meaning of each statement of the language Give the formal specification of what a program is supposed to do.

Approaches to define Semantics (2) Denotation Semantics Define the meaning of each type of statement that occurs in the abstract syntax as a state-transformation mathematical function. The meaning can be expressed as a collection of functions operating on a program state

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

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

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

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*-

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

Expression Sort-Circuit Evaluation (2) Advantages: shorter and clear code Disadvantages: A && B is not the same as B && A A  false and B  undefined A && B  false B && A  undefined

Example while (p !=null){ If (p.inf == key) break; P=p.next; } 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; }

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

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

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

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

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

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, …}

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

Assignment Semantics The semantic is as follows 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

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)) ...

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.