5.2 A Data Language Example

Building Parsers with Java
By Steven John Metsker

Table of Contents
Chapter 5.  Parsing Data Languages


Consider a coffee company that creates a monthly brochure that promotes certain coffees. The marketing director types a list each month of coffees that she wants the brochure to mention, and she sends the list to a creative writer elsewhere in the department. This writer has a small, homegrown application that reads the file and provides a user interface with links to other information, specifically a data warehouse with demographics about the sales of each coffee in the file. This application has been plagued with defects, especially around the reading of the input file. Your job is to simplify the reading of this file. The marketing director has flatly refused to change how she enters the file and will not listen to anyone who uses acronyms, especially if the acronym begins with X. Thus, you cannot move this application to XML; you must simply clean up the reading of the current language. Here is this month's list of coffees to read:

 Brimful, Regular, Kenya, 6.95  Caress (Smackin), French, Sumatra, 7.95 Fragrant Delicto, Regular/French, Peru, 9.95 Havalavajava, Regular, Hawaii, 11.95 Launch Mi, French, Kenya, 6.95 Roman Spur (Revit), Italian, Guatemala, 7.95 Simplicity House, Regular/French, Colombia, 5.95 

With a little analysis, you determine that the file contains descriptions of types of coffee. Each line has the name , roast, country of origin, and price (per pound ) for the coffees featured this month. If a type of coffee has a new name, the former name appears in parentheses after the new name. If the coffee is offered as a French roast in addition to its normal roast, the characters /French follow the normal roast.

In addition to the existing business rules, there is an existing Coffee class, shown in Figure 5.1.

Figure 5.1. The Coffee class. A Coffee object has various attributes and is publicly cloneable because Coffee implements the PubliclyCloneable interface.


The goal in this example is to create a new parser that recognizes the existing language and builds Coffee objects from their textual descriptions. The tasks are as follows :

  • Write a grammar.

  • Write assemblers to build a Coffee object.

  • Generate the parser from the grammar, plugging in the assemblers.


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net