logo资料库

The Definitive ANTLR 4 Reference.pdf

第1页 / 共322页
第2页 / 共322页
第3页 / 共322页
第4页 / 共322页
第5页 / 共322页
第6页 / 共322页
第7页 / 共322页
第8页 / 共322页
资料共322页,剩余部分请下载后查看
Cover
Table of Contents
Acknowledgments
Welcome Aboard!
Who Is This Book For?
What's So Cool About ANTLR V4?
What's in This Book?
Learning More About ANTLR Online
Part I—Introducing ANTLR and Computer Languages
1. Meet ANTLR
Installing ANTLR
Executing ANTLR and Testing Recognizers
2. The Big Picture
Let's Get Meta!
Implementing Parsers
You Can't Put Too Much Water into a Nuclear Reactor
Building Language Applications Using Parse Trees
Parse-Tree Listeners and Visitors
3. A Starter ANTLR Project
The ANTLR Tool, Runtime, and Generated Code
Testing the Generated Parser
Integrating a Generated Parser into a Java Program
Building a Language Application
4. A Quick Tour
Matching an Arithmetic Expression Language
Building a Calculator Using a Visitor
Building a Translator with a Listener
Making Things Happen During the Parse
Cool Lexical Features
Part II—Developing Language Applications with ANTLR Grammars
5. Designing Grammars
Deriving Grammars from Language Samples
Using Existing Grammars as a Guide
Recognizing Common Language Patterns with ANTLR Grammars
Dealing with Precedence, Left Recursion, and Associativity
Recognizing Common Lexical Structures
Drawing the Line Between Lexer and Parser
6. Exploring Some Real Grammars
Parsing Comma-Separated Values
Parsing JSON
Parsing DOT
Parsing Cymbol
Parsing R
7. Decoupling Grammars from Application-Specific Code
Evolving from Embedded Actions to Listeners
Implementing Applications with Parse-Tree Listeners
Implementing Applications with Visitors
Labeling Rule Alternatives for Precise Event Methods
Sharing Information Among Event Methods
8. Building Some Real Language Applications
Loading CSV Data
Translating JSON to XML
Generating a Call Graph
Validating Program Symbol Usage
Part III—Advanced Topics
9. Error Reporting and Recovery
A Parade of Errors
Altering and Redirecting ANTLR Error Messages
Automatic Error Recovery Strategy
Error Alternatives
Altering ANTLR's Error Handling Strategy
10. Attributes and Actions
Building a Calculator with Grammar Actions
Accessing Token and Rule Attributes
Recognizing Languages Whose Keywords Aren't Fixed
11. Altering the Parse with Semantic Predicates
Recognizing Multiple Language Dialects
Deactivating Tokens
Recognizing Ambiguous Phrases
12. Wielding Lexical Black Magic
Broadcasting Tokens on Different Channels
Context-Sensitive Lexical Problems
Islands in the Stream
Parsing and Lexing XML
Part IV—ANTLR Reference
13. Exploring the Runtime API
Library Package Overview
Recognizers
Input Streams of Characters and Tokens
Tokens and Token Factories
Parse Trees
Error Listeners and Strategies
Maximizing Parser Speed
Unbuffered Character and Token Streams
Altering ANTLR's Code Generation
14. Removing Direct Left Recursion
Direct Left-Recursive Alternative Patterns
Left-Recursive Rule Transformations
15. Grammar Reference
Grammar Lexicon
Grammar Structure
Parser Rules
Actions and Attributes
Lexer Rules
Wildcard Operator and Nongreedy Subrules
Semantic Predicates
Options
ANTLR Tool Command-Line Options
A1. Bibliography
Index
– SYMBOLS –
– A –
– B –
– C –
– D –
– E –
– F –
– G –
– H –
– I –
– J –
– K –
– L –
– M –
– N –
– O –
– P –
– R –
– S –
– T –
– U –
– V –
– W –
– X –
Early Praise for The Definitive ANTLR 4 Reference Parr’s clear writing and lighthearted style make it a pleasure to learn the practical details of building language processors. ➤ Dan Bornstein Designer of the Dalvik VM for Android ANTLR is an exceptionally powerful and flexible tool for parsing formal languages. At Twitter, we use it exclusively for query parsing in our search engine. Our grammars are clean and concise, and the generated code is efficient and stable. This book is our go-to reference for ANTLR v4—engaging writing, clear descriptions, and practical examples all in one place. ➤ Samuel Luckenbill Senior manager of search infrastructure, Twitter, Inc. ANTLR v4 really makes parsing easy, and this book makes it even easier. It explains every step of the process, from designing the grammar to making use of the output. ➤ Niko Matsakis Core contributor to the Rust language and researcher at Mozilla Research I sure wish I had ANTLR 4 and this book four years ago when I started to work on a C++ grammar in the NetBeans IDE and the Sun Studio IDE. Excellent content and very readable. ➤ Nikolay Krasilnikov Senior software engineer, Oracle Corp.
This book is an absolute requirement for getting the most out of ANTLR. I refer to it constantly whenever I’m editing a grammar. ➤ Rich Unger Principal member of technical staff, Apex Code team, Salesforce.com I have been using ANTLR to create languages for six years now, and the new v4 is absolutely wonderful. The best news is that Terence has written this fantastic book to accompany the software. It will please newbies and experts alike. If you process data or implement languages, do yourself a favor and buy this book! ➤ Rahul Gidwani Senior software engineer, Xoom Corp. Never have the complexities surrounding parsing been so simply explained. This book provides brilliant insight into the ANTLR v4 software, with clear explanations from installation to advanced usage. An array of real-life examples, such as JSON and R, make this book a must-have for any ANTLR user. ➤ David Morgan Student, computer and electronic systems, University of Strathclyde
The Definitive ANTLR 4 Reference Terence Parr The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. Cover image by BabelStone (Own work) [CC-BY-SA-3.0 (http://creativecommons.org/licens- es/by-sa/3.0)], via Wikimedia Commons: http://commons.wikimedia.org/wiki/File%3AShang_dynasty_inscribed_scapula.jpg The team that produced this book includes: Susannah Pfalzer (editor) Potomac Indexing, LLC (indexer) Kim Wimpsett (copyeditor) David J Kelly (typesetter) Janet Furlow (producer) Juliet Benda (rights) Ellie Callahan (support) Copyright © 2012 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-93435-699-9 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—January 2013
Contents Acknowledgments Welcome Aboard! . . . . . . . . . . . . . . . . . . . . . . . ix xi Part I — Introducing ANTLR and Computer Languages 1. Meet ANTLR . . . . . . . . . . . Installing ANTLR 1.1 1.2 Executing ANTLR and Testing Recognizers . . . . . . 2. 3. 4. . . . . Let’s Get Meta! Implementing Parsers You Can’t Put Too Much Water into a Nuclear Reactor The Big Picture 2.1 2.2 2.3 2.4 Building Language Applications Using Parse Trees 2.5 Parse-Tree Listeners and Visitors . . . . . . A Starter ANTLR Project . 3.1 3.2 3.3 3.4 Building a Language Application The ANTLR Tool, Runtime, and Generated Code Testing the Generated Parser Integrating a Generated Parser into a Java Program . . . . . . . . . . . . . . . Matching an Arithmetic Expression Language A Quick Tour 4.1 4.2 Building a Calculator Using a Visitor 4.3 Building a Translator with a Listener 4.4 Making Things Happen During the Parse 4.5 Cool Lexical Features . . . . 3 3 6 9 9 11 13 16 17 21 22 24 26 27 31 32 38 42 46 50
Contents • vi Part II — Developing Language Applications with ANTLR Grammars . Deriving Grammars from Language Samples Designing Grammars 5.1 5.2 Using Existing Grammars as a Guide 5.3 Recognizing Common Language Patterns with ANTLR . . . . . . . . . Grammars 5.4 Dealing with Precedence, Left Recursion, and Associativity 5.5 Recognizing Common Lexical Structures 5.6 Drawing the Line Between Lexer and Parser . . . . . . . Exploring Some Real Grammars . 6.1 6.2 6.3 6.4 6.5 Parsing Comma-Separated Values Parsing JSON Parsing DOT Parsing Cymbol Parsing R Decoupling Grammars from Application-Specific Code . 7.1 7.2 7.3 7.4 7.5 Evolving from Embedded Actions to Listeners Implementing Applications with Parse-Tree Listeners Implementing Applications with Visitors Labeling Rule Alternatives for Precise Event Methods Sharing Information Among Event Methods . Building Some Real Language Applications . Loading CSV Data 8.1 8.2 Translating JSON to XML 8.3 Generating a Call Graph 8.4 Validating Program Symbol Usage . . . . Part III — Advanced Topics A Parade of Errors Error Reporting and Recovery 9.1 9.2 Altering and Redirecting ANTLR Error Messages 9.3 Automatic Error Recovery Strategy . . . . . . . . 57 58 60 61 69 72 79 83 84 86 93 98 102 109 110 112 115 117 119 127 127 130 134 138 149 149 153 158 5. 6. 7. 8. 9.
分享到:
收藏