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.6. Translating the Coffee Grammar to Code

You can follow the rules of Section 3.6 “Translating a Grammar to Code,” and code subparsers as methods on a parser class. Figure 5.4 shows CoffeeParser.

Figure 5.4. The CoffeeParser class. This class organizes its subparsers as individual methods.

Most of the methods of the CoffeeParser class return a parser that recognizes one aspect of the coffee grammar. For example, the price() method returns a parser for the rule

price = Num; 

The code for CoffeeParser.price() plugs in the price assembler:

 /* * Return a parser that will recognize the sequence: * * price = Num; * * Use a PriceAssembler to update ...

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