% File: c.p % grammar for a shapes micro-world % pseudo-code for semantic action translated to Prolog :- ['process.p']. :- dynamic shape/4. sentence --> cmmnd. sentence --> query. sentence --> []. cmmnd --> [place], indefArticle, color(C), [square], [at], number(X), number(Y), [with], [side], number(S), ['.'], { assert(shape(square,coords(X, Y),side(S),color(C))) }. cmmnd --> [place], indefArticle, color(C), [rectangle], [at], number(X), number(Y), [of], [height], number(H), [and], [width], number(W), ['.'], { assert(shape(rectangle,coords(X, Y),sides(H,W),color(C))) }. cmmnd --> [place], indefArticle, color(C), [circle], [at], number(X), number(Y), [of], [radius], number(R), ['.'], { assert(shape(circle,coords(X, Y),radius(R),color(C))) }. cmmnd --> [stop], ['.']. query --> [what], shapes(Shape), [are], [there], ['?'], { enumerate(Shape) }. query --> [what], [color], shapes(Shape), [are], [present], ['?'], { enumerate_colors(Shape) }. query --> [are], [there], [any], color(C), shapes(Shape), [present], ['?'], { respond_yes_no(C,Shape) }. query --> [how], [big], [is], defArticle, shape(Shape), [at], number(X), number(Y), ['?'], { respond_size(Shape,X,Y) }. defArticle --> [the]. indefArticle --> [a]. indefArticle --> [an]. color(red) --> [red]. color(blue) --> [blue]. color(green) --> [green]. number(N) --> [N], { number(N) }. shape(square) --> [square]. shape(rectangle) --> [rectangle]. shape(circle) --> [circle]. shape(shape) --> [shape]. shapes(square) --> [squares]. shapes(rectangle) --> [rectangles]. shapes(circle) --> [circles]. shapes(shape) --> [shapes]. enumerate(shape) :- shape(Shape,coords(X,Y),_,color(C)), write(Shape), write_string(" at ("), write(X), write_string(","), write(Y), write_string("), with color "), write(C), nl, fail. enumerate(shape). enumerate(Shape) :- shape(Shape,coords(X,Y),_,color(C)), write(Shape), write_string(" at ("), write(X), write_string(","), write(Y), write_string("), with color "), write(C), nl, fail. enumerate(_). enumerate_colors(shape) :- shape(_,coords(_,_),_,color(C)), write(C), nl, fail. enumerate_colors(shape). enumerate_colors(Shape) :- shape(Shape,coords(_,_),_,color(C)), write(C), nl, fail. enumerate_colors(_). respond_yes_no(C,shape) :- shape(_,coords(_,_),_,color(C)), write_string("yes"), nl. respond_yes_no(_,shape) :- write_string("no"), nl. respond_yes_no(C,Shape) :- shape(Shape,coords(_,_),_,color(C)), write_string("yes"), nl. respond_yes_no(_,_) :- write_string("no"), nl. respond_size(shape,X,Y) :- shape(_,coords(X,Y),Size,color(_)), write(Size), nl. respond_size(_,X,Y) :- write_string("Couldn't find anything at "), write(X), write_string(" "), write(Y), nl. respond_size(Shape,X,Y) :- shape(Shape,coords(X,Y),Size,color(_)), write(Size), nl. respond_size(_,X,Y) :- write_string("Couldn't find anything at "), write(X), write_string(" "), write(Y), nl.