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