Search code examples
prologpattern-matchinggrammarcontext-free-grammar

Matching part of a DFG in Prolog


I have defined a DFG grammar in Prolog which is something like this:

start --> subject, verb, object.
subject --> ([i]; [you]).
verb --> ([like]; [need]).
object --> article, noun.
article --> ([my];[your]).
noun --> ([car] ; [bike]).

Now, I would like to have a predicate that returned me the object part of a phrase accepted by this DFG.

For example, objectPart([i, like, my, car], X) should return X = [my, car].

How can I do this?


Solution

  • This can be done:

    start(O) --> subject, verb, my_object(O).
    subject --> ([i]; [you]).
    verb --> ([like]; [need]).
    my_object(L) --> article(A), noun(N), {L = [A,N]}.
    article(A) --> ([my],{A=my};[your],{A=your}).
    noun(N) --> ([car],{N=car} ; [bike],{N=bike}).
    
    objectPart(Lst, R) :-
        phrase(start(R), Lst).
    

    Result :

    ?- objectPart([i, like, your, car], Z).
    Z = [your, car] .
    

    EDIT I change object in my_object because SWI-Prolog use object for XPCE.