Next: , Previous: State variables, Up: Syntax   [Contents]

2.13 DCG-rules

Definite Clause Grammar notation is intended for writing parsers and sequence generators in a particular style; in the past it has also been used to thread an implicit state variable, typically the I/O state, through code. As a matter of style, we recommend that in future DCG notation be reserved for writing parsers and sequence generators, and that state variable syntax be used for passing state threads.

DCG-rules in Mercury have identical syntax and semantics to DCG-rules in Prolog.

A DCG-rule is an item of the form ‘Head --> Body’. The Head term must not be a variable. A DCG-rule is an abbreviation for an ordinary rule with two additional implicit arguments appended to the arguments of Head. These arguments are fresh variables, which we shall call V_in and V_out. The Body must be a valid DCG-goal, and is an abbreviation for an ordinary goal. The next section defines a mathematical function ‘DCG-transform(V_in, V_out, DCG-goal)’ which specifies the semantics of how DCG goals are transformed into ordinary goals. (The ‘DCG-transform’ function is purely for the purposes of exposition, to define the semantics — it is not part of the language.)