


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



This code appears in the class directory. It is provided here with color highlighting to aid the identification of the changes. The changed portions of the code appear in red. % The following is BNF to describe a simple collection of expressions. % Note that for simplicity in this BNF, no constants or whitespace are % provided for, and only the letters 'a' through 'z' can be used in % identifiers. A production label denotes the kind of expr produced by % using it, and these labels appear as node names in derivation trees. % To have lowest precedence, the conditional will appear at the top of % derivation trees. A new start symbol
parse_factor(Xs, ide(T)) :- parse_ident(Xs, T). parse_factor(Xs, prn(T)) :- name('(', [N1]), name(')', N2), append([N1|Ys], N2, Xs), parse_cond(Ys, T). parse_ident([X], let(T)) :- parse_letter([X], T). parse_ident(Xs, idL(T1,T2)) :- append(Ys, [X], Xs), parse_ident(Ys, T1), parse_letter([X], T2). parse_letter([X],L) :- 97=<X, X=<122, name(L,[X]). % The following predicate will accept a derivation tree from parse, and % produce a list of "assembly language" instructions to evaluate the expr. % The assembler uses only nine instructions: load accum. from memory, store % accum. into memory, add memory to accum., subtract memory from accum. % multiply accum. by memory, divide accum. by memory, a no-op, % branch to (i.e., goto) the instruction N (the address) forward from the % current one, and branch on zero accum. to the instruction N forward from % current one. % Code for any (sub) expression leaves its value in the accumulator. % A no-op instruction needs to be added as a last instruction as an exit % target in case the last sub-expression is a conditional compile(Tree, Code) :- % compile/2 calls compile/3 and adds noop compile(Tree, Code1, 1), append(Code1, [noop], Code). % The third argument N is the index of the next available temporary memory % location to be used. compile(exp(T), Code, N) :- compile(T, Code, N). % The code for the conditional is % code(e1?e2:e3) = code(e1)++[brz(N1)]++code(e2)++[br(N2)]++code(e3) % where N1 branches over code(e2) and N2 branches over code(e3) compile(cnd(T1,T2,T3), Code, N) :- compile(T1, Code1, N), compile(T2, Code2, N), compile(T3, Code3, N), length(Code2,L2), B1 is L2+2, length(Code3,L3), B2 is L3+1, append(Code2, [br(B2)|Code3], Code23), append(Code1, [brz(B1)|Code23], Code). compile(trm(T), Code, N) :- compile(T, Code, N). % N is temp memory index compile(sum(T1,T2), Code, N) :- compile(T2, Code2, N), N1 is N+1, compile(T1, Code1, N1), nextTemp(Temp,N), append(Code2, [sto(Temp) | Code1], Code3), % 'sto' is store inst append(Code3, [add(Temp)], Code). % 'add' is add inst compile(dif(T1,T2), Code, N) :- compile(T2, Code2, N), N1 is N+1, compile(T1, Code1, N1), nextTemp(Temp,N), append(Code2, [sto(Temp) | Code1], Code3), % 'sto' is store inst append(Code3, [sub(Temp)], Code). % 'sub' is subtract inst compile(fac(T), Code, N) :- compile(T, Code, N).
execute2([add(A) | Code], Reg, Env, Res) :- member((A,X), Env) % lookup value of A in Env -> Y is Reg+X, execute2(Code, Y, Env, Res) ; undefined_var(A). execute2([sub(A) | Code], Reg, Env, Res) :- member((A,X), Env) % lookup value of A in Env -> Y is Reg-X, execute2(Code, Y, Env, Res) ; undefined_var(A). execute2([mul(A) | Code], Reg, Env, Res) :- member((A,X), Env) % lookup value of A in Env -> Y is Reg*X, execute2(Code, Y, Env, Res) ; undefined_var(A). execute2([div(A) | Code], Reg, Env, Res) :- member((A,X), Env) % lookup value of A in Env -> Y is Reg/X, execute2(Code, Y, Env, Res) ; undefined_var(A). execute2([br(A) | Code], Reg, Env, Res) :- A1 is A-1, drop(A1,Code,Code1), execute2(Code1, Reg, Env, Res). execute2([brz(A) | Code], Reg, Env, Res) :- A1 is A-1, drop(A1,Code,Code1), Reg =:= 0 -> execute2(Code1, Reg, Env, Res) ; execute2(Code, Reg, Env, Res). undefined_var(A) :- write(undefined_var(A)), nl, fail. % drop(?N,?Xs,?Ys) succeeds when Ys is the list obtained by removing the first % N items of list Xs. drop(N,Xs,Ys) :- append(Zs,Ys,Xs), length(Zs,N).