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

17
ISBN 0-321-33025-0 Chapter 3 Describing Semantics

Upload: spencer-long

Post on 14-Dec-2015

240 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

ISBN 0-321-33025-0

Chapter 3

Describing Semantics

Page 2: ISBN 0-321-33025-0 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

Page 3: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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

Page 4: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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

Page 5: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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)

Page 6: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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

Page 7: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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

Page 8: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-8

Attribute Grammars: An Example

• Syntax<assign> -> <var> = <expr>

<expr> -> <var> + <var> | <var>

<var> A | B | C

• actual_type: synthesized for <var> and <expr>

• expected_type: inherited for <expr>

Page 9: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-9

Attribute Grammar

• Syntax rule: <expr> <var>[1] + <var>[2]Semantic rules: <expr>.actual_type <var>[1].actual_type

Predicate: <var>[1].actual_type == <var>[2].actual_type

<expr>.expected_type == <expr>.actual_type

• Syntax rule: <var> id Semantic rule:

<var>.actual_type lookup (<var>.string)

Page 10: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-10

A parse tree for A = A + B

Page 11: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-11

Applying the Rules

<expr>.expected_type inherited from parent

<var>[1].actual_type lookup (A)

<var>[2].actual_type lookup (B)

<var>[1].actual_type

=? <var>[2].actual_type

<expr>.actual_type

<var>[1].actual_type

<expr>.actual_type

=? <expr>.expected_type

Page 12: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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

Page 13: ISBN 0-321-33025-0 Chapter 3 Describing 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.

Page 14: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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.

Page 15: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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

Page 16: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-16

Axiomatic Semantics Form

• Pre-, post form: {P} statement {Q}

• An examplea = b + 1 {a > 1}– One possible precondition: {b > 10}– Weakest precondition: {b > 0}

Page 17: ISBN 0-321-33025-0 Chapter 3 Describing Semantics

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