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>*)
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>*)
is the same symbol as the one naming the defgrammar
<other grammar name>
is a symbol naming another defgrammar
is an atom, which forms the words of the sentence to be matched
is a variable reference
is any Common Prolog logic expression, including a variable
<lisp function name>
is a symbol naming a Lisp function
is any Lisp form, which is evaluated and passed to the function
, extra arguments can be added by specifying
must specify the same
form and have the same number of extra arguments.
The meaning of the various
> 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.