5.3 A Coffee Grammar


 
Building Parsers with Java
By Steven John Metsker

Table of Contents
Chapter 5.  Parsing Data Languages

    Content

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 is just a word, and the price is just a number:

 country = Word;  price   = Num; 

You can translate this grammar directly to code using the rules in Section 3.6 "Translating a Grammar to Code." When you write the parser, you will want to plug in your assemblers, a subject we will address shortly.


   
Top


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

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