定从句文法 (dcg) Prolog(作业)
definite clause grammar (dcg) Prolog (homework)
我试着写一个谓词 N={Expression,Number,Digit,Operator,Variable}
T={1,2,3,+,-,*,(,),X,Y,Z}
并且 S 是表达式,程序 p 定义为
Expression-->Number
**Expression-->(Expression) Operator (Expression)**
Number-->Digit
**Number --> Digit Number**
Digit-->1
Digit-->2
Digit-->3
Operator-->+
Operator-->-
Operator-->*
Variable-->X
Variable-->Y
Variable-->Z
我认为我实现了很多部分但是无法实现粗体部分!!
我的序言代码通过依赖程序 P:
描述终端
expression(S,S).
expression(S,R):-number(S,R).
expression(S,R):-expression(S,R),-operator(S,['('|S]),expression(S,[')'|R]).
expression(S,R):-operator(S,[','|S1]),expression(S1,R).
expression(S,R):-variable(S,[','|S1]),expression(S1,R).
number(S,R):-digit(S,R).
digit(['1'|R],R).
digit(['2'|R],R).
digit(['3'|R],R).
operator(['+'|R],R).
operator(['-'|R],R).
operator(['*'|R],R).
variable(['X'|R],R).
variable(['Y'|R],R).
variable(['Z'|R],R).
请帮助我。
这可能是您所指的dcg:
:- set_prolog_flag(double_quotes, chars).
expression --> number.
expression --> variable.
expression --> "(", expression, operator, expression, ")".
number --> digit.
number --> digit, number.
digit --> "1".
digit --> "2".
digit --> "3".
operator --> "+"|"-"|"*". % more compact notation
variable --> "X"|"Y"|"Z".
像这样使用它来生成按长度排序的所有句子:
?- length(L, N), phrase(expression, L).
L = ['1'],
N = 1 ;
L = ['2'],
N = 1 ;
L = ['3'],
N = 1 ;
L = ['X'],
N = 1 ;
L = ['Y'],
N = 1 ;
L = ['Z'],
N = 1 ;
L = ['1', '1'],
N = 2 ;
L = ['1', '2'],
N = 2 ...
要获得最简洁易读的答案,请参阅 this 了解更多信息。即下载以下模块:
?- use_module(double_quotes).
?- length(L, N), phrase(expression, L).
L = "1",
N = 1 ;
L = "2",
N = 1 ;
L = "3",
N = 1 ;
L = "X",
N = 1 ;
L = "Y",
N = 1 ;
L = "Z",
N = 1 ;
L = "11",
N = 2 ;
L = "12",
N = 2
要查看 DCG 是如何实现的,请为每个非终端说 listing
。例如:
?- listing(expression).
expression(A, B) :-
number(A, B).
expression(A, B) :-
variable(A, B).
expression(['('|A], E) :-
expression(A, B),
operator(B, C),
expression(C, D),
D=[')'|E].
我试着写一个谓词 N={Expression,Number,Digit,Operator,Variable}
T={1,2,3,+,-,*,(,),X,Y,Z}
并且 S 是表达式,程序 p 定义为
Expression-->Number
**Expression-->(Expression) Operator (Expression)**
Number-->Digit
**Number --> Digit Number**
Digit-->1
Digit-->2
Digit-->3
Operator-->+
Operator-->-
Operator-->*
Variable-->X
Variable-->Y
Variable-->Z
我认为我实现了很多部分但是无法实现粗体部分!!
我的序言代码通过依赖程序 P:
描述终端 expression(S,S).
expression(S,R):-number(S,R).
expression(S,R):-expression(S,R),-operator(S,['('|S]),expression(S,[')'|R]).
expression(S,R):-operator(S,[','|S1]),expression(S1,R).
expression(S,R):-variable(S,[','|S1]),expression(S1,R).
number(S,R):-digit(S,R).
digit(['1'|R],R).
digit(['2'|R],R).
digit(['3'|R],R).
operator(['+'|R],R).
operator(['-'|R],R).
operator(['*'|R],R).
variable(['X'|R],R).
variable(['Y'|R],R).
variable(['Z'|R],R).
请帮助我。
这可能是您所指的dcg:
:- set_prolog_flag(double_quotes, chars).
expression --> number.
expression --> variable.
expression --> "(", expression, operator, expression, ")".
number --> digit.
number --> digit, number.
digit --> "1".
digit --> "2".
digit --> "3".
operator --> "+"|"-"|"*". % more compact notation
variable --> "X"|"Y"|"Z".
像这样使用它来生成按长度排序的所有句子:
?- length(L, N), phrase(expression, L).
L = ['1'],
N = 1 ;
L = ['2'],
N = 1 ;
L = ['3'],
N = 1 ;
L = ['X'],
N = 1 ;
L = ['Y'],
N = 1 ;
L = ['Z'],
N = 1 ;
L = ['1', '1'],
N = 2 ;
L = ['1', '2'],
N = 2 ...
要获得最简洁易读的答案,请参阅 this 了解更多信息。即下载以下模块:
?- use_module(double_quotes).
?- length(L, N), phrase(expression, L).
L = "1",
N = 1 ;
L = "2",
N = 1 ;
L = "3",
N = 1 ;
L = "X",
N = 1 ;
L = "Y",
N = 1 ;
L = "Z",
N = 1 ;
L = "11",
N = 2 ;
L = "12",
N = 2
要查看 DCG 是如何实现的,请为每个非终端说 listing
。例如:
?- listing(expression).
expression(A, B) :-
number(A, B).
expression(A, B) :-
variable(A, B).
expression(['('|A], E) :-
expression(A, B),
operator(B, C),
expression(C, D),
D=[')'|E].