Definite Clause Grammar - Parsing With DCGs

Parsing With DCGs

The main practical use of a DCG is to parse sentences of the given grammar, i.e. to construct a parse tree. This can be done by providing "extra arguments" to the functors in the DCG, like in the following rules:

sentence(s(NP,VP)) --> noun_phrase(NP), verb_phrase(VP). noun_phrase(np(D,N)) --> det(D), noun(N). verb_phrase(vp(V,NP)) --> verb(V), noun_phrase(NP). det(d(the)) --> . det(d(a)) --> . noun(n(bat)) --> . noun(n(cat)) --> . verb(v(eats)) --> .

One can now query the interpreter to yield a parse tree of any given sentence:

| ?- sentence(Parse_tree, ). Parse_tree = s(np(d(the),n(bat)),vp(v(eats),np(d(a),n(cat)))) ? ;

Read more about this topic:  Definite Clause Grammar