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)); ...

Get Building Parsers with Java™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.