Prolog中的DCG - 字符串

时间:2010-12-08 15:54:00

标签: parsing lisp prolog grammar dcg

我正在使用Prolog的内置DCG功能编写Lisp-to-C转换器。这就是我处理算术的方法:

expr(Z) --> "(", "+", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%d + %d", [M, N])}.
expr(Z) --> "(", "-", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%d - %d", [M, N])}.
expr(Z) --> "(", "*", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%d * %d", [M, N])}.
expr(Z) --> "(", "/", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%d / %d", [M, N])}.
expr(E) --> number(E).

number(C) --> "-", digits(X), {C is -X}.
number(C) --> digits(C).
digits(D) --> digit(D);digit(A),digits(B), {number_codes(B,Cs),length(Cs,L), D is A*(10^L)+B}.
digit(D) --> [C], {"0"=<C, C=<"9", D is C - "0"}.

现在,它不处理嵌套表达式。以下是我认为可行的方法:

expr(Z) --> "(", "+", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "%s + %s", [M, N])}.
expr(E) --> number(N), {swritef(E, "%d", [N])}.

但我得到了这个:

?- expr(E, "42", []).
E = "42" %all OK

?- expr(E, "(+ 3 (* 2 2))", []).
E = "%s + %s" %not OK

如何让它发挥作用?

2 个答案:

答案 0 :(得分:2)

问题是%s格式说明符需要参数是一个字符列表。 所以你可以这样做:

:-set_prolog_flag(double_quotes, codes).  % This is for SWI 7+ to revert to the prior interpretation of quoted strings.

expr(Z) --> "(", "+", spaces, lexpr(M), spaces, lexpr(N), ")", {swritef(Z, "%s + %s", [M, N])}.
expr(Z) --> "(", "-", spaces, lexpr(M), spaces, lexpr(N), ")", {swritef(Z, "%s - %s", [M, N])}.
expr(Z) --> "(", "*", spaces, lexpr(M), spaces, lexpr(N), ")", {swritef(Z, "%s * %s", [M, N])}.
expr(Z) --> "(", "/", spaces, lexpr(M), spaces, lexpr(N), ")", {swritef(Z, "%s / %s", [M, N])}.
expr(N) --> number(N).

lexpr(Z) --> expr(M), {atom_chars(M, Z)}.

number(C) --> "-", digits(X), {C is -X}.
number(C) --> digits(C).

digits(D) --> digit(D);digit(A),digits(B), {number_codes(B,Cs),length(Cs,L), D is A*(10^L)+B}.
digit(D) --> [C], {"0"=<C, C=<"9", D is C - "0"}.

spaces --> " ", spaces.
spaces --> [].

谓词lexpr只是将解析后的表达式转换为字符列表。

编辑:03/07/2016:自SWI 7.0版开始,用双引号括起来的文字不再被解释为字符代码列表。 您可以使用后引号(`)更改双引号或添加指令;

:-set_prolog_flag(double_quotes, codes).

在代码的开头。

答案 1 :(得分:1)

swritef中使用%t or %w, not %d。注意,%d不是C的printf格式。

如果你只是将类似lisp的转换为C-like,那么你真的不需要转换字符串 将数字表示为数字。把它留作字符串。 (当然它依赖于 关于你的任务的复杂性)。否则,上层规则会产生一个他们期望字符串的数字。

将生成的C代码放入括号中,以使结果中的优先级和关联性正确。

expr(Z) --> "(", "-", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "(%t - %t)", [M, N])}.
expr(Z) --> "(", "*", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "(%t * %t)", [M, N])}.
expr(Z) --> "(", "/", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "(%t / %t)", [M, N])}.
expr(Z) --> "(", "+", spaces, expr(M), spaces, expr(N), ")", {swritef(Z, "(%t + %t)", [M, N])}.
expr(E) --> number(N), {swritef(E, "%s", [N])}.

spaces --> " ".

number([C|Cs]) --> "-", {C = "-"}, digits(Cs).
number(C) --> digits(C).

digits([D|[]]) --> digit(D).

digits([D|Ds]) --> digit(D), digits(Ds).
digit(D) --> [D], {code_type(D, digit)}.

这就是它的工作原理。

?- expr(E, "(* 1342 (/ 44 -17))", []).
E = "(1342 * (44 / -17))" ;
false.