The following is a simple grammar for which Prolog clauses are given. Levent Yilmaz provided this sample by simplifying a program he wrote for a Natural Language processing course. This simple grammar covers a few very basic sentences, like "john ran" and "john threw the books".
The Prolog program uses a clause that tokenizes a given sentence. The name of this clause is "convert". The code for "convert" has not been included to keep the program simple and to emphasize how BNF rules can be described in terms of clauses.
Here is the grammar:
Rule 1: <SENTENCE> --> <NP><VP>
Rule 2: <VP> --> <V>
Rule 3: <VP> --> <V><NP>
Rule 4: <NP> --> <N>
Rule 5: <NP> --> <DET> <N>
Rule 6: <NP> --> <ADJ> <N>
Rule 7: <DET> --> the | a
Rule 8: <V> --> threw | ran
Rule 9: <N> --> john | books
Rule 10 <ADJ> --> old | small | nice
Now here is the equivalent Prolog program, where each one of the rules above has been translated into a clause:
start:-
write('Enter a sentence: '),
read_chars(S),
convert(S,L), /* This clause is assumed to tokenize the list */
reverse(L,L1),
sentence(L1).
sentence(X) :-
append(Y,Z,X),
noun_phrase(Y),
write('NP is '), write(Y), nl,
verb_phrase(Z),
write('VP is '), write(Z), nl.
noun_phrase(X) :-
noun(X).
noun_phrase(X):-
append(Y,Z,X),
determiner(Y),
noun_phrase(Z).
noun_phrase(X):-
append(Y,Z,X),
adjective(Y),
noun_phrase(Z).
verb_phrase(X):-
append(Y,Z,X),
verb(Y),
noun_phrase(Z).
verb_phrase(X):-
verb(X).
prep_phrase(X):-
append(Y,Z,X),
prep(Y),
noun_phrase(Z).
determiner(["the"]).
determiner(["a"]).
adjective(["old"]).
adjective(["small"]).
adjective(["nice"]).
noun(["john"]).
noun(["books"]).
verb(["threw"]).
verb(["ran"]).
Another item to note is the way the code is formatted. It is common to see Prolog programs written with the head of the clause on one line, and then each antecedent term by itself on a separate line. Not only does this make it easy to see what is going on, but it also makes it easy to either comment out some terms (just place a % at the beginning of that line), or insert write() terms for debugging purposes.