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

3.4. Grammars: A Shorthand for Parsers

A grammar is a collection of related parser definitions in which the definitions follow a standard shorthand. A goal of the design phase in software construction is to illustrate in compact form the important features that will appear in Java code. Consider again the code that builds a parser to recognize a description of a good cup of coffee:

 package sjm.examples.introduction; import sjm.parse.*; import sjm.parse.tokens.*; /** * Show how to create a composite parser. */ public class ShowComposite { public static void main(String[] args) { Alternation adjective = new Alternation(); adjective.add(new Literal("steaming")); adjective.add(new Literal("hot")); Sequence good = new Sequence(); good.add(new Repetition(adjective)); ...

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