Previous: Earley Parser, Up: Syntax Protocol


18.4.4.5 Specifying A Grammar

An incremental parser is created from a grammar.

— Macro: drei-syntax:grammar &body body

Create a grammar object from a set of rules.

— Rule: symbol -> (&rest arguments) &optional body

Each rule is a list of this form.

Here symbol is the target symbol of the rule, and should be the name of a CLOS class.

— Rule argument: (var type test)

The most general form of a rule argument.

Here var is the name of a lexical variable. The scope of the variable contains the test, all the following arguments and the body of the rule. The type is a Common Lisp type specification. The rule applies only of the type of the object contain in var is of that type. The test contains arbitrary Common Lisp code for additional checks as to the applicability of the rule.

— Rule argument: (var type)

Abbreviated form of a rule argument.

Here, type must be a symbol typically the name of a CLOS class. This form is an abbreviation for (var type t).

— Rule argument: (var test)

Abbreviated form of a rule argument.

Here, test must not be a symbol. This form is an abbreviation of (var var test), i.e., the name of the variable is also the name of a type, typically a CLOS class.

— Rule argument: var

Abbreviated form of a rule argument.

This form is an abbreviation of (var var t).

The body of a rule, if present, contains an expression that should have an instance (not necessarily direct) of the class named by the symbol (the left-hand-side) of the rule. It is important that this restriction be respected, since the Earley algorithm will not work otherwise.

If the body is absent, it is the same as if a body of the form (make-instance 'symbol) had been given.

The body can also be a sequence of forms, the first one of which must be a symbol. These forms typically contain initargs, and will be passed as additional arguments to (make-instance 'symbol).