Re: DCG

Anne Ogborn <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
Thanks, Richard - the paper you reference says '
	1. Find all of the strings in L that start with c.
 ' as if that's trivial.

Here's a broken example. This runs forever because it tries appending first one, then two, then three, unbound
variables out to infinity.

I'm looking for a solution for any definition of sentence. I'd take a solution for any definition of
sentence over some known alphabet. The example grammar has a finite maximum length, that's just
this example.

% next_token(+Prefix, -NextList)
% Prefix is a list and NextList is the potential next tokens
% in sentence.
% So next_token([give], [a, the, ball, toy, tom, helen]).
%
% This runs forever
next_token(Prefix, NextList) :-
setof(Next, a_next_token(Prefix, Next), NextList).

a_next_token(Prefix, Next) :-
vocabulary(V),
member(Next, V),
append([Prefix, [Next], _], ANextList),
phrase(sentence, ANextList).

% imperative sentence of questionable semantics
sentence -->  imperative_sentence.

imperative_sentence -->
intransitive_verb,
object_phrase.
imperative_sentence -->
transitive_verb(V),
object_phrase,
matching_preposition(V),
object_phrase.


intransitive_verb -->
[X],
{
    intransitive_verbs(List),
    memberchk(X, List)
}.

intransitive_verbs([drink,eat]).

transitive_verb(X) -->
[X],
{
    transitive_verbs(List),
    memberchk(X, List)
}.

transitive_verbs([give,take,throw,make]).

matching_preposition(V) -->
[Prep],
{
    memberchk(V/Prep, [give/to, take/from, throw/to, make/for])
}.

object_phrase --> object.
object_phrase --> [a], object.
object_phrase --> [the], object.

object --> [ball].
object --> [toy].
object --> [tom].
object --> [helen].

vocabulary([ball,toy,tom,helen,a,the,give,to,take,from,throw,make,for,eat,drink]).
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.