An exposition of the data structures and basic method used to deal with the command-parsing grammar implied by Understand sentences in the source text.


§1. This is grammar in the sense of the parsing structures used at run-time, and it occupies a chapter of its own in the source code since it is to some extent detached from the rest of Inform: what we create in this chapter is almost an independent compiler in its own right, but of a much simpler language. Although we use many higher-level features of Inform in the process, none use this.

Grammar is organised in a three-level hierarchy:

The picture is not quite so hierarchical as it looks, though, because a GV naming a token can be used as a token inside other GVs. We need to be careful that this does not lead to infinite regress: see below.

Much of what we do with grammar involves recursing down this hierarchy, in some cases allowing results to percolate back upwards. What happens takes place in four chronological phases. (This division into phases is convenient because Inform 6 requires that all general parsing routines and noun filter routines already exist when a Verb directive is reached which uses them.)

§2. Phase I: Slash Grammar. Slashing is the process of dealing with slashes / used in grammar to indicate alternatives.

§3. Phase II: Determining Grammar. We check that the grammar is well-founded and find the types of values expressed by it, if any.

Determining well-foundedness means checking that no two grammar tokens each require the use of the other, and that when a grammar token takes several alternative forms, they have compatible results: so, for instance, you can't have one version resulting in a number and another in a thing. (This check is only meaningful for grammar verbs of type GV_IS_TOKEN.)

The result of a GV_IS_TOKEN is a single specification, which is the union of the kinds resulting from its grammar lines. This is a more sophisticated approach than we really need here, but might be useful for future expansion.

Of the determining traverse the following can be said:

We note of the determining routines that:

§4. Phase III: Sort Grammar. We must ensure that if grammar line L1 is logically impossible once L2 has been parsed, then L1 must be checked before L2, regardless of the ordering in the source code. Since the data sets are very small and time is not of the essence, we simply insertion-sort the original definition-order list into a second linked list.

§5. Phase IV: Compile Grammar. The final run-through, which uses the sorted order and not the original declaration order, actually compiles the necessary I6 Verb and Extend directives.