Chapter 9: The Dictionary
This chapter describes the dictionary: a compile-time data structure that maps textual words to compact binary entries used by the parser at runtime. Every single-quoted word that appears in source code creates or references an entry in the dictionary, and the parser consults this table to recognize words typed by the player.
9.1 Overview
The dictionary is a sorted table of word entries built by the compiler and embedded in the output story file. Its primary purpose is to allow the runtime parser to look up words from the player's input and determine what they mean - whether a word is a verb, a noun, a preposition, or some combination of these.
Every single-quoted word of two or more characters in source code (such as
'lamp' or 'take') causes the compiler to create or locate an entry in the
dictionary. (A single-character single-quoted literal such as 'a' is instead a
character constant - the character's ZSCII value, or its Unicode code point on
Glulx - not a dictionary word; see §1.7.) The compiler assigns flag bits to each
entry based on how the word is used: words appearing in Verb directives
receive verb and preposition flags, words in name properties receive noun
flags, and so on. At runtime, the parser tokenizes the player's input, looks up
each token in the dictionary, and uses the flags and associated data to
interpret the command.
Dictionary entries are stored in alphabetical order and encoded in a compact
binary format that differs between the Z-machine and Glulx virtual machines. The
compiler handles all encoding automatically; the programmer interacts with the
dictionary through single-quoted word literals, the Dictionary directive, and
a small set of compiler constants for low-level access.
Object lamp "brass lamp"
with name 'brass' 'lamp' 'lantern',
description "A well-polished brass lamp.";
[ Main;
! Each word in 'name' creates a dictionary entry.
print "'lamp' is at address ", 'lamp', "^";
];
9.2 Dictionary Word Literals
A dictionary word literal is a word enclosed in single quotes. It evaluates at runtime to the address of the corresponding entry in the dictionary table. If the word does not yet exist in the dictionary, the compiler creates a new entry for it.
if (w == 'take') print "You said take!^";
if (w == 'get') print "You said get!^";
Dictionary words appear most commonly in three contexts:
-
nameproperties - listing the words that the parser should recognize as referring to an object:Object -> sword "rusty sword" with name 'rusty' 'sword' 'blade' 'weapon'; -
Verbdirectives - defining the grammar that the parser uses to interpret player commands:Verb 'take' 'get' 'grab' * noun -> Take * 'up' noun -> Take; -
Explicit comparisons - testing a parsed word against a known dictionary entry in game code:
[ TryDirection w; if (w == 'north' or 'south' or 'east' or 'west') print "That's a compass direction.^"; ];
9.2.1 Case Insensitivity
Dictionary lookups are case-insensitive. The words 'Take', 'take', and
'TAKE' all refer to the same dictionary entry. This matches the behavior of
the runtime parser, which converts player input to lowercase before performing
dictionary lookups.
9.2.2 Double-Quoted Dictionary Words
In most contexts, dictionary words are written with single quotes. However, a
few directives also accept a double-quoted string in a position that would
otherwise hold a dictionary word literal, and treat the entire string as a
single dictionary entry. These are the Dictionary directive (§9.6), the verb
synonyms and grammar-line prepositions of the Verb and Extend directives,
and double-quoted values inside the built-in name property.
9.3 Word Length Limits
Dictionary words have a maximum number of significant characters. Any characters beyond this limit are silently truncated, which means that two words differing only after the cutoff point will map to the same dictionary entry.
9.3.1 Z-Machine Limits
[Z-machine] In Z-machine version 3, only the first 6 characters of a dictionary word are significant (though internally only 4 bytes of Z-encoded text are stored). In versions 4 and above, the first 9 characters are significant (6 bytes of Z-encoded text).
This means that in a version 5 game (the most common Z-machine target), the
words 'pineapple' (9 characters) and 'pineapples' (10 characters) are
encoded identically - the trailing 's' is lost:
! In Z-machine v5, these refer to the SAME dictionary entry:
Object -> pineapple "pineapple"
with name 'pineapple' 'pineapples';
! Truncation is silent; no warning is issued. The two
! literals produce the same dictionary entry.
The virtual machine specification fixes the Z-machine word size: the compiler
automatically limits each dictionary entry's encoded text to 4 bytes (v3) or 6
bytes (v4+), and the memory setting $DICT_WORD_SIZE is ignored when targeting
Z-code.
Note: Due to a current compiler quirk, explicitly attempting to set
$DICT_WORD_SIZEto any value other than 6 on the command line (or in an!%header comment) for a Z-code game will produce a fatal error, even if the value matches the version's natural byte size (for example,$DICT_WORD_SIZE=4for v3). To compile cleanly, omit the setting and let the compiler choose the correct size for the target version.
9.3.2 Glulx Limits
[Glulx] On Glulx, the number of significant characters is controlled by the
$DICT_WORD_SIZEsetting (default: 9). The byte size of each character is controlled by$DICT_CHAR_SIZE(default: 1; set to 4 for full Unicode support).
Because Glulx supports configuration, the programmer can increase the dictionary
word size to avoid truncation. Setting $DICT_CHAR_SIZE to 4 enables full
Unicode dictionary words:
!% $DICT_WORD_SIZE=12
!% $DICT_CHAR_SIZE=4
! First 12 characters are significant, 4 bytes per character (Unicode).
The total byte size of the encoded word portion of each dictionary entry is
DICT_WORD_SIZE × DICT_CHAR_SIZE.
9.3.3 The Truncation Flag
When a dictionary word is longer than the significant character limit, the
compiler can optionally set the TRUNC_DFLAG (bit 6) on the entry to indicate
that truncation occurred. The $DICT_TRUNCATE_FLAG setting controls this
behavior:
!% $DICT_TRUNCATE_FLAG=1
! Truncated dictionary entries will now have bit 6 set in their flags.
When $DICT_TRUNCATE_FLAG is 0 (the default), bit 6 is instead set on all verb
words - a legacy behavior where TRUNC_DFLAG acts as an additional verb marker.
When $DICT_TRUNCATE_FLAG is 1, bit 6 is used exclusively to mark truncated
words (verbs or otherwise), and verbs no longer automatically receive this bit.
Enabling the truncation flag can be useful for games that want to warn players
when their input is truncated.
9.4 Dictionary Flags
Each dictionary entry has a set of associated flag bits stored in the first data
field (#dict_par1). These flags describe how the word is used in the game's
grammar and object definitions. The compiler sets most flags automatically based
on where the word appears in source code.
| Flag | Value | Meaning |
|---|---|---|
VERB_DFLAG |
1 | Word is used as a verb in grammar |
META_DFLAG |
2 | Word is a meta-verb (always paired with VERB_DFLAG) |
PLURAL_DFLAG |
4 | Word is a plural form (set by //p suffix) |
PREP_DFLAG |
8 | Word is used as a preposition in grammar |
SING_DFLAG |
16 | Word is a singular form (set by //s suffix) |
TRUNC_DFLAG |
64 | Word was truncated (if $DICT_TRUNCATE_FLAG enabled) |
NOUN_DFLAG |
128 | Word is used as a noun (set by //n suffix) |
The flags are bit values and can be combined. When the same word appears in
multiple contexts, the flags are combined with bitwise OR. For example, a word
that is both a verb and a preposition will have both VERB_DFLAG and
PREP_DFLAG set, for a combined flag value of 1 | 8 = 9.
9.4.1 The Second and Third Data Fields
In addition to the flag field (#dict_par1), each dictionary entry has two more
data fields:
-
#dict_par2- reserved for the verb number. When a word hasVERB_DFLAGset, this field contains the index into the grammar table for the verb's grammar. You should not modify this field directly. -
#dict_par3- used for preposition (adjective) numbers in grammar version 1. In grammar versions 2 and 3, this field is unused and set to zero. It may be omitted entirely in Z-code when the$ZCODE_LESS_DICT_DATAoption is set.
9.5 Source Suffix Flags
Dictionary word literals can carry suffix flags that explicitly set or clear
specific dictionary flags on the entry. Suffixes are written after the closing
single quote, separated by //:
Object -> dogs "pack of dogs"
with name 'dogs'//p 'pack';
! 'dogs' has PLURAL_DFLAG set
9.5.1 Available Suffixes
The following suffixes are recognized:
| Suffix | Effect |
|---|---|
//p |
Set PLURAL_DFLAG (4) - marks word as plural |
//s |
Set SING_DFLAG (16) - marks word as singular |
//n |
Set NOUN_DFLAG (128) - marks word as a noun |
//~p |
Clear PLURAL_DFLAG |
//~s |
Clear SING_DFLAG |
//~n |
Clear NOUN_DFLAG |
The tilde (~) prefix negates the flag, explicitly clearing it even if it would
otherwise be set by context.
9.5.2 Combining Suffixes
Multiple suffix flags can be chained on a single word by writing one //
followed by the concatenated flag letters. The ~ character toggles the
negation state for the next flag letter, so it can be repeated to mix set and
clear operations within one suffix group:
Object -> sheep "sheep"
with name 'sheep'//pn;
! 'sheep' has both PLURAL_DFLAG and NOUN_DFLAG set
Object -> geese "flock of geese"
with name 'geese'//p 'goose'//s 'flock';
! 'geese' is plural, 'goose' is singular
A second // is not allowed inside a single-word literal - the compiler
reports an error if any character other than ~, p, s, or n appears after
the opening //.
9.5.3 Implicit Singular ($DICT_IMPLICIT_SINGULAR)
When the $DICT_IMPLICIT_SINGULAR setting is enabled (set to 1), the compiler
automatically adds SING_DFLAG to any word that has NOUN_DFLAG set but does
not have PLURAL_DFLAG set, and where //~s has not been used to clear the
singular flag explicitly.
!% $DICT_IMPLICIT_SINGULAR=1
Object -> lamp "lamp"
with name 'lamp';
! 'lamp' gets NOUN_DFLAG from the name property.
! Because NOUN_DFLAG is set and PLURAL_DFLAG is not,
! the compiler also sets SING_DFLAG automatically.
This feature is turned off by default ($DICT_IMPLICIT_SINGULAR=0). When
enabled, it provides the parser with more information about noun number, which
can be useful for games that distinguish singular and plural nouns in their
responses.
9.6 The Dictionary Directive
The Dictionary directive forces a word into the dictionary and optionally sets
flag values on it. It has three forms:
Dictionary 'word';
Dictionary 'word' val1;
Dictionary 'word' val1 val3;
9.6.1 Basic Usage
The simplest form ensures a word exists in the dictionary without associating it with any grammar or object:
Dictionary 'xyzzy';
! The word 'xyzzy' is now in the dictionary, even though
! it does not appear in any verb or name property.
9.6.2 Setting Flag Values
The second form sets the first data field (#dict_par1) of the entry:
Dictionary 'magic' 128;
! Creates (or updates) the entry for 'magic' with
! NOUN_DFLAG (128) set in #dict_par1.
The third form sets both the first and third data fields:
Dictionary 'spell' 128 42;
! Sets #dict_par1 to 128 and #dict_par3 to 42.
If the word already exists in the dictionary, the flag values are combined with
the existing values using bitwise OR. There is no way to set #dict_par2 via
the Dictionary directive, because that field is reserved for verb numbers and
modifying it directly would corrupt the grammar table.
9.6.3 Value Limits
[Z-machine] In Z-code, each flag value must fit in a single byte (0-255, i.e.,
$00-$FF). The compiler issues a warning if a value exceeds this range.
[Glulx] In Glulx, each flag value must fit in two bytes (0-65535, i.e.,
$0000-$FFFF). The compiler issues a warning if a value exceeds this range.
9.7 Dictionary Entry Format
The internal format of a dictionary entry differs between the Z-machine and Glulx. In both cases, each entry consists of an encoded word followed by data fields. All entries have the same fixed size, and the dictionary table stores them in sorted order for binary search at runtime.
9.7.1 Z-Machine Entry Format
[Z-machine] Each dictionary entry on the Z-machine contains:
- Encoded word: 4 bytes (version 3) or 6 bytes (version 4+) of Z-character encoded text.
- Data fields: The flag/data bytes that follow. In the default configuration, there are 3 data bytes:
#dict_par1(1 byte) - dictionary flags (§9.4)#dict_par2(1 byte) - verb number#dict_par3(1 byte) - preposition/adjective dataIn version 3, the total entry size is 7 bytes (4 + 3). In versions 4+, the total is 9 bytes (6 + 3). When
$ZCODE_LESS_DICT_DATAis set, the third data byte is omitted, reducing the entry size by 1 byte.
The Z-character encoding compresses words using a 5-bit-per-character scheme defined by the Z-machine specification. The compiler handles encoding transparently.
9.7.2 Glulx Entry Format
[Glulx] Each dictionary entry on Glulx contains:
- Type tag:
DICT_CHAR_SIZEbytes (1 byte with the default char size). The first byte is$60, which identifies this as a dictionary word.- Encoded word:
DICT_WORD_SIZE × DICT_CHAR_SIZEbytes. With the defaults (word size 9, char size 1), this is 9 bytes. With Unicode support (char size 4), it is 36 bytes.- Data fields: 3 fields of 2 bytes (total 6 bytes):
#dict_par1(2 bytes) - dictionary flags (§9.4)#dict_par2(2 bytes) - verb number#dict_par3(2 bytes) - preposition/adjective dataWith defaults, the total entry size is 16 bytes (1 + 9 + 6).
Glulx stores dictionary characters without compression: each character occupies
either 1 byte (Latin) or 4 bytes (Unicode), depending on $DICT_CHAR_SIZE.
9.7.3 Dictionary Table Header
The dictionary table begins with a short header describing its structure:
[Z-machine] The header contains: a count of word-separator characters, the separator characters themselves, the byte length of each entry, and the number of entries.
[Glulx] The header contains the number of entries (4 bytes). The entries follow immediately after the header, each with the same fixed byte length.
The #dictionary_table compiler constant (§9.8) gives the address of this
header.
9.8 Runtime Dictionary Access
The compiler provides several special constants for accessing the dictionary at
runtime. These constants are written with a # prefix and are resolved at
compile time.
9.8.1 Dictionary Address of a Word
The simplest way to reference a dictionary entry is through a word literal. At
runtime, 'word' evaluates to the byte address of that word's entry in the
dictionary table:
[ ShowAddress;
print "The entry for 'lamp' is at address ", 'lamp', ".^";
print "The entry for 'take' is at address ", 'take', ".^";
];
If a parsed word does not match any dictionary entry, the parser stores 0 (not a valid dictionary address) as the result.
9.8.2 The #dictionary_table Constant
The #dictionary_table constant gives the byte address of the start of the
dictionary table in memory. This is useful for low-level code that needs to
iterate over all dictionary entries or inspect the table structure directly.
9.8.3 The #dict_par1, #dict_par2, #dict_par3 Constants
These constants give the byte offset from the start of a dictionary entry to each of the three data fields. They allow runtime code to read or inspect dictionary flags and data:
[ IsVerb w;
! Check whether a dictionary word has VERB_DFLAG set.
! #dict_par1 gives the offset to the flags field.
if (w && (w->) & 1)
rtrue;
rfalse;
];
[Z-machine] In version 3, the offsets are
#dict_par1= 4,#dict_par2= 5,#dict_par3= 6. In versions 4+, the offsets are#dict_par1= 6,#dict_par2= 7,#dict_par3= 8.
[Glulx] The offsets depend on
DICT_WORD_SIZEandDICT_CHAR_SIZE. The#dict_par1,#dict_par2, and#dict_par3constants refer to the low bytes of the two-byte fields. For example, with defaults (word size 9, char size 1):#dict_par1= 11,#dict_par2= 13,#dict_par3= 15. The high bytes are at offsets one less than these values.
9.8.4 Reading Dictionary Flags at Runtime
Using the constants above, game code can inspect dictionary flags at runtime. This is occasionally useful for debugging or for special parser extensions:
[ PrintWordInfo w flags;
if (w == 0) { print "Unknown word.^"; return; }
flags = w->;
print "Dictionary flags for word at ", w, ": ", flags, "^";
if (flags & 1) print " VERB^";
if (flags & 2) print " META^";
if (flags & 4) print " PLURAL^";
if (flags & 8) print " PREPOSITION^";
if (flags & 16) print " SINGULAR^";
if (flags & 128) print " NOUN^";
];
[Glulx] On Glulx, the data fields are two bytes wide. Because
#dict_par1,#dict_par2, and#dict_par3point at the low byte of each field, a single-byte read (w->#dict_par1) returns the low 8 bits - which is sufficient for every flag described in §9.4, since all standard*_DFLAGvalues fit in one byte. To read the full 16-bit value (only relevant when custom values larger than 255 have been stored via theDictionarydirective), use the Glulx assembly opcode@aloads, which loads a 16-bit short. Because@aloadscomputes its source address asarray + 2 * index, the usual idiom is to point the array operand at the high byte of the field and pass an index of zero:addr = w + - 1; ! point to high byte of par1 @aloads addr 0 flags; ! load 16-bit big-endian shortNote that the
-->operator is not suitable here, because on Glulx it reads a 4-byte word rather than a 2-byte field.
9.9 The Dictionary and the Parser
The dictionary exists primarily to serve the parser. Understanding the relationship between them helps explain why dictionary entries are structured the way they are.
9.9.1 Input Tokenization
When the player types a command, the parser breaks the input into tokens (words) and looks up each token in the dictionary using a binary search. If a token matches a dictionary entry, the parser stores the address of that entry. If no match is found, the parser stores 0 for that token.
! In a game, if the player types: "take the lamp"
! The parser tokenizes this into: "take", "the", "lamp"
! and looks up each word in the dictionary.
! 'take' -> address of 'take' entry (has VERB_DFLAG)
! 'the' -> address of 'the' entry (a noise word)
! 'lamp' -> address of 'lamp' entry (has NOUN_DFLAG)
9.9.2 Object Name Matching
The name property of an object contains a list of dictionary word addresses.
When the parser is trying to identify which object the player is referring to,
it compares the dictionary addresses of the player's input words against the
name lists of objects in scope.
9.9.3 Grammar and Prepositions
Verb directives define grammar patterns that the parser uses to interpret
commands. Dictionary words within grammar lines (as opposed to the verb synonyms
at the start) are treated as prepositions and receive PREP_DFLAG:
Verb 'put'
* noun 'in' noun -> Insert
* noun 'on' noun -> PutOn
* 'down' noun -> Drop;
! 'put' gets VERB_DFLAG (it's the verb itself)
! 'in', 'on', 'down' get PREP_DFLAG (prepositions)
The parser uses these flags during command interpretation: it first identifies
the verb (checking VERB_DFLAG), then matches the remaining input against the
grammar patterns, recognizing prepositions by their PREP_DFLAG.
9.9.4 Unrecognized Words
When a word typed by the player does not appear in the dictionary, the parser assigns it a dictionary address of 0. The standard library handles this with messages like "I don't understand that word."
9.10 Practical Considerations
9.10.1 Dictionary Size
There is no hard-coded limit on the number of dictionary entries in typical configurations, but each entry consumes space in the story file. In Z-machine games, the story file size limits can become a constraint. Glulx games have much larger address spaces and are rarely limited by dictionary size. The compiler reports the number of dictionary entries in its compilation statistics.
9.10.2 Avoiding Truncation Pitfalls
Because dictionary words are silently truncated, it is good practice to keep important vocabulary within the significant character limit. When two distinct game concepts have names that differ only beyond the truncation point, the parser cannot distinguish them:
! In Z-machine v5 (9 significant characters), these are
! the same dictionary entry:
Object -> container1 "container"
with name 'container';
Object -> containers "containers"
with name 'containers';
! Both truncate to 'container' (9 chars).
! Solutions: use shorter synonyms, target Glulx with
! larger $DICT_WORD_SIZE, or use parse_name.
9.10.3 Dictionary Words vs. Strings
Dictionary words (single-quoted) and strings (double-quoted) are fundamentally different things:
'lamp'is a dictionary word literal - it evaluates to a dictionary entry address and is used for parser matching."lamp"is a string literal - it evaluates to a packed string address and is used for printing text.
These two values are not interchangeable. Comparing a dictionary address to a string address will never match, even if the text content is identical:
[ Main;
! These are completely different values:
print 'lamp', "^"; ! prints the numeric dictionary address
print "lamp", "^"; ! prints the text "lamp"
! This comparison is always false:
if ('lamp' == "lamp") print "match^"; ! never prints
];