All Manuals > KnowledgeWorks and Prolog User Guide > A Common Prolog

NextPrevUpTopContentsIndex

A.9 Defining Definite Clause Grammars

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 .

The relation has the form

(<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

<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

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:

A.9.1 Examples


KnowledgeWorks and Prolog User Guide (Macintosh version) - 6 Dec 2011

NextPrevUpTopContentsIndex