prolog,机器人用语法+解析器移动
prolog, robot move with grammar + parser
我在 Prolog 中进行了一些语法练习。这是我第一次接触语法(任何语言)。我需要创建一个解析器来计算机器人的目的地坐标以及它当前指向的方向(标记为:n、s、w、e 之一)。
可能的走法是:[go,10]
(或任何数字)、[turn, left]
、[turn, right]
。
编写识别正确句子的解析器很容易(我认为..),例如:[go,10,turn,right,go,10]
:
robot_p-->pol.
pol-->[go],num.
pol-->[go],num,pol.
pol-->[turn],[left].
pol-->[turn],[left],pol.
pol-->[turn],[right].
pol-->[turn],[right],pol.
num-->[D],{number(D)}.
robot(L):-robot_p(L,[]).
但是现在,我不知道,我怎么能把每一步都累积起来,所以对于我举的例子来说,结果会是[10,10,e]
。我想我应该这样写:
robot_p(result)-->pol(result).
但如果是这样,它会是什么样子呢?我可以将 num
更改为:
num(D)-->[D],{number(D)}.
接下来开始:
pol([X,Y,N])-->[go],num(D),{N==n;//here I don't know what next}.
但是,我不确定这是否是个好方法,如果是,现在该怎么办。
编辑:
忘记说了,我需要传递起始坐标。
所以对于 [0,0,n]
和移动 [go,10,turn,right,go,10]
结果是 [10,10,e]
.
编辑2:
非常感谢@BretC 的帮助。我已经按照您的提示创建了令我满意的版本。也许它会对某人有所帮助:)
pol([X])-->[go],num(X).
pol([X| T])-->[go],num(X),pol(T).
pol([left])-->[turn],[left].
pol([left | T])-->[turn],[left],pol(T).
pol([right])-->[turn],[right].
pol([right | T])-->[turn],[right],pol(T).
commands(L,V):-pol(V,L,[]).
move([X,Y,D],V,[X1,Y,D]):-D==n,X1 is X+V.
move([X,Y,D],V,[X1,Y,D]):-D==s,X1 is X-V.
move([X,Y,D],V,[X,Y1,D]):-D==w,Y1 is Y-V.
move([X,Y,D],V,[X,Y1,D]):-D==e,Y1 is Y+V.
rotate(D,P,D1):-P==left,D==n,D1=w.
rotate(D,P,D1):-P==left,D==s,D1=e.
rotate(D,P,D1):-P==left,D==w,D1=s.
rotate(D,P,D1):-P==left,D==e,D1=n.
rotate(D,P,D1):-P==right,D==n,D1=e.
rotate(D,P,D1):-P==right,D==s,D1=w.
rotate(D,P,D1):-P==right,D==w,D1=n.
rotate(D,P,D1):-P==right,D==e,D1=s.
robot_H(D,[],D).
robot_H([X,Y,D],[H|T],K):-number(H),move([X,Y,D],H,ND),robot_H(ND,T,K).
robot_H([X,Y,D],[H|T],K):- \+number(H),rotate(D,H,D1),robot_H([X,Y,D1],T,K).
robot(S,P,K):-commands(P,P1),robot_H(S,P1,K).
num(X)-->[X],{number(X)}.
因此,例如:
?- robot([0,0,n],[go, 10, turn, left,turn,left, go, 20],K).
K = [-10, 0, s]
不确定这是否有帮助,但我会使用 DGC 构建代表自然语言的子句列表,然后处理这些子句。
例如,如果您将以下参数添加到语法中...
pol([move(X)])-->[go],num(X).
pol([move(X) | T])-->[go],num(X),pol(T).
pol([turn(left)])-->[turn],[left].
pol([turn(left) | T])-->[turn],[left],pol(T).
pol([turn(right)])-->[turn],[right].
pol([turn(right) | T])-->[turn],[right],pol(T).
num(D)-->[D],{number(D)}.
...现在您可以获得 'commands' 的列表,例如...
4 ?- pol(X, [go,10,turn,left,go,10], []).
X = [move(10), turn(left), move(10)] .
这就是解析位,现在你需要编写子句来处理模拟,例如...
% Parses grammar and processes commands
robot(SENTANCE, XOut, YOut) :-
% Parse the sentance
pol(COMMANDS, SENTANCE, []),
% Process the commands
process(COMMANDS, north, 0, 0, XOut, YOut).
% process(COMMAND_LIST, HEADING, XSTART, YSTART, XOUT, YOUT)
% Case 1: No commands to process, return current position
process([], _, X, Y, X, Y).
% Case 2: Process a single command then process the rest of the commands
process([COMMAND | T], Heading, XIn, YIn, XOut, YOut) :-
do_command(COMMAND, Heading, XIn, YIn, HeadingTmp, XTmp, YTmp),
process(T, HeadingTmp, XTmp, YTmp, XOut, YOut).
% do_command(COMMAND, HEADING_START, XSTART, YSTART, HEADING_OUT, XOUT, YOUT).
% Move in current direction command
do_command(move(N), Heading, X, Y, Heading, XOut, YOut) :-
x_delta(Heading, N, XDelta),
y_delta(Heading, N, YDelta),
XOut is X + XDelta,
YOut is Y + YDelta.
% Turn command
do_command(turn(Dir), HeadingIn, X, Y, HeadingOut, X, Y) :-
turn(HeadingIn, Dir, HeadingOut).
% Utility clauses to help with deltas + turns
x_delta(north, _, 0).
x_delta(south, _, 0).
x_delta(east, X, X).
x_delta(west, X, Y) :- Y is -X.
y_delta(north, X, X).
y_delta(south, X, Y) :- Y is -X.
y_delta(east, _, 0).
y_delta(west, _, 0).
turn(north, right, east).
turn(east, right, south).
turn(south, right, west).
turn(west, right, north).
turn(north, left, west).
turn(east, left, north).
turn(south, left, east).
turn(west, left, south).
示例输出...
2 ?- robot([go, 10, turn, left, go, 11], X, Y).
X = -11,
Y = 10
我会应用每一个动作来改变当前状态,获得更新后的状态。
pol(Curr,Curr) --> [].
pol(Curr,Last) --> move(Curr,Next), pol(Next, Last).
move(s(X,Y,D), s(X1,Y1,D)) -->
[go, N], {
D == n -> X1 is X, Y1 is Y+N
;D == e -> X1 is X-N, Y1 is Y
...
}.
move(s(X,Y,D), s(X,Y,D1)) -->
[turn, left], { D == n -> D1 = e ; ... }.
并用 phrase/2
来调用它
?- phrase(pol(s(0,0,n),F), [go,10]).
我在 Prolog 中进行了一些语法练习。这是我第一次接触语法(任何语言)。我需要创建一个解析器来计算机器人的目的地坐标以及它当前指向的方向(标记为:n、s、w、e 之一)。
可能的走法是:[go,10]
(或任何数字)、[turn, left]
、[turn, right]
。
编写识别正确句子的解析器很容易(我认为..),例如:[go,10,turn,right,go,10]
:
robot_p-->pol.
pol-->[go],num.
pol-->[go],num,pol.
pol-->[turn],[left].
pol-->[turn],[left],pol.
pol-->[turn],[right].
pol-->[turn],[right],pol.
num-->[D],{number(D)}.
robot(L):-robot_p(L,[]).
但是现在,我不知道,我怎么能把每一步都累积起来,所以对于我举的例子来说,结果会是[10,10,e]
。我想我应该这样写:
robot_p(result)-->pol(result).
但如果是这样,它会是什么样子呢?我可以将 num
更改为:
num(D)-->[D],{number(D)}.
接下来开始:
pol([X,Y,N])-->[go],num(D),{N==n;//here I don't know what next}.
但是,我不确定这是否是个好方法,如果是,现在该怎么办。
编辑:
忘记说了,我需要传递起始坐标。
所以对于 [0,0,n]
和移动 [go,10,turn,right,go,10]
结果是 [10,10,e]
.
编辑2:
非常感谢@BretC 的帮助。我已经按照您的提示创建了令我满意的版本。也许它会对某人有所帮助:)
pol([X])-->[go],num(X).
pol([X| T])-->[go],num(X),pol(T).
pol([left])-->[turn],[left].
pol([left | T])-->[turn],[left],pol(T).
pol([right])-->[turn],[right].
pol([right | T])-->[turn],[right],pol(T).
commands(L,V):-pol(V,L,[]).
move([X,Y,D],V,[X1,Y,D]):-D==n,X1 is X+V.
move([X,Y,D],V,[X1,Y,D]):-D==s,X1 is X-V.
move([X,Y,D],V,[X,Y1,D]):-D==w,Y1 is Y-V.
move([X,Y,D],V,[X,Y1,D]):-D==e,Y1 is Y+V.
rotate(D,P,D1):-P==left,D==n,D1=w.
rotate(D,P,D1):-P==left,D==s,D1=e.
rotate(D,P,D1):-P==left,D==w,D1=s.
rotate(D,P,D1):-P==left,D==e,D1=n.
rotate(D,P,D1):-P==right,D==n,D1=e.
rotate(D,P,D1):-P==right,D==s,D1=w.
rotate(D,P,D1):-P==right,D==w,D1=n.
rotate(D,P,D1):-P==right,D==e,D1=s.
robot_H(D,[],D).
robot_H([X,Y,D],[H|T],K):-number(H),move([X,Y,D],H,ND),robot_H(ND,T,K).
robot_H([X,Y,D],[H|T],K):- \+number(H),rotate(D,H,D1),robot_H([X,Y,D1],T,K).
robot(S,P,K):-commands(P,P1),robot_H(S,P1,K).
num(X)-->[X],{number(X)}.
因此,例如:
?- robot([0,0,n],[go, 10, turn, left,turn,left, go, 20],K).
K = [-10, 0, s]
不确定这是否有帮助,但我会使用 DGC 构建代表自然语言的子句列表,然后处理这些子句。
例如,如果您将以下参数添加到语法中...
pol([move(X)])-->[go],num(X).
pol([move(X) | T])-->[go],num(X),pol(T).
pol([turn(left)])-->[turn],[left].
pol([turn(left) | T])-->[turn],[left],pol(T).
pol([turn(right)])-->[turn],[right].
pol([turn(right) | T])-->[turn],[right],pol(T).
num(D)-->[D],{number(D)}.
...现在您可以获得 'commands' 的列表,例如...
4 ?- pol(X, [go,10,turn,left,go,10], []).
X = [move(10), turn(left), move(10)] .
这就是解析位,现在你需要编写子句来处理模拟,例如...
% Parses grammar and processes commands
robot(SENTANCE, XOut, YOut) :-
% Parse the sentance
pol(COMMANDS, SENTANCE, []),
% Process the commands
process(COMMANDS, north, 0, 0, XOut, YOut).
% process(COMMAND_LIST, HEADING, XSTART, YSTART, XOUT, YOUT)
% Case 1: No commands to process, return current position
process([], _, X, Y, X, Y).
% Case 2: Process a single command then process the rest of the commands
process([COMMAND | T], Heading, XIn, YIn, XOut, YOut) :-
do_command(COMMAND, Heading, XIn, YIn, HeadingTmp, XTmp, YTmp),
process(T, HeadingTmp, XTmp, YTmp, XOut, YOut).
% do_command(COMMAND, HEADING_START, XSTART, YSTART, HEADING_OUT, XOUT, YOUT).
% Move in current direction command
do_command(move(N), Heading, X, Y, Heading, XOut, YOut) :-
x_delta(Heading, N, XDelta),
y_delta(Heading, N, YDelta),
XOut is X + XDelta,
YOut is Y + YDelta.
% Turn command
do_command(turn(Dir), HeadingIn, X, Y, HeadingOut, X, Y) :-
turn(HeadingIn, Dir, HeadingOut).
% Utility clauses to help with deltas + turns
x_delta(north, _, 0).
x_delta(south, _, 0).
x_delta(east, X, X).
x_delta(west, X, Y) :- Y is -X.
y_delta(north, X, X).
y_delta(south, X, Y) :- Y is -X.
y_delta(east, _, 0).
y_delta(west, _, 0).
turn(north, right, east).
turn(east, right, south).
turn(south, right, west).
turn(west, right, north).
turn(north, left, west).
turn(east, left, north).
turn(south, left, east).
turn(west, left, south).
示例输出...
2 ?- robot([go, 10, turn, left, go, 11], X, Y).
X = -11,
Y = 10
我会应用每一个动作来改变当前状态,获得更新后的状态。
pol(Curr,Curr) --> [].
pol(Curr,Last) --> move(Curr,Next), pol(Next, Last).
move(s(X,Y,D), s(X1,Y1,D)) -->
[go, N], {
D == n -> X1 is X, Y1 is Y+N
;D == e -> X1 is X-N, Y1 is Y
...
}.
move(s(X,Y,D), s(X,Y,D1)) -->
[turn, left], { D == n -> D1 = e ; ... }.
并用 phrase/2
来调用它?- phrase(pol(s(0,0,n),F), [go,10]).