[Previous] [Up] [Next]
Go backward to Definite Clause Grammars
Go up to Difference Lists and Definite Clause Grammars
Go forward to Exercises

Prolog Samples

A DCG for a fragment of English contains a sample DCG, which is also available as ~avk/Temp/dcg1.pl (try it out and extend it). A Prolog Listing of the DCG shows the Prolog procedures which are loaded into the interpreter as a result of loading the file. This is what you see if you type listing. Finally, A Trace of the DCG shows a trace of the DCG when trying to prove that the sentence every cat chases a dog is grammatical.

Note how the clause corresponding to the distinguished symbol s is called--since Prolog only knows about Prolog facts, and the s rule hence has arity two, you must write your query as a goal with arity two also.

You can use the grammar in dcg1.pl to randomly generate sentences by providing a variable as the first argument to s:

| ?- s(Sentence, []).
Sentence = [every,dog,chases,every,dog] ? ;
Sentence = [every,dog,chases,every,cat] ? ;
Sentence = [every,dog,chases,a,dog] ? ;
Sentence = [every,dog,chases,a,cat] ? ;
...

A DCG for a Fragment of English

s --> np,vp. 
 
np --> det, n. 
np --> pn. 
 
vp --> vt, np. 
vp --> vi. 
 
det --> [every]. 
det --> [a]. 
 
vt --> [chases]. 
vi --> [miaows]. 
 
n --> [dog]. 
n --> [cat]. 
 
pn --> [fido]. 
pn --> [tigger].

A Prolog Listing of the DCG

s(A, B) :-
        np(A, C),
        vp(C, B).
 
pn(A, B) :-
        'C'(A, fido, B).
pn(A, B) :-
        'C'(A, tigger, B).
 
n(A, B) :-
        'C'(A, dog, B).
n(A, B) :-
        'C'(A, cat, B).
 
vi(A, B) :-
        'C'(A, miaows, B).
 
vt(A, B) :-
        'C'(A, chases, B).
 
det(A, B) :-
        'C'(A, every, B).
det(A, B) :-
        'C'(A, a, B).
 
vp(A, B) :-
        vt(A, C),
        np(C, B).
vp(A, B) :-
        vi(A, B).
 
np(A, B) :-
        det(A, C),
        n(C, B).
np(A, B) :-
        pn(A, B).
 

A Trace of the DCG

| ?- trace, s([every,cat,chases,a,dog],[]).
{The debugger will first creep -- showing everything (trace)}
   1  1  Call: s([every,cat,chases,a,dog],[]) ? 
   2  2  Call: np([every,cat,chases,a,dog],_701) ? 
   3  3  Call: det([every,cat,chases,a,dog],_922) ? 
   4  4  Call: C([every,cat,chases,a,dog],every,_922) ? 
   4  4  Exit: C([every,cat,chases,a,dog],every,[cat,chases,a,dog]) ? 
   3  3  Exit: det([every,cat,chases,a,dog],[cat,chases,a,dog]) ? 
   5  3  Call: n([cat,chases,a,dog],_701) ? 
   6  4  Call: C([cat,chases,a,dog],dog,_701) ? 
   6  4  Fail: C([cat,chases,a,dog],dog,_701) ? 
   6  4  Call: C([cat,chases,a,dog],cat,_701) ? 
   6  4  Exit: C([cat,chases,a,dog],cat,[chases,a,dog]) ? 
   5  3  Exit: n([cat,chases,a,dog],[chases,a,dog]) ? 
   2  2  Exit: np([every,cat,chases,a,dog],[chases,a,dog]) ? 
   7  2  Call: vp([chases,a,dog],[]) ? 
   8  3  Call: vt([chases,a,dog],_2894) ? 
   9  4  Call: C([chases,a,dog],chases,_2894) ? 
   9  4  Exit: C([chases,a,dog],chases,[a,dog]) ? 
   8  3  Exit: vt([chases,a,dog],[a,dog]) ? 
   10  3  Call: np([a,dog],[]) ? 
   11  4  Call: det([a,dog],_3889) ? 
   12  5  Call: C([a,dog],every,_3889) ? 
   12  5  Fail: C([a,dog],every,_3889) ? 
   12  5  Call: C([a,dog],a,_3889) ? 
   12  5  Exit: C([a,dog],a,[dog]) ? 
   11  4  Exit: det([a,dog],[dog]) ? 
   13  4  Call: n([dog],[]) ? 
   14  5  Call: C([dog],dog,[]) ? 
   14  5  Exit: C([dog],dog,[]) ? 
   13  4  Exit: n([dog],[]) ? 
   10  3  Exit: np([a,dog],[]) ? 
   7  2  Exit: vp([chases,a,dog],[]) ? 
   1  1  Exit: s([every,cat,chases,a,dog],[]) ? 
 
yes
{trace}

<a.von.klopp@bangor.ac.uk>

[Previous] [Up] [Next]