Assignment 3 Parser (ab)+ / (ba)+ Parser a^n b^n (n > 1) Parser [0-9] ([+-*/] [0-9])+ Pushdown Automaton (ab)+ / (ba)+ Pushdown Automaton a^n b^n (n > 1) Pushdown Automaton [0-9] ([+*] [0-9])+ Grammar (ab)+ / (ba)+ Grammar a^n b^n (n > 1) Grammar [0-9] ([+-*/] [0-9])* Knowledge Base /* File: prob1_i.pl Author: James Ashworth This accepts strings that can be generated by the grammar: S -> A | B A -> abA | ? B -> baB | ? */ generates(Input) :- atom_codes(X, Input), atom_chars(X, Y), once(start(Y)). start(X) :- evalab(X). start(X) :- evalba(X). evalab([]). evalab([a, b|T]) :- evalab(T). evalba([]). evalba([b, a|T]) :- evalba(T). Queries generates(""). generates("ab"). generates("abab"). generates("ba"). generates("baba"). generates("abba"). Run Output Knowledge Base /* File: prob1_ii.pl Author: James Ashworth This accepts strings that can be generated by the grammar: S -> aNb N -> aNb | ? */ generates(Input) :- atom_codes(X, Input), atom_chars(X, Y), once(eval(Y, [])). evalb([b|T], T). eval([a, b|T], T). eval([a, a|T], X) :- eval([a|T], Y), evalb(Y, X). Queries generates(""). generates("ab"). generates("aabb"). generates("aaabb"). generates("ba"). Run Output Knowledge Base /* File: prob1_iii.pl Author: James Ashworth This accepts strings that can be generated by the grammar: S -> i | iOS O -> + | - | * | / */ generates(Input) :- atom_codes(X, Input), atom_chars(X, Y), once(start(Y)). start(X) :- num(X, []). start(X) :- num(X, X1), operator(X1, X2), start(X2). num([X|R], R) :- member(X, ['0','1','2','3','4','5','6','7','8','9']). operator([X|L], L) :- member(X, ['+','-','*','/']). Queries generates("1"). generates("1+"). generates("+"). generates("1+1"). generates("1+2+3"). Run Output Knowledge Base /* File: prob2_i.pl Author: James Ashworth This PDA accepts strings that can also be generated by the grammar: S -> A | B A -> abA | ? B -> baB | ? */ accept(L) :- initial(Q), atom_codes(X, L), atom_chars(X, Y), once(accept(Q, Y, [$])). accept(Q, [H|T], S) :- delta(Q, H, S, Q1, S1), accept(Q1, T, S1). accept(Q, [], [$]) :- final(Q). initial(q0). final(q0). final(q3). delta(q0, a, [$|T], q1, [a, $|T]). delta(q1, b, [a|T], q1, T). delta(q1, a, [$|T], q1, [a, $|T]). delta(q1, b, [a|T], q3, T). delta(q0, b, [$|T], q2, [b, $|T]). delta(q2, a, [b|T], q2, T). delta(q2, b, [$|T], q2, [b, $|T]). delta(q2, a, [b|T], q3, T). Queries accept(""). accept("ab"). accept("abab"). accept("ba"). accept("baba"). accept("abba"). Run Output Knowledge Base /* File: prob2_ii.pl Author: James Ashworth This PDA accepts strings that can also be generated by the grammar: S -> aNb N -> aNb | ? */ accept(L) :- initial(Q), atom_codes(X, L), atom_chars(X, Y), once(accept(Q, Y, [$])). accept(Q, [H|T], S) :- delta(Q, H, S, Q1, S1), accept(Q1, T, S1). accept(Q, [], [$]) :- final(Q). initial(q0). final(q2). delta(q0, a, [$|T], q1, [a, $|T]). delta(q1, a, S, q1, [a|S]). delta(q1, b, [a|S], q2, S). delta(q2, b, [a|S], q2, S). Queries accept(""). accept("ab"). accept("aabb"). accept("aaabb"). accept("ba"). Run Output Knowledge Base /* File: prob2_iii.pl Author: James Ashworth This PDA accepts strings that can also be generated by the grammar: S -> iOA O -> + | * A -> i | iOA */ accept(L) :- initial(Q), atom_codes(X, L), atom_chars(X, Y), once(accept(Q, Y, [$])). accept(Q, [H|T], S) :- delta(Q, H, S, Q1, S1), accept(Q1, T, S1). accept(Q, [], [$]) :- final(Q). initial(q0). final(q1). delta(q0, X , [$|T], q1, [$|T] ) :- member(X, ['0','1','2','3','4','5','6','7','8','9']). delta(q1, '*', [$|T], q1, [a, $|T]). delta(q1, '+', [$|T], q1, [a, $|T]). delta(q1, X , [a|T], q1, T ) :- member(X, ['0','1','2','3','4','5','6','7','8','9']). Queries accept("1"). accept("1+"). accept("+"). accept("1+1"). accept("1+2+3"). Run Output Knowledge Base /* File: prob3_i.pl Author: James Ashworth This grammar can be written as: S -> A | B A -> abA | ? B -> baB | ? */ generates(Input) :- atom_codes(X, Input), atom_chars(X, Y), once(phrase(start, Y)). start --> ab. start --> ba. ab --> [a, b], ab. ab --> []. ba --> [b, a], ba. ba --> []. Queries generates(""). generates("ab"). generates("abab"). generates("ba"). generates("baba"). generates("abba"). Run Output Knowledge Base /* File: prob3_ii.pl Author: James Ashworth This grammar can be written as: S -> aNb N -> aNb | ? */ generates(Input) :- atom_codes(X, Input), atom_chars(X, Y), once(phrase(start, Y)). start --> [a], middle, [b]. middle --> [a], middle, [b]. middle --> []. Queries generates(""). generates("ab"). generates("aabb"). generates("aaabb"). generates("ba"). Run Output Knowledge Base /* File: prob3_iii.pl Author: James Ashworth This grammar can be written as: S -> i | iOS O -> + | - | * | / */ generates(Input) :- atom_codes(X, Input), atom_chars(X, Y), once(phrase(start, Y)). start --> num. start --> num, operator, start. num --> [D], {member(D, ['0','1','2','3','4','5','6','7','8','9'])}. operator --> [O], {member(O, ['+','-','*','/'])}. Queries generates("1"). generates("1+"). generates("+"). generates("1+1"). generates("1+2+3"). Run Output