isbn 0-321-33025-0 chapter 3 describing semantics

Click here to load reader

Post on 14-Dec-2015

221 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

ISBN 0-321-33025-0 Chapter 3 Describing Semantics Slide 2 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 Slide 3 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 Slide 4 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 Slide 5 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) Slide 6 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 Slide 7 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 Slide 8 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 Slide 9 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) Slide 10 Copyright 2006 Addison-Wesley. All rights reserved.1-10 A parse tree for A = A + B Slide 11 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 Slide 12 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 Slide 13 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. Slide 14 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. Slide 15 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 Slide 16 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} Slide 17 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

View more