3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

14
3.2 Semantics

Transcript of 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

Page 1: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

3.2 Semantics

Page 2: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

2Semantics

• Attribute Grammars

• The Meanings of Programs: Semantics

• Sebesta Chapter 3

Page 3: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

3Attribute Grammars

• CFG cannot describe all necessary information

• An attribute grammar is – a CFG G = (S, N, T, P) with the following

additions:• For each grammar symbol x there is a set A(x) 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 4: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

4Attribute Grammars (cont.)

• Invented by Donald Knuth

• Can carry complex syntactic and some semantic information along through parse trees– e.g. complex syntax rule (static semantics):

a variable must be defined before it is used

• Primary value of AGs:– static semantics specification– compiler design (static semantics checking)

Page 5: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

5Describing Semantics

• There is no single widely accepted notation or formalism for semantics

• Different formalisms are used:– Operational Semantics– Axiomatic Semantics– Denotational Semantics

Page 6: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

6Operational Semantics

• Describes the meaning of a program by executing its statements on a machine, either simulated or actual.

• The meaning of a statement is defined by the change in the state of the machine (memory, registers, etc.)

Page 7: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

7Operational Semantics Limitations

• A virtual machine is needed

• A hardware pure interpreter – too expensive for a high-level language

• A software pure interpreter– too difficult to understand the actions

because of the detailed characteristics of the particular computer used

– machine-dependent

Page 8: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

8Operational Semantics

• A better alternative:

A complete computer simulation

• The process:– Build a translator which 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)

Page 9: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

9Axiomatic Semantics

• Based on formal logic - predicate calculus– Original purpose: formal program verification

• Approach: – Define axioms or inference rules for each

statement type in the language (to allow transformations of expressions to other expressions)

– The expressions are called assertions

Page 10: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

10Prolog

• Prolog – logic programming language

• Axiomatic semantics

• Contains facts (assertions)

• Contains inference rules

• Inference process answers a query by generating new facts from known facts and rules

• We will learn Prolog in this course to practice the logic programming paradigm

Page 11: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

11Axiomatic Semantics Evaluation

• Good for correctness proofs and for reasoning about programs

• Too difficult to develop axioms and inference rules for all of the statements in a high-level language

• Too difficult to understand the axioms and rules• It is not very useful for

– language design– compiler/interpreter writers – programmers

Page 12: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

12Denotational Semantics

• Based on recursive function theory• The process:

– Define a mathematical object for each language entity

– Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects

– The meaning of language constructs are defined by the values of the program's variables

• The state changes are defined by mathematical functions – (operational semantics defines them in program code)

Page 13: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

13Denotational Semantics Evaluation

• The most abstract method of describing semantics

• Can be used to prove the correctness of programs

• Provides a rigorous way to think about programs

• Can aid language design

• Has been used in compiler generators

• Too complex to be useful to programmers

Page 14: 3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.

14Summary

• EBNF is an established formalism to define the syntax of PLs

• There is no single commonly used way to define the semantics – all formalisms have disadvantages