5.4. A Tokenizing Problem

The coffee grammar accepts coffee names, roasts, and countries as Word terminals. This creates a problem if any of these “words” contains a blank. For example, a coffee name might be “Toasty Rita,” from Costa Rica. By default, the class Tokenizer in sjm.parse.tokens treats a blank as the end of a word. When tokenizing the text

Toasty Rita, Italian, Costa Rica, 9.95 

a default tokenizer would return Toasty as a Word, followed by Rita as a Word. After the first word, the grammar will be looking for a comma and not another word, and a parser generated from the grammar will fail to match the input text.

One solution is to ask the tokenizer to allow blanks to occur inside words. The following code snippet creates such a ...

Get Building Parsers with Java™ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.