
|
The Parse Treeby Richard Bartle
So youve stayed up until the small hours, night after night, and you finally have yourself a parser. Players can type in complicated imperative sentences and your program will accept them or generate a stream of expletives. How do you use it, though?
<input line>
|
<sentence>
|
<command>
|
<command 2>
|
+-----------------+--------+--------+-----------------+
| | | |
| <noun phrase> | <noun phrase>
| | | |
| <noun group> | <noun group>
| | | |
| +-----+-----+ | +-----+-----+
| | | | | |
WITH THE DIAMOND RING THE BELL
preposition article noun verb article noun
WITH THE DIAMOND RING THE BELLpreposition article noun verb article noun From this data structure (which you can build up piecemeal on the unwind of the recursion a tiresome but not time-consuming exercise) its possible to identify not only what the individual words refer to, but what the individual rules do. Thus, if you want to find out what the second noun group of a command is, you really can just read it off the data structure. If you prefer a brute-force approach, then you dont strictly need the data structure you can rip elements out of the token array by relying on what you know about your grammar (e.g. anything between a preposition and either a verb, preposition or end of sentence is a noun phrase if you ignore the adverbs and hack your way round conjunctions). In the case where your parser is only partially backtracking, like MUD2s, youll pretty well have to do it this way. Making meaning
So its possible to group tokens together into meaningful grammatical units. Now you have to figure out what they mean in game terms.
The verb modifiers are just the adverbs and prepositions that the command contains. The noun phrases themselves have their own particular format:
where the noun modifiers are the adjectives and qualifying noun phrases associated with the noun. Examples
Heres are a couple of examples Ive used before in this series of articles:
[ drop,
[inside, very, quickly, secretively],
[ [treasure,
[the,
[except,
[and,
[ [emerald,
[the]
],
[stone,
[the, small, white]
]
]
]
]
]
],
[box,
[the, open, music]
]
],
]
This list is the result of the parsing module, and its passed on to the binder. The binder has to take the list, and use its own knowledge of the game worlds state to derive individual function calls. You dont have to write the binder in your game worlds definition language, but its a darned sight easier if you do (MUD2s is written in MUDDLE like the rest of the game now; I learned the hard way...). The binder is the final step of the whole parsing process, but it can be among the most tortuous and frustrating of them all because there are so many special cases involved. |