如何在 DCG 中表示字母数字字符串?
How do I represent an alphanumeric string in DCG?
我正在尝试为编码为 S 表达式的命题演算编写解析器。
我取得了一些进步:
expression --> op.
op --> ['('], bin-op, bool, bool, [')'].
op --> ['('], unary-op, bool, [')'].
bool --> tok.
bool --> op.
bin-op --> ["IFF"].
bin-op --> ["IF"].
bin-op --> ["XOR"].
unary-op --> ["NOT"].
tok --> ["a"].
在 swipl
中,我通过调用 phrase
:
获得了适当的响应
?- phrase(expression, Ls).
Ls = ['(', "IFF", "a", "a", ')']
然而,这仅适用于 tok
“a”。有没有办法在 DCG 中说“tok 是任何字母数字字符串”?我找到了 this,但我不确定如何将它应用到我正在做的事情中。
如果你只想解析,那么下面的token就可以了。
tok([A|B], B) :- an_code(A).
alpha_numeric(X) :-
between(0'0, 0'9, X); between(0'A, 0'Z, X); between(0'a, 0'z, X).
an_code(A) :- atom_codes(A, C), maplist(alpha_numeric, C).
?- phrase(expression, ['(', "IFF", "A1", "1A", ')']).
true
?- phrase(expression, ['(', "IFF", ".A1", "1A", ')']).
false.
?- phrase(expression, ['(', "IFF", ".A1", "(1A", ')']).
false.
使用 an_code 如下所示,您也可以生成公式:
an_code(A) :- var(A) ->
length(C,L), L >= 1,
maplist(alpha_numeric, C),
string_codes(A, C);
atom_codes(A, C), maplist(alpha_numeric, C).
?- phrase(expression, Ls).
Ls = ['(', "IFF", "0", "0", ')'] ;
Ls = ['(', "IFF", "0", "1", ')'] ;
Ls = ['(', "IFF", "0", "2", ')'] ;
?- nth0(1, Ls, "XOR"), phrase(expression, Ls).
Ls = ['(', "XOR", "0", "0", ')'] ;
Ls = ['(', "XOR", "0", "1", ')'] ;
Ls = ['(', "XOR", "0", "2", ')']
?- nth0(1, Ls, "NOT"), phrase(expression, Ls).
Ls = ['(', "NOT", "0", ')'] ;
Ls = ['(', "NOT", "1", ')'] ;
Ls = ['(', "NOT", "2", ')']
在生成版本中,使用的一些谓词是 swi-prolog
内置的,因此它们不能与其他实现一起使用。
A swi-prolog 内置 char_type/2
也将作为 alpha_numeric char_type(C, alnum)
工作。以下是使用swi-prolog
谓词的dcg风格代码。
tok -->
[A],
{ string_codes(A, AC),
maplist([C]>>char_type(C, alnum), AC)
}.
我正在尝试为编码为 S 表达式的命题演算编写解析器。
我取得了一些进步:
expression --> op.
op --> ['('], bin-op, bool, bool, [')'].
op --> ['('], unary-op, bool, [')'].
bool --> tok.
bool --> op.
bin-op --> ["IFF"].
bin-op --> ["IF"].
bin-op --> ["XOR"].
unary-op --> ["NOT"].
tok --> ["a"].
在 swipl
中,我通过调用 phrase
:
?- phrase(expression, Ls).
Ls = ['(', "IFF", "a", "a", ')']
然而,这仅适用于 tok
“a”。有没有办法在 DCG 中说“tok 是任何字母数字字符串”?我找到了 this,但我不确定如何将它应用到我正在做的事情中。
如果你只想解析,那么下面的token就可以了。
tok([A|B], B) :- an_code(A).
alpha_numeric(X) :-
between(0'0, 0'9, X); between(0'A, 0'Z, X); between(0'a, 0'z, X).
an_code(A) :- atom_codes(A, C), maplist(alpha_numeric, C).
?- phrase(expression, ['(', "IFF", "A1", "1A", ')']).
true
?- phrase(expression, ['(', "IFF", ".A1", "1A", ')']).
false.
?- phrase(expression, ['(', "IFF", ".A1", "(1A", ')']).
false.
使用 an_code 如下所示,您也可以生成公式:
an_code(A) :- var(A) ->
length(C,L), L >= 1,
maplist(alpha_numeric, C),
string_codes(A, C);
atom_codes(A, C), maplist(alpha_numeric, C).
?- phrase(expression, Ls).
Ls = ['(', "IFF", "0", "0", ')'] ;
Ls = ['(', "IFF", "0", "1", ')'] ;
Ls = ['(', "IFF", "0", "2", ')'] ;
?- nth0(1, Ls, "XOR"), phrase(expression, Ls).
Ls = ['(', "XOR", "0", "0", ')'] ;
Ls = ['(', "XOR", "0", "1", ')'] ;
Ls = ['(', "XOR", "0", "2", ')']
?- nth0(1, Ls, "NOT"), phrase(expression, Ls).
Ls = ['(', "NOT", "0", ')'] ;
Ls = ['(', "NOT", "1", ')'] ;
Ls = ['(', "NOT", "2", ')']
在生成版本中,使用的一些谓词是 swi-prolog
内置的,因此它们不能与其他实现一起使用。
A swi-prolog 内置 char_type/2
也将作为 alpha_numeric char_type(C, alnum)
工作。以下是使用swi-prolog
谓词的dcg风格代码。
tok -->
[A],
{ string_codes(A, AC),
maplist([C]>>char_type(C, alnum), AC)
}.