




The 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
sentence
) matches a particular grammar. The remaining tokens in the list become the
sentence tail
.
(<grammar name> <sentence> <sentence tail> <extra argument>*)
where the <extra argument> items are terms defined below.
The syntax of the defgrammar macro is
(defgrammar <grammar name>
<rule>*)
<rule> ::= (<lhs> <rhs>*)
<lhs> ::= <grammar name>
| (<grammar name> <term>*)
<rhs> ::= <atom>
| <var>
| (<other grammar name> <term>*)
| <lisp clause>
| (call <term>)
| (cut)
<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
<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
Within the <lhs>, extra arguments can be added by specifying <term>s. Every <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.(call <term>) calls <term> as a normal Prolog relation.(cut) calls the normal Prolog cut relation.KnowledgeWorks and Prolog User Guide (Unix version) - 24 Mar 2017