Scryer Prolog

Module dcgs

:- use_module(library(dcgs)).

Support for Definite Clause Grammars.

A Prolog definite clause grammar (DCG) describes a sequence. Operationally, DCGs can be used to parse, generate, complete and check sequences manifested as lists.

Check The Power of Prolog chapter on DCGs to learn more about them.

phrase(+Body, ?Ls).

True iff Body describes the list Ls. Body must be a DCG body. It is equivalent to phrase(Body, Ls, []).

Examples:


as --> [].
as --> [a], as.

?- phrase(as, Ls).
   Ls = []
;  Ls = "a"
;  Ls = "aa"
;  Ls = "aaa"
;  ... .

?- phrase(as, "aaa").
   true.

phrase(+Body, ?Ls, ?Ls0).

True iff Body describes part of the list Ls and the rest of Ls is Ls0.

Example:


?- phrase(seq(X), "aaa", Y).
   X = [], Y = "aaa"
;  X = "a", Y = "aa"
;  X = "aa", Y = "a"
;  X = "aaa", Y = [].

seq(Seq)//

Describes a sequence

seqq(SeqOfSeqs)//

Describes a sequence of sequences

...//

Describes an arbitrary number of elements

phrase/4

phrase/5

(-->)/2