CS 214: Programming Languages
Spring 2009

Home|Syllabus|Schedule
<<|>>|Prolog templates

Definite Clause Grammars
Prolog, Iteration 7

Work through Chapter 7 of Learn Prolog Now! (LNP!).

Testing a DCG

SWI-Prolog does have a built-in DCG library which allows you to write grammars as in section 7.2. To test the grammars, use the same names as difference-list predicates.

Here's the tests that I wrote for Exercise 7.2:

:- begin_tests(ex7_2).

test(s2, [fail]) :- s2([], []).
test(s2)         :- s2([a, b], []), !.
test(s2, [fail]) :- s2([a, a, b], []).
test(s2, [fail]) :- s2([b, a], []).
test(s2)         :- s2([a, a, a, a, b, b, b, b], []), !.
test(s2, [fail]) :- s2([a, a, b, b, b, b], []).
test(s2, [fail]) :- s2([a, a, a, a, b], []).
test(s2, [fail]) :- s2([a, a, a, a, c, b, b, b, b], []).

:- end_tests(ex7_2).

s2 is the name of the "sentence terminal" for the grammar. (s was already taken by my solution for Exercise 7.1, and to avoid naming conflicts when running the tests, I used s2 here.)