next up previous contents index
Next: Database Up: Built-in predicates Previous: The `block' control-structure   Contents   Index

DCG Grammar rules

Grammar rules form a comfortable interface to difference-lists. They are designed both to support writing parsers that build a parse-tree from a list as for generating a flat list from a term. Unfortunately, Definite Clause Grammar (DCG) handling is not part of the Prolog standard. Most Prolog engines implement DCG, but the details differ slightly.

Grammar rules look like ordinary clauses using 2 for separating the head and body rather then 2. Expanding grammar rules is done by expand_term2, which adds two additional argument to each term for representing the difference list. We will illustrate the behaviour by defining a rule-set for parsing an integer.


\begin{code}
integer(I) -->
digit(D0),
digits(D),
{ number_chars(I, [D0\vert ...
....
digits([]) -->
.
\par digit(D) -->
[D],
{ code_type(D, digit)
}.
\end{code}

The body of a grammar rule can contain three types of terms. A compound term interpreted as a reference to a grammar-rule. Code between {...} is interpreted as a reference to ordinary Prolog code and finally, a list is interpreted as a sequence of literals. The Prolog control-constructs (1, 2, /2, 2 and 0) can be used in grammar rules.

Grammar rule-sets are called using the builtin predicates phrase2 and phrase3:

phrase2+RuleSet, +InputList Equivalent to phrase(RuleSet, InputList, []). phrase3+RuleSet, +InputList, -Rest Activate the rule-set with given name. `InputList' is the list of tokens to parse, `Rest' is unified with the remaining tokens if the sentence is parsed correctly. The example below calls the rule-set `integer' defined above.


\begin{code}
?- phrase(integer(X), ''42 times'', Rest).
\par X = 42
Rest = [32, 116, 105, 109, 101, 115]
\end{code}


next up previous contents index
Next: Database Up: Built-in predicates Previous: The `block' control-structure   Contents   Index
Dr. Richard Botting 2001-12-12