ISBN 0-321-33025-0 Chapter 3 Describing 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
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Operational Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
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.
ISBN Chapter 3 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.
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.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
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
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:

ISBN Chapter 3 Describing Semantics

Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units Pragmatics: the usage of the language

Copyright © 2006 Addison-Wesley. All rights reserved.1-3 How do we describe semantics? In words - as in a manual –hard to write a manual that everyone understands the same way Use a translator - do an experiment and see what happens –What if the translator isn't correct? isn't available? Use a formal notation

Copyright © 2006 Addison-Wesley. All rights reserved.1-4 Static Semantics Context-free grammars (CFGs) cannot describe all of the syntax of programming languages For example –the requirement that a variable be declared before it can be used is impossible to express in a grammar –information about variable and expression types could be included in a grammar but only at the cost of great complexity

Copyright © 2006 Addison-Wesley. All rights reserved.1-5 Attribute Grammars Attribute grammars allow some semantic information to be added to a parse tree Primary value of attribute grammars –Static semantics specification –Compiler design (static semantics checking)

Copyright © 2006 Addison-Wesley. All rights reserved.1-6 Attribute Grammars : Definition An attribute grammar is a context-free grammar with the following additions: –For each grammar symbol there is a set 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

Copyright © 2006 Addison-Wesley. All rights reserved.1-7 Attribute Grammars: Definition Initially, there are intrinsic attributes on the leaves Synthesized attributes are computed from the attributes of the children of the node. Inherited attributes are computed from the attributes of the parent node

Copyright © 2006 Addison-Wesley. All rights reserved.1-8 Attribute Grammars: An Example Syntax -> = -> + | A | B | C actual_type : synthesized for and expected_type : inherited for

Copyright © 2006 Addison-Wesley. All rights reserved.1-9 Attribute Grammar Syntax rule:  [1] + [2] Semantic rules:.actual_type  [1].actual_type Predicate: [1].actual_type == [2].actual_type.expected_type ==.actual_type Syntax rule:  id Semantic rule:.actual_type  lookup (.string)

Copyright © 2006 Addison-Wesley. All rights reserved.1-10 A parse tree for A = A + B

Copyright © 2006 Addison-Wesley. All rights reserved.1-11 Applying the Rules.expected_type  inherited from parent [1].actual_type  lookup (A) [2].actual_type  lookup (B) [1].actual_type =? [2].actual_type.actual_type  [1].actual_type.actual_type =?.expected_type

Copyright © 2006 Addison-Wesley. All rights reserved.1-12 Semantics Semantics deals with the meaning of a program and its parts There is no single widely acceptable notation or formalism for describing semantics Several approaches –Operational Semantics –Axiomatic Semantics –Denotational Semantics

Copyright © 2006 Addison-Wesley. All rights reserved.1-13 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 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), it was used for describing semantics of PL/I.

Copyright © 2006 Addison-Wesley. All rights reserved.1-14 Axiomatic Semantics Based on formal logic (predicate calculus) –Axioms or inference rules are defined for each statement type in the language (to allow transformations of expressions to other expressions) - This is hard Original purpose: formal program verification –It is a good tool for correctness proofs, and an excellent framework for reasoning about programs. Its usefulness in describing the meaning of a programming language is limited.

Copyright © 2006 Addison-Wesley. All rights reserved.1-15 Assertions 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 © 2006 Addison-Wesley. All rights reserved.1-16 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}

Copyright © 2006 Addison-Wesley. All rights reserved.1-17 Denotational Semantics Based on recursive function theory The most abstract semantics description method Evaluation –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, they are of little use to language users