Prolog-将英语翻译成C [英] Prolog- translating English to C
问题描述
我们有一个相对简单的任务,我在理论上理解,但我认为我只是不太理解 Prolog 的语法,不足以将其写入代码.基本上,我们有一个表示 C 中操作的英文符号列表.当它们被传递给我们的 Prolog 程序时,它们被存储为一个列表.例如:
We have a relatively simple assignment that I understand in theory but I think I just don't quite understand Prolog's syntax enough to get that into code. Basically, we have a list of English notations that represent operations in C. They're stored as a list when they're passed to our Prolog program. For example:
add 4 to 3
是
[add, 4, to, 3]
我们需要编写一个函数来获取该列表并返回等效项.所以如果我打电话给
We need to write a function that takes that list an returns the equivalent. So if I called
english2C([add,4,to,3], C).
C = 4+3
它将 C 绑定到结果.所以数据结构本身类似于 +(4(3)).我们有一个我们必须翻译的英语符号的列表,所以它是一个有限的数字.我们不必考虑所有可能性.还有组合,它们采用两个操作并将它们组合起来(中间用逗号)
It would bind C to the result. So the data structure itself would be something like +(4(3)). We have a list of such English notation we have to translate, so it's a finite number. It's not like we have to account for all possibilities. There are also combinations, where they take two operations and combine them (with a comma in between)
english2C([add,3,to,5,',',then,subtract,7], C).
C = 3+5-7
我只是有点困惑如何开始.我知道我可以获取列表的第一个元素,它始终是一个运算符(+、-、* 等),然后我可以递归地遍历列表以查找操作数.问题在于需要操作顺序的事物,例如将 3 加到 5 然后乘以 4",它应该表示为 (3+5)*4 但如果你直接翻译它,你会得到 3+5*4.
I'm just somewhat confused as to how to start. I know I can take the very first element of the list and that will always be an operator (+,-,*, etc etc) and then I can just recursively go through the list looking for the operands. The problem there is for things that require order of operations, like "add 3 to 5 then multiply by 4", which should be represented as (3+5)*4 but if you just translate it directly you get 3+5*4.
哦,我们必须看看我们是否可以让它向后运行(给它一个 C 语句 (3+5) 并翻译回英文(添加 3 到 5)).那部分我真的一点想法都没有.
Oh and we have to see if we can get it to run backwards (give it a C statement (3+5) and translate back to english (add 3 to 5)). That part I don't really have an idea for at all.
有足够大的可能的英文符号排列,我不能只是模式匹配所有内容.我知道我需要做的是将第一个运算符与其相应的算术符号匹配,然后找到操作数.对于组合语句,这将是第一部分(所以我会有 3+5),然后会有一个逗号,然后是下一个语句.顺便说一句,组合语句可以随心所欲,所以不仅仅是两个语句,我已经完成了.
There's a large enough permutations of possible English notations that I can't just pattern match everything. I get the idea that what I need to do is match the first operator with it's corresponding arithmetic symbol then find the operands. For a combinational statement, that would be the first part (so I would have 3+5) and then there would be a comma followed by the next statement. By the way, the combinational statements can be as long as they want, so it's not just two statements and I'm done.
推荐答案
如果有合理的少量模式,你可以这样做:
If there is a reasonable small number of patterns, you could do:
english2C([add,X,to,Y], R) :- R is X+Y.
english2C([add,A,to,B,',',then,subtract,C], R) :- R is A+B-C.
编辑
上面的那些规则计算值.要翻译,我们可以使用 DCG 进行匹配,它也可以向后"工作.
Those rules above compute the value. To translate, we can use DCG for matching, it's working 'backwards' as well.
english2C(In, R) :- phrase(toe(R), In, []).
toe(X+Y) --> [add,X,to,Y].
toe(X*Y) --> [multiply,X,by,Y].
toe(L-R) --> toe(L), [',',then,subtract,R].
测试:
?- english2C(X,3+6).
X = [add, 3, to, 6].
编辑抱歉,我忘记剪辑了.加上这个,我得到了
edit sorry, I forgot a cut. With that added, I get
?- english2C([add,3,to,5,',',then,subtract,4],X).
X = 3+5-4.
?- english2C(L,3+5-4).
L = [add, 3, to, 5, ',', then, subtract, 4].
没有,后面有循环;
?- english2C([add,3,to,5,',',then,subtract,4],X).
X = 3+5-4 ;
^CAction (h for help) ? goals
[698,875] toe(_G2096630, [add, 3, to, 5, ',', then, subtract, 4], _G2096652)
[698,874] toe('<garbage_collected>', '<garbage_collected>', _G2096652)
...
单点变化:你更喜欢自己找吗?
It's a single point change: do you prefer to find it yourself?
这篇关于Prolog-将英语翻译成C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!