8.
Procedures
Formal
grammars may be seen as declarative descriptions of string languages. A
frequently raised question addresses the possibility of encapsulating
procedural
knowledge
into declarative knowledg
e.
In a formal grammar this may be accomplished by "flags" (see "Programmed gramma
rs",
QuickStart §11) or by other procedures, see for instance "Destroying
structur
es"
§4.5.
Here
we deal with
procedures
that may be inserted in subgrammar rules. Every procedure name starts with an
underline character. Current procedures are
_destr
u
_got
o
_faile
d
_repea
t
_sto
p
that
control derivations, and
_prin
t
_printO
n
_printOf
f
_stepO
n
_stepOf
f
_traceO
n
_traceOf
f
that
modify the trace output, not the derivation itself.
Note
that the names of procedures are not case-sensitive. Thus, for example,
"_traceOn" may be equivalently written "_Traceon", "_TRACEON", etc.
If
a procedure appears in the left argument of a rul
e
it is executed before the rule is applied. If it appears in the right argument
it is executed once the rule has been applied. Trace procedures, _destr
u
and _sto
p
may appear on either side, but _got
o,
_faile
d
and _repea
t
must only appear in the right argument of a rul
e.
Procedures
in the left argument should be placed after weights and flags, and before any
variables or terminal symbols. Procedures in the right argument should be
placed after variables or terminal symbols.