Scryer Prolog documentation

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, []).


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

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

?- phrase(as, "aaa").

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

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


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


Describes a sequence


Describes a sequence of sequences


Describes an arbitrary number of elements