defgrammar macro can be used to define a definite clause grammar (DCG), which is a relation that determines whether the start of a list of tokens (a
) matches a particular grammar. The remaining tokens in the list become the
The relation has the form
(<grammar name> <sentence> <sentence tail> <extra argument>*)
<extra argument> items are terms defined below.
The syntax of the defgrammar macro is
(defgrammar <grammar name>
<rule> ::= (<lhs> <rhs>*)
<lhs> ::= <grammar name>
| (<grammar name> <term>*)
<rhs> ::= <atom>
| (<other grammar name> <term>*)
| <lisp clause>
| (call <term>)
<lisp clause> ::= (<non-atomic lisp form> <term>*)
<non-atomic lisp form> ::= (<lisp function name> <lisp arg>*)
<grammar name> is the same symbol as the one naming the defgrammar
<other grammar name> is a symbol naming another defgrammar
<atom> is an atom, which forms the words of the sentence to be matched
<var> is a variable reference
<term> is any Common Prolog logic expression, including a variable
<lisp function name> is a symbol naming a Lisp function
<lisp arg> is any Lisp form, which is evaluated and passed to the function
<lhs>, extra arguments can be added by specifying
<rule> must specify the same
<grammar name> as the
defgrammar form and have the same number of extra arguments.
The meaning of the various
<rhs> items is as follows:
<atom>matches that atom in the sentence
<var>is unified with the next item in the sentence
(<other grammar name> <term>*)calls the grammar relation
<other grammar name>on the rest of the sentence. The optional
<term>arguments are passed to the relation as its extra arguments.
<lisp clause>evaluates the
<non-atomic lisp form>as a Lisp form and unifies the values that it returns with the
<term>s that follow it.
<term>as a normal Prolog relation.
(cut)calls the normal Prolog cut relation.
KnowledgeWorks and Prolog User Guide (Macintosh version) - 24 Mar 2017