Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 54
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 2
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 3
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 4
Chapter Twenty-Three Modern Programming Languages, 2nd ed. 5
< exp > ::= < exp > + < mulexp > | < mulexp > < mulexp > ::= < mulexp > ***** < rootexp > | < rootexp > < rootexp > ::= ( < exp > ) | < constant >
Docsity.com
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
Chapter Twenty-Three Modern Programming Languages, 2nd ed. 7
+
1
2
3 plus(const(1),times(const(2),const(3)))
Docsity.com
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. 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
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 11
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
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
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 14
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 15
Chapter Twenty-Three Modern Programming Languages, 2nd ed. 16
1 1 2 2
val1(times(X,Y),Value) :- val1(X,XValue), val1(Y,YValue), Value is XValue * YValue.
Docsity.com
Chapter Twenty-Three Modern Programming Languages, 2nd ed. 17
1 1 2 2
1 1 2 2
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. 18
Docsity.com
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 19
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 20
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
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 22
Chapter Twenty-Three Modern Programming Languages, 2nd ed. (^) Docsity.com 23
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
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