O'Reilly logo

Building Parsers with Java™ by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.3. A Coffee Grammar

You can create a grammar for the coffee data language by applying the following algorithm:

1.
Define the parser you want as a composite of subparsers.
2.
Repeat step 1 until every subparser is defined or is a terminal.

The parser you want to define is

coffee 

From the preceding language element, you can see that a coffee description is, at a high level, the following:

coffee = name ',' roast ',' country ',' price; 

A coffee's name is a word followed optionally by a former name. The former name is a word in parentheses.

name       = Word (formerName | Empty); 
formerName = '(' Word ')';

The roast is a word followed optionally by the characters /French:

roast    = Word (orFrench | Empty); 
orFrench =  '/' "french";

The country ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required