>>2 % Prolog
算術式解析(_文,_残りの文,_初期節番号,_終了節番号,_式) :-
atom_chars(_文,Chars),
算術式(_初期節番号,_終了節番号,Chars,R,_式),
atom_chars(_残りの文,R),!.
定数(A) :- member(A,['0','1','2','3','4','5','6','7','8','9']),!.
変数(A) :- member(A,[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]),!.
変数(A) :- member(A,['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']),!.
算術式(N1,N2,[A|R],R,B) :-
定数(A),
atom_to_term(A,B,_),
assertz(算術式構文木(N1,[],[],B)),
N2 is N1 + 1,!.
算術式(N1,N2,[A|R],R,A) :-
変数(A),
assertz(算術式構文木(N1,[],[],A)),
N2 is N1 + 1,!.
算術式(N1,N2,L,R,'+'(S1,S2)) :-
append(['('|L1],[')'|R],L),append(L2,['+'|L3],L1),
N3 is N1 + 1,
算術式(N3,N4,L2,_,S1),算術式(N4,N2,L3,_,S2),
assertz(算術式構文木(N1,N3,N4,'+')),!.
算術式(N1,N2,L,R,'*'(S1,S2)) :-
append(['('|L1],[')'|R],L),append(L2,['*'|L3],L1),
N3 is N1 + 1,
算術式(N3,N4,L2,_,S1),算術式(N4,N2,L3,_,S2),
assertz(算術式構文木(N1,N3,N4,'*')),!.