Definite Clause Grammars

These tests verify functionalities in the DCGLibrary provided with the tuProlog distribution. The typical interface predicates exposed by DCG are phrase/2 and phrase/3.

1. phrase/2

phrase(Category, List) is true iff the list List can be parsed as a phrase (i.e. sequence of terminals) of type Category. Category can be any term which would be accepted as a non-terminal of the grammar (or, in general, can be any grammar rule body) and must be instantiated to a non-variable term at the time of the call. If List is bound to a list of terminals by the time of the call, then the goal corresponds to parsing List as a phrase of type Category; otherwise, if List is unbound, then the grammar is being used for generation.

Templates and modes for the predicate are as follows:

phrase(+term, ?list)

1.1 Example tests

Let's run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture
enter query load_library('alice.tuprolog.lib.DCGLibrary').
check hasSolution true

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter theory a --> [].
enter query phrase(a, []).
check hasSolution true
enter theory a --> [a], b.
b --> [].
enter query phrase(a, [a]).
check hasSolution true
enter query phrase(b, []).
check hasSolution true
enter theory a --> [a], a, [d].
a --> [b], a, [d].
a --> [c].
enter query phrase(a, [a, b, a, a, c, d, d, d, d]).
check hasSolution true
enter theory x(V) --> [a], x(V), [a].
x(V) --> [V].
enter query phrase(x(1), [a, a, a, 1, a, a, a]).
check hasSolution true
enter theory x --> [a], x, [a].
x --> [V], { number(V) }.
enter query phrase(x, [a, a, a, 151, a, a, a]).
check hasSolution true
enter theory e --> o, et.
et --> [].
et --> ['+'], e.
o --> ['('], e, [')'].
o --> [X], { number(X) }.
enter query phrase(e, [1, '+', '(', 2, '+', 3, ')', '+', 4]).
check hasSolution true
enter theory e(V) --> o(V1), et(V1, V).
et(V, V) --> [].
et(VI, VO) --> ['+'], o(V1), { VI1 is VI + V1 }, et(VI1, VO).
o(V) --> ['('], e(V), [')'].
o(X) --> [X], { number(X) }.
enter query phrase(e(V), [1, '+', '(', 2, '+', 3, ')']).
check hasSolution true
enter variable V
check binding 6
enter theory e --> t, et.
et --> [].
et --> [and], e.
t --> ['0'].
t --> ['1'].
enter query phrase(e, ['0']).
check hasSolution true
enter theory e(V) --> t(W), et(W, V).
et(V, V) --> [].
et(W, V) --> [and], t(V1), { W = 1, V1 = 1, !, W2 = 1 ; W2 = 0 }, et(W2, V).
t(0) --> ['0'].
t(1) --> ['1'].
enter query phrase(e(V), ['1']).
check hasSolution true
enter variable V
check binding 1

Another candidate for similar tests is phrase/3.

Run the tests!


The results of the tests for Definite Clause Grammars are as follows:

fit.Summary