/* DCG capture in Picat. This is a module for capturing substrings in a DCG. It is based on false's solution of my question in https://stackoverflow.com/questions/73954102/capture-matching-substrings-from-a-dcg/73956401#73956401 Using phrase_capture/2 in a DCG makes it possible to - capture substrings - check a given string - generating all solutions (and terminate) Usage: jaco_bassist([Name,WasIs,Adj,Noun]) --> phrase_capture( (("J" ; "j"), "aco"), Name), space, phrase_capture( ("was" ; "is"), WasIs), space, ("a" ; "an"), space, phrase_capture( ("American" ; "famous"), Adj), space, phrase_capture( ("dancer" ; "bassist" ; "singer" ; "programmer" ; "dueller"), Noun). space --> " ". % Checking a string go => jaco_bassist([Name,WasIs,Adj,Noun],"Jaco was an American bassist",[]), println([Name,WasIs,Adj,Noun]), nl. % Generate all solutions and capture substrings go2 => jaco_bassist([Name,WasIs,Adj,Noun],String,[]), println(String), println([Name,WasIs,Adj,Noun]), nl, fail. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ module dcg_capture. % This is from % https://stackoverflow.com/questions/65354457/pure-append-3-for-mode-that-wouldnt-leave-a-choice-point/65361752#65361752 % The two fixes (using -> ; or if/then/else) works but they do leave a choice point which if_/3 does not, if I understood it % correctly. append2u(Xs, Ys, Zs) :- %% This is the original, but reqiuires the reif module (in SWI-Prolog etc) % if_(Ys = Zs, % Xs = [], % ( Xs = [X|Xs1], Zs = [X|Zs1], append2u(Xs1, Ys, Zs1) ) % ). % This works: (Ys == Zs -> Xs = [] ; Xs = [X|Xs1], Zs = [X|Zs1], append2u(Xs1, Ys, Zs1) ). % As does this: % if Ys == Zs then % Xs = [] % else % Xs = [X|Xs1], % Zs = [X|Zs1], % append2u(Xs1, Ys, Zs1) % end. % This is from from false's answer of my question % question https://stackoverflow.com/questions/73954102/capture-matching-substrings-from-a-dcg phrase_capture(NT__0, Capture, S0,S) :- phrase(NT__0, S0,S1), append2u(Capture, S1, S0), S1 = S. phrase(A,B,C) :- bp.phrase(A,B,C).