Skip to content

Instantly share code, notes, and snippets.

@dineshappavoo
Last active August 29, 2015 14:11
Show Gist options
  • Save dineshappavoo/e43ec2d08b435a5fa91d to your computer and use it in GitHub Desktop.
Save dineshappavoo/e43ec2d08b435a5fa91d to your computer and use it in GitHub Desktop.
parse tree generater
program(pprog(K)) --> k(K),[.].
k(pblock(D,C)) --> [begin],d(D),[;],c(C),[end].
d(D) --> d_d(D).
d(pdeclaration(D1,D2)) --> d_d(D1),[;],d(D2).
d_d(pconstant(I,N)) --> [const],i(I),[=],n(N).
d_d(pvar(I)) --> [var],i(I).
c(C) --> c_c(C).
c(pcc(C1,C2)) --> c_c(C1),[;],c(C2).
c_c(passignie(I,E)) --> i(I),[:=],e(E).
c_c(pfullif(B,C1,C2)) --> [if],b(B),[then],c(C1),[else],c(C2),[endif].
c_c(pwhile(B,C)) --> [while],b(B),[do],c(C),[endwhile].
e(E) --> e_e(E).
e(passign(I,E)) --> i(I),[:=],e(E).
e(padd(E1,E2)) --> e_e(E1),[+],e(E2).
e(psubtract(E1,E2)) --> e_e(E1),[-],e(E2).
e(pmul(E1,E2)) --> e_e(E1),[*],e(E2).
e(fdiv(E1,E2)) --> e_e(E1),[/],e(E2).
e_e(pi(I)) --> i(I).
e_e(pn(N)) --> n(N).
e_e(pe(E)) --> ['('],e(E),[')'].
b(passignequal(E1,E2)) --> e(E1),[=],e(E2).
b(pnot(B)) --> [not], b(B).
b(pbbrace(B)) --> ['('], b(B),[')'].
b(ptrue(true)) --> [true].
b(pfalse(false)) --> [false].
i(x) --> [x].
i(y) --> [y].
i(z) --> [z].
i(u) --> [u].
i(v) --> [v].
i(v) --> [w].
n(0) --> [0].
n(1) --> [1].
n(2) --> [2].
n(3) --> [3].
n(4) --> [4].
n(5) --> [5].
n(6) --> [6].
n(7) --> [7].
n(8) --> [8].
n(9) --> [9].
test(P) :- program(P,[begin, const, x, =, 8, ;, var, y, ;, var, z, ;, z, :=, x,+,2, ;, if, x, =, y, +, 2, then, z , := , 5, else, z, :=, 3, endif, ;, while, not, x, =, z, do, z, :=, z, +, 2, endwhile, end, .], []).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment