Subject: Re: [SWIPL] SWI-Prolog - Definite Clause Grammar

From: "Richard A. O'Keefe" <[email-masked]>
Date: Wed, 17 Oct 2001 13:18:22 +1300 (NZDT)

"Dichev, Christo" <[email-masked]> wrote:
	Maybe "phrase/2" or "phrase/3" are provided for that purpose,
	but I don't know how to use them.

phrase/[2,3] are to DCGs are call/1 is to normal predicates.

phrase(Form, Sequence) :-
    'expand as if it were a DCG body'(Form, S0, S, Expansion),
    S0 = Sequence, S = [],
    call(Expansion).

phrase(Form, Seq0, Seq) :-
    'expand as if it were a DCG body'(Form, S0, S, Expansion),
    S0 = Seq0, S = Seq,
    call(Expansion).

The purpose of phrase/2 is to be an interface between DCG rules and
normal clauses.  For example,

    solve :-
        read_sentence(Problem),
        phrase(sentence(Interpretation), Problem, []),
        solve_interpretation(Interpretation).

    sentence(Interpretation) --> ....

The purpose of phrase/3 is to provide an analogue of call/1 that
may be used in the body of a grammar rule.

For example,

    :- op(10, xf, *).

    X* --> [] ; X, X*.

Some DCG expanders will handle this, some will not.
The ones that do handle it should handle it as if it read

    X* --> [] ; phrase(X), X*.

How do I know that?  Because that's what I invented phrase/3 for.

	Is it possible to express in SWI-Prolog grammar rules in the form:
	
		s --> [a], [b].
		s --> [a], s, [b].
	
Yes.