/* article([the|T],T).
article([a|T],T).
noun([cat|T],T).
noun([dog|T],T).
verb([saw|T],T).
verb([chased|T],T).
sentence(L,S) :-
 article(L,S1),noun(S1,S2),verb(S2,S3),article(S3,S4),noun(S4,S).
*/
/*
article --> [the].
article --> [a].
noun --> [cat].
noun --> [dog].
verb --> [saw].
verb --> [chased].
sentence --> article, noun, verb,article, noun.
*/
article(article(the)) --> [the].
article(article(a)) --> [a].
noun(noun(cat)) --> [cat].
noun(noun(dog)) --> [dog].
verb(verb(saw)) --> [saw].
verb(verb(chased)) --> [chased].
sentence(sentence(A1,N1,V,A2,N2)) --> 
 article(A1),noun(N1),
 verb(V),article(A2),noun(N2).
digit(0) --> [0].
digit(1) --> [1].
digit(2) --> [2].
digit(3) --> [3].
digit(4) --> [4].
digit(5) --> [5].
digit(6) --> [6].
digit(7) --> [7].
digit(8) --> [8].
digit(9) --> [9].
number([D|N]) --> digit(D),number(N).
number([D]) --> digit(D).
base(B) --> ['('],exp(B),[')'].
base(n(N)) --> number(N).
term(*(B,T)) --> base(B),[*],term(T).
term(/(B,T)) --> base(B),[/],term(T).
term(B) --> base(B).
exp(+(T,E)) --> term(T),[+],exp(E).
exp(-(T,E)) --> term(T),[-],exp(E).
exp(T) --> term(T).
eval(E1+E2,V) :- eval(E1,V1),eval(E2,V2),V is V1+V2.
eval(E1-E2,V) :- eval(E1,V1),eval(E2,V2),V is V1-V2.
eval(E1*E2,V) :- eval(E1,V1),eval(E2,V2),V is V1*V2.
eval(E1/E2,V) :- eval(E1,V1),eval(E2,V2),V is V1/V2.
eval(n(N),V) :- evalNumb(N,0,V).
evalNumb([],V,V).
evalNumb([H|T],V1,V) :- V2 is 10*V1+H,evalNumb(T,V2,V).
run(L,V) :- exp(T,L,_),eval(T,V).
