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 .
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)
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.
Goal | Theory | success(String goal,String theory) |
---|---|---|
load_library('alice.tuprolog.lib.DCGLibrary'). | null | true |
Then, ask the engine to solve a query, and check variable bindings.
Goal | Theory | success(String goal,String theory) |
---|---|---|
phrase(a, []). | a --> []. | true |
phrase(a, [a]). | a --> [a], b. b --> []. | true |
phrase(b, []). | a --> [a], b. b --> []. | true |
phrase(a, [a, b, a, a, c, d, d, d, d]). | a --> [a], a, [d]. a --> [b], a, [d]. a --> [c]. | true |
phrase(x(1), [a, a, a, 1, a, a, a]). | x(V) --> [a], x(V), [a]. x(V) --> [V]. | true |
phrase(x, [a, a, a, 151, a, a, a]). | x --> [a], x, [a]. x --> [V], { number(V) }. | true |
phrase(e, [1, '+', '(', 2, '+', 3, ')', '+', 4]). | e --> o, et. et --> []. et --> ['+'], e. o --> ['('], e, [')']. o --> [X], { number(X) }. | true |
phrase(e, ['0']). | e --> t, et. et --> []. et --> [and], e. t --> ['0']. t --> ['1']. | true |
Goal | Theory | Variable | Solution | success(String goal,String theory,String variable,Strng solution) |
---|---|---|---|---|
phrase(e(V), [1, '+', '(', 2, '+', 3, ')']). | 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) }. | V | 6 | true |
phrase(e(V), ['1']). | 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']. | V | 1 | true |