Each grammar rule is translated into a Prolog clause as it is consulted or compiled. This DCG term expansion is as follows:
A DCG rule such as:
p(X) -> q(X). |
will be translated (expanded) into the Prolog rule:
p(X, Li, Lo) :- |
If there is more than one non-terminal on the right-hand side, as in
p(X, Y) -> q(X), r(X, Y), s(Y). |
the corresponding input and output arguments are identified, translating into:
p(X, Y, Li, Lo) :- |
Terminals are translated using the built-in predicate 'C'/3 (See section 8.3 for its description). For instance:
p(X) -> [go, to], q(X), [stop]. |
is translated into:
p(X, S0, S) :- |
Extra conditions expressed as explicit procedure calls naturally translate into themselves. For example,
positive_number(X) -> |
translates to:
positive_number(X, Li, Lo) :- |
Similarly, a cut is translated literally.
Push-back lists (a proper list of terminals on the left-hand side of a DCG rule) translate into a sequence of 'C'/3 goals with the first and third arguments reversed. For example,
it_is(X), [is, not] -> [aint]. |
becomes
it_is(X, Li, Lo) :- |
Disjunction has a fairly obvious translation. For example, the DCG clause:
expr(E) -> |
translates to the Prolog rule:
expr(E, Li, Lo) :- |