Last active
August 29, 2015 14:11
-
-
Save dineshappavoo/d0389aef0ac5f828b32e to your computer and use it in GitHub Desktop.
Denotational semantic for the parse tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newstore([]). | |
access(_,[],0). | |
access(I,[(I,V)|_],V). | |
access(I,[_|T],V) :- access(I,T,V). | |
update(I,NV,[],[(I,NV)]). | |
update(I,NV,[(I,_)|T],[(I,NV)|T]). | |
update(I,NV,[P|T],[P|T1]) :- update(I,NV,T,T1). | |
psem(pprog(K), A, B, D) :- newstore(S), | |
update(x, A, S, S1), | |
update(y, B, S1, S2), | |
ksem(K, S2, S3), | |
access(z, S3, D). | |
ksem(pblock(D,C),Sin,Sout) :- dsem(D,Sin,Sin1), | |
csem(C,Sin1,Sout). | |
dsem(pdeclaration(D1,D2),Sin,Sout):- dsem(D1,Sin,Sin1), | |
dsem(D2,Sin1,Sout). | |
dsem(pconstant(I,N),Sin,Sout):- update(I,N,Sin,Sout). | |
dsem(pvar(_),Sin,Sin). | |
csem(pcc(C1,C2),Sin,Sout):- csem(C1,Sin,Sin1), | |
csem(C2,Sin1,Sout). | |
csem(passignie(I,E),Sin,Sout):- esem(E,Sin,Sin1,Val), | |
update(I,Val,Sin1,Sout). | |
csem(pfullif(B,C1,C2),Sin,Sout):- bsem(B,Sin,Sin1,RV),(RV -> csem(C1,Sin1,Sout) ; csem(C2,Sin1,Sout)). | |
csem(pwhile(B,C),Sin,Sout):- bsem(B,Sin,Sin1,RV),(RV -> csem(C,Sin1,Sin2),csem(pwhile(B,C),Sin2,Sout) ; Sout=Sin1). | |
esem(pi(I),Sin,Sin,Val):- access(I,Sin,Val). | |
esem(pn(N),Sin,Sin,N). | |
esem(pe(E),Sin,Sout,Value):- esem(E,Sin,Sout,Value). | |
esem(passign(I,E),Sin,Sout,Value):- esem(E,Sin,Sin1,Value), | |
update(I,Value,Sin1,Sout). | |
esem(padd(E1,E2),Sin,Sout,Value):- esem(E1,Sin,Sin1,Value1), | |
esem(E2,Sin1,Sout,Value2), | |
Value is Value1 + Value2. | |
esem(psubtract(E1,E2),Sin,Sout,Value):- esem(E1,Sin,Sin1,Value1), | |
esem(E2,Sin1,Sout,Value2), | |
Value is Value1 - Value2. | |
esem(pmul(E1,E2),Sin,Sout,Value):- esem(E1,Sin,Sin1,Value1), | |
esem(E2,Sin1,Sout,Value2), | |
Value is Value1 * Value2. | |
esem(pdiv(E1,E2),Sin,Sout,Value):- esem(E1,Sin,Sin1,Value1), | |
esem(E2,Sin1,Sout,Value2), | |
Value is Value1 / Value2. | |
bsem(passignequal(E1,E2),Sin,Sout,RV):- esem(E1,Sin,Sin1,Value1), | |
esem(E2,Sin1,Sout,Value2), | |
(Value1=Value2 -> RV = true ; RV = false). | |
bsem(pnot(B),Sin,Sout,RV):- bsem(B,Sin,Sout,RV1), | |
(RV1 -> RV = false ; RV = true). | |
bsem(pbbrace(B),Sin,Sout,RV):- bsem(B,Sin,Sout,RV). | |
bsem(ptrue(true),_,_,true). | |
bsem(pfalse(false),_,_,false). | |
main(ValX, ValY, A) :- program(P, [begin, var, x, ;, var, y, ;, var,z, ;, var, w, ;, z, :=, 1, ;, w, :=, x, ;, while, w, >, 0, do, z, :=,z, *, y, ;, w, :=, w, -, 1, endwhile, end, .],[]), write(P), | |
psem(P,ValX,ValY,A). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment