如何将 DCG 转换为 PROLOG 中的普通定语子句?
如何将以下 DCG 转换为 PROLOG 的普通定子句?
expr_regular --> cor_ini,numero,guion,numero,cor_fin.
cor_ini --> ['['].
numero --> ['0'];['1'];['2'];['3'];['4'];['5'];['6'];['7'];['8'];['9'].
cor_fin --> [']'].
guion --> ['-'].
编辑:我想将 DCG 转换为普通 PROLOG 子句,因为我不能在同一代码中同时使用 DCG 和普通子句(在我的情况)。我有这两段代码:
Piece1:
traducir(Xs, Ys) :- maplist(traduccion, Xs, Ys).
traduccion('^',comeza_por).
traduccion('[',inicio_rango).
traduccion('0',cero).
traduccion('-',a).
traduccion('9',nove).
traduccion(']',fin_rango).
如何使用它的示例:
?- traducir(['[','0','-','9',']'],[]).
true .
和 Piece2:
traducir--> cor_ini,numero,guion,numero,cor_fin.
cor_ini --> ['['].
numero --> ['0'];['1'];['2'];['3'];['4'];['5'];['6'];['7'];['8'];['9'].
cor_fin --> [']'].
guion --> ['-'].
如何使用它的示例:
traducir(['^','[','0','-','9',']'],X).
X = [comeza_por, inicio_rango, cero, a, nove, fin_rango].
我想将两个代码合并成一个,以测试 traducir 是否写得很好(如果它遵循 DCG)并将您输入的内容翻译成文本,因此最终的程序应该能够执行以下操作:
?- traducir(['^','[','0','-','9',']'],X).
X = [comeza_por, inicio_rango, cero, a, nove, fin_rango].
?- traducir(['[','0','-','9',']'],[]).
true .
How would you translate the following DCGs into normal definite clauses of PROLOG?
expr_regular --> cor_ini,numero,guion,numero,cor_fin.
cor_ini --> ['['].
numero --> ['0'];['1'];['2'];['3'];['4'];['5'];['6'];['7'];['8'];['9'].
cor_fin --> [']'].
guion --> ['-'].
EDIT: I want to translate the DCG into normal PROLOG clauses cause I can't use both DCGs and normal clauses in the same code(in my case). I have this two pieces of code:
Piece1:
traducir(Xs, Ys) :- maplist(traduccion, Xs, Ys).
traduccion('^',comeza_por).
traduccion('[',inicio_rango).
traduccion('0',cero).
traduccion('-',a).
traduccion('9',nove).
traduccion(']',fin_rango).
An example of how to use it would be:
?- traducir(['[','0','-','9',']'],[]).
true .
And Piece2:
traducir--> cor_ini,numero,guion,numero,cor_fin.
cor_ini --> ['['].
numero --> ['0'];['1'];['2'];['3'];['4'];['5'];['6'];['7'];['8'];['9'].
cor_fin --> [']'].
guion --> ['-'].
An example of how to use it would be:
traducir(['^','[','0','-','9',']'],X).
X = [comeza_por, inicio_rango, cero, a, nove, fin_rango].
I want to join both codes into one to test if traducir is well written (if it follows the DCG) and to translate what you enter into text ,so the final program should able to do the following:
?- traducir(['^','[','0','-','9',']'],X).
X = [comeza_por, inicio_rango, cero, a, nove, fin_rango].
?- traducir(['[','0','-','9',']'],[]).
true .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SWI-Prolog 中,您可以直接在 prolog-toplevel:
就是这样!查看相关问题的答案“有没有一种方法或算法可以将 DCG 转换为 Prolog 中的普通定语子句?”。
In SWI-Prolog you can use
listing/1
directly on the prolog-toplevel:That's it! You may profit from looking at the answers of the related question "Is there a way or an algorithm to convert DCG into normal definite clauses in Prolog?".
你的语法非常简单:只是终端。因此,我们可以转换为非常具体的模式(注意:不允许泛化)。
唯一值得注意的是 ISO 标准字符表示法......
Your grammar it's exceptionally simple: just terminals. So we can translate to a very specific pattern (beware: no generalization allowed).
The only think worth to note it's ISO standard character notation...
好吧,我真的不明白你的问题。无论如何,如果您出于某种原因不想使用漂亮的 DCG 语法,您仍然可以使用
wildcard_match/2
之类的东西,或者重新发明轮子并自己使用差异列表来重新实现 DCG,或追加。对于wildcard_match/2
部分:Well I don't really get your question. Anyway, if you don't want to use the nice DCGs syntax for some reason you can still go with things like
wildcard_match/2
, or reinvent the wheel and use difference lists yourself to re-implement DCGs, or append. For thewildcard_match/2
part: