Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Formal Semantics - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

The key points are: Formal Semantics, Definitions of Syntax, Abstract Syntax Trees, Programming Language Semantics, Prolog Interpreters, Natural Semantics, Adding Functions, Adding Variables, Integer Expressions

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani 🇮🇳

4.5

(2)

86 documents

1 / 54

Toggle sidebar

Related documents


Partial preview of the text

Download Formal Semantics - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Formal Semantics

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Formal Semantics

 At the beginning of the book we saw formal

definitions of syntax with BNF

 And how to make a BNF that generates

correct parse trees: “where syntax meets

semantics”

 We saw how parse trees can be simplified

into abstract syntax trees (AST’s)

 Now… the rest of the story: formal

definitions of programming language

semantics

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Outline

 Natural semantics and Prolog interpreters

– Language One

– Language Two: adding variables

– Language Three: adding functions

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 3

Defining Language One

 A little language of integer expressions:

– Constants

– The binary infix operators + and * , with the

usual precedence and associativity

– Parentheses for grouping

 Lexical structure: tokens are + , * , ( , ) , and

integer constants consisting of one or more

decimal digits

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Syntax: Phrase Structure

 (A subset of ML expressions, Java

expressions, and Prolog terms)

 This grammar is unambiguous

 Both operators are left associative, and *

has higher precedence than +

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 5

< exp > ::= < exp > + < mulexp > | < mulexp > < mulexp > ::= < mulexp > ***** < rootexp > | < rootexp > < rootexp > ::= ( < exp > ) | < constant >

Docsity.com

Parse Trees And AST’s

 The grammar generates parse trees

 The AST is a simplified form: same order

as the parse tree, but no non-terminals

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 6

< exp >

< exp > + < mulexp >

3

< mulexp > ***** < rootexp >

1 2

< mulexp >

< rootexp > < rootexp >

+

1

2


3

Docsity.com

Continuing The Definition

 That is as far as we got in Chapters 2 and 3

 One way to define the semantics of the

language is to give an interpreter for it

 We will write one in Prolog, using AST’s as

input:

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 7

+

1

2


3 plus(const(1),times(const(2),const(3)))

Docsity.com

Abstract Syntax

 Note: the set of legal AST’s can be defined

by a grammar, giving the abstract syntax of

the language

 An abstract syntax can be ambiguous, since

the order is already fixed by parsing with

the original grammar for concrete syntax

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 8

< exp > ::= plus( < exp > , < exp > ) | times( < exp > , < exp > ) | const( < constant > )

Docsity.com

Language One: Prolog Interpreter

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 9

val1(plus(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue + YValue. val1(times(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue * YValue. val1(const(X),X).

Docsity.com

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 10

?- val1(const(1),X). X = 1.

?- val1(plus(const(1),const(2)),X). X = 3.

?- val1(plus(const(1),times(const(2),const(3))),X). X = 7.

Docsity.com

Problems

 What is the value of a constant?

– Interpreter says val1(const(X),X).

– This means that the value of a constant in

Language One is whatever the value of that

same constant is in Prolog

– Unfortunately, different implementations of

Prolog handle this differently

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 11

Value Of A Constant

 Some Prologs treat values greater than 2^31 -

as floating-point constants; others don’t

 Did we mean Language One to do this?

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 12

?- val1(const(2147483647),X). X = 2147483647.

?- val1(const(2147483648),X). X = 2.14748e+

?- val1(const(2147483647),X). X = 2147483647.

?- val1(const(2147483648),X). X = 2147483648.

Docsity.com

Value Of A Sum

 Some Prologs expresses sums greater than

2 31 -1 as floating-point results; others don’t

 Did we mean Language One to do this?

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 13

?- val(plus(const(2147483647),const(1)),X). X = 2.14748e+009.

?- val(plus(const(2147483647),const(1)),X). X = 2147483648.

Docsity.com

Defining Semantics By

Interpreter

 Our val1 is not satisfactory as a definition

of the semantics of Language One

 “Language One programs behave the way

this interpreter says they behave, running

under this implementation of Prolog on this

computer system ”

 We need something more abstract

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 14

Natural Semantics

 A formal notation we can use to capture the

same basic proof rules in val

 We are trying to define the relation between

an AST and the result of evaluating it

 We will use the symbol → for this relation,

writing E → v to mean that the AST E

evaluates to the value v

 For example, our semantics should establish

times(const(2),const(3)) → 6

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 15

A Rule In Natural Semantics

 Conditions above the line, conclusion below

 The same idea as our Prolog rule:

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 16

( 1 2 ) 12

1 1 2 2

E , E v v

E v E v

→ ×

→ →

times

val1(times(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue * YValue.

Docsity.com

Language One, Natural Semantics

 Of course, this still needs definitions for

+, × and eval , but at least it won’t

accidentally use Prolog’s

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 17

( 1 2 ) 12

1 1 2 2

E , E v v

E v E v

→ +

→ →

plus

( 1 2 ) 12

1 1 2 2

E , E v v

E v E v

→ ×

→ →

times

const ( ) n → eval ( n )

val1(plus(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue + YValue. val1(times(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue * YValue. val1(const(X),X).

Docsity.com

Natural Semantics, Note

 There may be more than one rule for a

particular kind of AST node

 For instance, for an ML-style if-then-else

we might use something like this:

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 18

E 1 → true E 2 → v 2

if ( E 1 , E 2 , E 3 ) → v 2

E 1 → false E 3 → v 3

if ( E 1 , E 2 , E 3 ) → v 3

Docsity.com

Outline

 Natural semantics and Prolog interpreters

– Language One

– Language Two: adding variables

– Language Three: adding functions

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 19

Defining Language Two

 That one was too easy!

 To make it a little harder, let’s add:

– Variables

– An ML-style let expression for defining them

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 20

Syntax

 (A subset of ML expressions)

 This grammar is unambiguous

 A sample Language Two expression:

let val y = 3 in y*y end

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 21

< exp > ::= < exp > + < mulexp > | < mulexp > < mulexp > ::= < mulexp > ***** < rootexp > | < rootexp > < rootexp > ::= let val < variable > = < exp > in < exp > end | ( < exp > ) | < variable > | < constant >

Docsity.com

Abstract Syntax

 Two more kinds of AST nodes:

– var(X) for a reference to a variable X

– let(X,Exp1,Exp2) for a let expression

that evaluates Exp2 in an environment where

the variable X is bound to the value of Exp1

 So for the Language Two program

let val y = 3 in y*y end

we have this AST:

let(y,const(3),times(var(y),var(y)))

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 22

Representing Contexts

 A representation for contexts:

– bind(Variable,Value) = the binding

from Variable to Value

– A context is a list of zero or more bind terms

 For example:

– The context in which y is bound to 3 could be

[bind(y,3)]

– The context in which both x and y are bound

to 3 could be [bind(x,3),bind(y,3)]

Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 23

Looking Up A Binding

 Looks up a binding in a context

 Finds the most recent binding for a given

variable, if more than one

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 24

lookup(Variable,[bind(Variable,Value)|],Value) :- !. lookup(VarX,[|Rest],Value) :- lookup(VarX,Rest,Value).

Docsity.com

Language Two: Prolog Interpreter

Chapter Twenty-Three Modern Programming Languages, 2nd ed. 25

val2(plus(X,Y),Context,Value) :- val2(X,Context,XValue), val2(Y,Context,YValue), Value is XValue + YValue. val2(times(X,Y),Context,Value) :- val2(X,Context,XValue), val2(Y,Context,YValue), Value is XValue * YValue. val2(const(X),_,X). val2(var(X),Context,Value) :- lookup(X,Context,Value). val2(let(X,Exp1,Exp2),Context,Value2) :- val2(Exp1,Context,Value1), val2(Exp2,[bind(X,Value1)|Context],Value2).

Docsity.com