在prolog中实现Reg.表达式



我有一个关于在prolog中实现一些谓词的问题。(用于正则表达式。(我需要实现4个谓词

我的实现是

sublist( Sublist, List ) :- append( [_, Sublist, _], List ). %true if SubList is a sublist of a List
match(one(List),W) :- sublist(List,W). %true if there is a single occurence of List
match(opt(_),[]).  %true if there are 0 or 1 occurences of List
match(opt(List),W) :- sublist(List,W).
match(star(_),[]).  %true if there are 0 or more occurences of List
%more occurences
match(plus(List),W) :- sublist(List,W). %true if there are 1 or more occurences of List
%more occurences

现在,我的问题是。

  1. 我不知道如何实现更多的发生
  2. 例如,如果我想把它称为match([这里会有更多的表达式],S(length(S,3),match([star[1],opt[2,3]],S)这样行吗

EDIT:
2(问题很容易解决,我只是在列表中迭代。

您的"匹配(一个(…"可以使用:

exactly_one_solution(Template, Goal) :-
% Check whether more than 1 solution exists
bagof(Template, limit(2, Goal), Solutions),
Solutions = [Template].

和你的";match(plus(…"(可以使用标准Prologonce/1,以确保零个以上的匹配。

最新更新