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)

83 documents

1 / 54

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Formal Semantics
Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

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

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

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

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 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

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 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 1 2 2

E , E v v

E v E v

plus

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

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