Prolog Language Definite Clause Grammars (DCGs) Parsing with DCGs


Example

DCGs can be used for parsing. Best of all, the same DCG can often be used to both parse and generate lists that are being described. For example:

sentence --> article, subject, verb, object.

article --> [the].

subject --> [woman] | [man].

verb --> [likes] | [enjoys].

object --> [apples] | [oranges].

Example queries:

?- phrase(sentence, Ls).
Ls = [the, woman, likes, apples] ;
Ls = [the, woman, likes, oranges] ;
Ls = [the, woman, enjoys, apples] .

?- phrase(sentence, [the,man,likes,apples]).
true .