Register your product to gain access to bonus material or receive a coupon.
No programming language can solve every problem, but Java can be extended to solve a far wider range of problems through the use of parsers -- "mini-languages" that bridge the gap between humans and computers, and offer targeted solutions for specific problem domains. In Building Parsers with Java, Steven John Metsker presents the first complete, start-to-finish guide to building parsers with Java. Metsker first explains what a parser is, introduces the building blocks of applied parsers, shows how to compose new parsers from existing ones; and walks step-by-step through designing, coding, and testing a working parser. Next, he presents in-depth coverage of creating Java-based parsers for a wide range of problem domains. You'll learn how to create parsers that can read elements of a data language; how to transform a grammar, ensuring the correct behavior of operators in a language; how to build arithmetic and regular expression parsers, and more. Metsker shows how to extend existing parser toolkits, and introduces a logic engine that can be used in a wide range of parsers, including both logic and query languages.
Click below for Sample Chapter related to this title:
metskch1.pdf
metskch2.pdf
metskch3.pdf
(NOTE: Each chapter concludes with a Summary.)
Foreword.
Preface.
Who Should Read This Book.
Using the Toolkit Code and the Sample Code.
Contents of the CD.
Applying the Code on the CD.
Hello World.
Coding Style.
Related Books.
Theoretical Context.
Yacc and Lex and Bison and Flex.
About the Cover.
Acknowledgments.
The Role of Parsers.
What Is a Language?
The Organization of This Book.
What Is a Parser?
Parser Collaborations.
Assemblies.
The Assembly Class Interfaces.
Token and Character Assemblies.
Tokenizing.
Default and Custom Tokenization.
Assembly Appearance.
Assembly Summary.
The Parser Hierarchy.
The Composition of a Parser.
Terminal Parsers.
Using Terminals.
Word Terminals.
Num Terminals.
Literals.
Caseless Literals.
Symbols.
Quoted Strings.
Composite Parsers.
Repetition.
Alternation and Sequence.
Composing a Parser.
The Empty Parser.
Parser Summary.
Assemblers.
Parsers Use Assemblers.
Assemblers Work On Assemblies.
Elements Above.
Design Overview.
Deciding to Tokenize.
Designing Assemblers.
The Collaboration of Parsers, Assemblers, and Assemblies.
Using an Assembly's Stack.
Assemblers Plug In to Parser Composites.
A Language to Plug In To: Minimath.
Calculating a Minimath Result.
The Minimath Parser as an Object.
Building a Target.
Making a Target Cloneable.
Grammars: A Shorthand for Parsers.
Standard Grammar Shorthand.
Top-Down Grammar Design.
Example: Designing a Grammar for a Track Robot.
A Track Robot Grammar.
Checking for Left Recursion and Cycles.
Translating a Grammar to Code.
Translate Quoted Strings.
Translate Sequences.
Translate Alternations.
Translate Terminals.
Create a Subparser for Each Rule.
Option 1: Declare Each Subparser.
Option 2: Arrange Subparsers as Methods.
Add a Start Method.
Completing a Parser.
Control Pushing.
Design the Target.
Plug In Assemblers.
Feature Testing.
Random Testing.
Ambiguity Testing.
Terminal Ambiguity.
Special Tokenizers and Targets.
The Role of Data Languages.
A Data Language Example.
A Coffee Grammar.
A Tokenizing Problem.
Coffee Assemblers.
Coffee Assembler Code.
Translating the Coffee Grammar to Code.
Data Language Parser Summary.
Parsers with XML.
A Brief History of XML.
The Evolution of XML.
An XML Example.
Helpers.
ShowCoffeeXML.
The Role of Grammar Transformation.
Ensuring Correct Associativity.
Eliminating Left Recursion.
An Algorithm.
Ensuring Proper Precedence.
Eliminating Parser Class Loops.
Building an Arithmetic Parser.
Conventional Symbols.
Conventional Precedence.
Conventional Associativity.
An Arithmetic Grammar.
Arithmetic Assemblers.
Assembler Code.
An Arithmetic Grammar Parser.
The Role of Regular Expressions.
Building a Regular Expression Parser.
A Regular Expression Grammar.
Regular Expression Assemblers.
Assembler Code.
A Regular Expression Parser.
The Role of a Tokenizer.
Acquiring a Tokenizer.
Tokenizers in Standard Java.
A Token Class.
A Tokenizer Class.
Tokenizer Lookup Tables.
Tokenizer States.
QuoteState.
NumberState.
SlashState.
SymbolState.
WhitespaceState.
WordState.
Setting a Tokenizer's Source.
Customizing a Tokenizer.
The TokenStringSource Utility.
Customizing a State.
Changing Which State the Tokenizer Enters.
Adding a State.
Token Strings.
Introduction.
Parser Matching.
Repetition Matching.
Collection Parsers.
Sequence Matching.
Alternation Matching.
Empty Matching.
Terminal Matching.
Token Terminals.
Character Terminals.
Terminals Summary.
Parser Matching Utilities.
The Role of New Types of Parsers.
New Terminals.
New Token Types.
New Parser Features.
An Error-Handling Parser.
Tracks in Action.
Engines versus Interpreters.
The Role of Engines.
Building Blocks.
Structures.
Variables.
Unification.
Facts.
Programs and Queries.
Proofs.
Backtracking.
Rules.
Additional Features of the Engine.
Comparisons.
Arithmetic.
Evaluation.
Not.
Anonymous Variables.
Lists.
The Role of Logic Languages.
Building Blocks.
Structures.
Variables.
A Logikus Interactive Development Environment.
Unification.
Comparisons.
Rules, Axioms, and Programs.
Proofs.
Variable Scope.
Variable Joins.
Backtracking.
Looping and Halting.
Gateways.
Additional Features of Logikus.
Comments.
Evaluations.
Negation.
Not Dangerous.
Anonymous Variables.
Lists.
Dot Notation.
List Applications.
Member.
Prefix.
Suffix.
Permutation.
Modeling Transitive and Symmetric Relations.
Symmetric Relations.
Example Applications.
An Authorization Program.
Epidemic.
Generate and Test.
Generate and Test in Java.
Altitude Bands.
Building a Logic Language Environment.
A Logikus Grammar.
Comments in Logikus.
Logikus Programs.
Logikus Assemblers.
The Logikus Interactive Development Environment.
A Facade for Logikus.
The Role of Query Languages.
A Sample Database.
Facts, Objects, and Rows.
Jaql.
Jaql Syntax.
Jaql Joins.
Jaql Expressions.
Building a Query Language Environment.
Translating User Queries to Engine Queries.
Natural Joins.
Where Clauses.
Projection.
A Query Builder.
A Speller.
Jaql Grammar.
Creating the Jaql Parser.
Jaql Assemblers.
The Jaql User Environment.
Exception Handling.
Tracks.
Handling Abject Failure.
The Role of Imperative Languages.
Sling.
Sling Programming.
A Basic Sling.
Adding Slings.
Plotting Time.
Line Effects.
Adding Lines.
Cartesian Plots.
Cartesians as Points.
Polar Plots.
For Loops.
Sliders.
A Composite Example.
More Plots.
The Elements of Sling>
Building the Sling Environment.
Building Commands.
A Command Example.
AssignmentCommand.
CommandSequence.
ForCommand.
IfCommand.
NullCommand.
PrintlnCommand.
ReadCommand.
WhileCommand.
Commands Summary.
Sling Commands.
AddFunctionCommand.
AssignFunctionCommand.
Building Runtime Functions.
Function Wrapping.
Umbrella Types.
Runtime Functions in Sling.
Execution Phases.
Prototyping.
Function Evaluation.
Sling Functions.
SlingFunction.
Abs, Ceil, Cos, Floor, Sin, and Tan.
Arithmetic.
Cartesian.
Point.
Polar.
Random.
Scale.
Sling.
Slider.
T.
Variable.
Sling Target.
A Sling Grammar.
Sling Assemblers.
AssignmentAssembler.
ForAssembler.
FunctionAssembler.
NegativeAssembler.
NumAssembler.
PiAssembler.
PlotAssembler.
ScaleAssembler.
SliderAssembler.
VariableAssembler.
A Sling Parser.
Reserved Words.
Assembler Placement.
Get Started.
Get the Most Out of This Book.
Other Essential Reading.
Keep Going.
Create New Languages for Your Colleagues.
Create New Languages for Children.
Explore Languages as Human Interfaces.
Improve Languages as Receptacles for Human Thought.
Choose Your Own Direction.
Classes.
Class Relationships.
Interfaces.
Objects.
The premise of this book is that by learning how to work with parsers, you can create new computer languages that exactly fit your domain. When you create a language, you give your language users a new way to control their computers. By learning about parsers, you learn to define the way your users interact with computers using text.
This book comes with a CD that contains all the code.
Contents of the CDThe CD includes all the code of the fundamental parser classes, the logic engine, and all the examples. The CD also contains the javadoc documentation from the code, which explains class by class how the code works.
Applying the Code on the CDThe code on the CD is free. It is copyrighted, so you may not claim that you wrote it. Otherwise, you may use the code as you wish.
Hello WorldThe following program is a sufficient test to verify that you can use the code from the CD. Load the code from the CD into your development environment. Type in the following program, or load it from ShowHello.java on the CD.
package sjm.examples.preface; import sjm.parse.*; import sjm.parse.tokens.*; public class ShowHello { public static void main(String args) { Terminal t = new Terminal(); Repetition r = new Repetition(t); Assembly in = new TokenAssembly("Hello world!"); Assembly out = r.completeMatch(in); System.out.println(out.getStack()); } }
Compiling and running this class prints the following:
Hello, world, !
Once you get this running in your environment, you will be able to use all the fundamental classes and all the examples in this book.
Coding StyleSome features of the coding style in this book may seem unusual. First, this book does not indent method signatures. This practice stems from the fact that the VisualAge development environment exports classes this way, resulting in a pair of curly braces at the end of a class. This convention has the happy effect of allowing a little more space before statements are wrapped within the narrow margins of this book.
Another feature of the coding style in this book that may give you pause is the use of extremely short variable names. Methods in this book nearly always perform a single service and thus are short. Temporary variables are never far from their declarations, and there is usually no need for names longer than one character. For example, it is not difficult in the preceding program to discern that the variable t refers to a Terminal object. In the rare event that two variables of a given type occur in one method, they receive meaningful names, such as in and out in the preceding example.
Comments in the code use javadoc tags such as @param and @exception, but the text usually omits these to save space. Comments for public methods begin with