Building Parsers with Java™

Book description

Parser building is a powerful programming technique that opens a world of opportunity for designing how users interact with applications. By creating mini-languages, you can precisely address the requirements of your application development domain. Writing your own parsers empowers you to access a database more effectively than SQL to efficiently control the movement of an order through its workflow, to command the actions of a robot, and to control access privileges to transactions in a system. The repertoire of today's professional programmer should include the know-how to create custom languages.

Building Parsers with Java™ shows how to create parsers that recognize custom programming languages. This book and its accompanying CD provide an in-depth explanation and clearly written tutorial on writing parsers, following the Interpreter Design Pattern. An easy-to-follow demonstration on how to apply parsers to vital development tasks is included, using more than a hundred short examples, numerous UML diagrams, and a pure Java parser toolkit to illustrate key points.

You will learn

  • How to design, code, and test a working parser

  • How to create a parser to read a data language, and how to create new computer languages with XML

  • How to translate the design of a language into code

  • How to accept an arithmetic formula and compute its result

  • How to accept and apply matching expressions like th* one

  • How to use tokenizers to define a parser in terms of logical nuggets instead of individual characters

  • How to build parsers for a custom logic language like Prolog

  • How to build parsers for a custom query language that goes beyond SQL

  • How to construct an imperative language that translates text into commands that direct a sequence of actions



  • 0201719622B04062001

    Table of contents

    1. Copyright
    2. Foreword
    3. Preface
    4. Introduction
      1. The Role of Parsers
      2. What Is a Language?
      3. The Organization of This Book
      4. Summary
    5. The Elements of a Parser
      1. What Is a Parser?
      2. Parser Collaborations
      3. Assemblies
      4. The Parser Hierarchy
      5. Terminal Parsers
      6. Composite Parsers
      7. Assemblers
      8. Summary
    6. Building a Parser
      1. Design Overview
      2. Deciding to Tokenize
      3. Designing Assemblers
      4. Grammars: A Shorthand for Parsers
      5. Example: Designing a Grammar for a Track Robot
      6. Translating a Grammar to Code
      7. Completing a Parser
      8. Summary
    7. Testing a Parser
      1. Feature Testing
      2. Random Testing
      3. Special Tokenizers and Targets
      4. Summary
    8. Parsing Data Languages
      1. The Role of Data Languages
      2. A Data Language Example
      3. A Coffee Grammar
      4. A Tokenizing Problem
      5. Coffee Assemblers
      6. Translating the Coffee Grammar to Code
      7. Data Language Parser Summary
      8. Parsers with XML
      9. Helpers
      10. Summary
    9. Transforming a Grammar
      1. The Role of Grammar Transformation
      2. Ensuring Correct Associativity
      3. Eliminating Left Recursion
      4. Ensuring Proper Precedence
      5. Eliminating Parser Class Loops
      6. Summary
    10. Parsing Arithmetic
      1. Building an Arithmetic Parser
      2. An Arithmetic Grammar
      3. Arithmetic Assemblers
      4. An Arithmetic Grammar Parser
      5. Summary
    11. Parsing Regular Expressions
      1. The Role of Regular Expressions
      2. Building a Regular Expression Parser
      3. A Regular Expression Grammar
      4. Regular Expression Assemblers
      5. A Regular Expression Parser
      6. Summary
    12. Advanced Tokenizing
      1. The Role of a Tokenizer
      2. Acquiring a Tokenizer
      3. Tokenizers in Standard Java
      4. A Token Class
      5. A Tokenizer Class
      6. Tokenizer Lookup Tables
      7. Tokenizer States
      8. Setting a Tokenizer's Source
      9. Customizing a Tokenizer
      10. The TokenStringSource Utility
      11. Token Strings
      12. Summary
    13. Matching Mechanics
      1. Introduction
      2. Parser Matching
      3. Repetition Matching
      4. Collection Parsers
      5. Sequence Matching
      6. Alternation Matching
      7. Empty Matching
      8. Terminal Matching
      9. Parser Matching Utilities
      10. Summary
    14. Extending the Parser Toolkit
      1. The Role of New Types of Parsers
      2. New Terminals
      3. New Token Types
      4. New Parser Features
      5. Summary
    15. Engines
      1. Engines versus Interpreters
      2. The Role of Engines
      3. Building Blocks
      4. Unification
      5. Facts
      6. Programs and Queries
      7. Proofs
      8. Rules
      9. Additional Features of the Engine
      10. Summary
    16. Logic Programming
      1. The Role of Logic Languages
      2. Building Blocks
      3. A Logikus Interactive Development Environment
      4. Unification
      5. Comparisons
      6. Rules, Axioms, and Programs
      7. Proofs
      8. Additional Features of Logikus
      9. Lists
      10. List Applications
      11. Modeling Transitive and Symmetric Relations
      12. Example Applications
      13. Summary
    17. Parsing a Logic Language
      1. Building a Logic Language Environment
      2. A Logikus Grammar
      3. Logikus Assemblers
      4. The Logikus Interactive Development Environment
      5. Summary
    18. Parsing a Query Language
      1. The Role of Query Languages
      2. A Sample Database
      3. Jaql
      4. Building a Query Language Environment
      5. Translating User Queries to Engine Queries
      6. A Query Builder
      7. A Speller
      8. Jaql Grammar
      9. Creating the Jaql Parser
      10. Jaql Assemblers
      11. The Jaql User Environment
      12. Summary
    19. Parsing an Imperative Language
      1. The Role of Imperative Languages
      2. Sling
      3. Sling Programming
      4. Building the Sling Environment
      5. Building Commands
      6. Sling Commands
      7. Building Runtime Functions
      8. Sling Functions
      9. Sling Target
      10. A Sling Grammar
      11. Sling Assemblers
      12. A Sling Parser
      13. Summary
    20. Directions
      1. Get Started
      2. Keep Going
    21. UML Twice Distilled
      1. Classes
      2. Class Relationships
      3. Interfaces
      4. Objects
    22. References

    Product information

    • Title: Building Parsers with Java™
    • Author(s): Steven John Metsker
    • Release date: March 2001
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780201719628