The Definitive ANTLR 4 Reference (英語) ペーパーバック – 2013/1/25
Kindle 端末は必要ありません。無料 Kindle アプリのいずれかをダウンロードすると、スマートフォン、タブレットPCで Kindle 本をお読みいただけます。
Programmers run into parsing problems all the time. Whether it's a data format like JSON, a network protocol like SMTP, a server configuration file for Apache, a PostScript/PDF file, or a simple spreadsheet macro language--ANTLR v4 and this book will demystify the process. ANTLR v4 has been rewritten from scratch to make it easier than ever to build parsers and the language applications built on top. This completely rewritten new edition of the bestselling Definitive ANTLR Reference shows you how to take advantage of these new features.
Build your own languages with ANTLR v4, using ANTLR's new advanced parsing technology. In this book, you'll learn how ANTLR automatically builds a data structure representing the input (parse tree) and generates code that can walk the tree (visitor). You can use that combination to implement data readers, language interpreters, and translators.
You'll start by learning how to identify grammar patterns in language reference manuals and then slowly start building increasingly complex grammars. Next, you'll build applications based upon those grammars by walking the automatically generated parse trees. Then you'll tackle some nasty language problems by parsing files containing more than one language (such as XML, Java, and Javadoc). You'll also see how to take absolute control over parsing by embedding Java actions into the grammar.
You'll learn directly from well-known parsing expert Terence Parr, the ANTLR creator and project lead. You'll master ANTLR grammar construction and learn how to build language tools using the built-in parse tree visitor mechanism. The book teaches using real-world examples and shows you how to use ANTLR to build such things as a data file reader, a JSON to XML translator, an R parser, and a Java class->interface extractor. This book is your ticket to becoming a parsing guru!
What You Need:
ANTLR 4.0 and above. Java development tools. Ant build system optional(needed for building ANTLR from source)
""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.
In chapter four the author is showing a lot of cool things that ANTLR can do for you, and the grammars that help accomplish that. However, he does not officially teach you how to write a grammar until chapter five. Chapter six shows you how you could use ANTLR to parse existing syntaxes, but it does not really teach you anything new. I wish the author had condensed his sales pitch to five pages or less, instead of dedicating whole chapters to it. In other words, I would have put chapters four and six somewhere farther back in the book, and focused on really teaching you the syntax of ANTLR grammars, and how to use its features.
Despite that, the book did a great job of explaining how to write ANTLR grammars and use them in your code. All of the online documentation I found on ANTLR was horrible, trying to get by without this book is crazy. Along the way, I came to love ANTLR as a tool, since it could save me a ton of development time, and since it supports so many great features. It's just a really powerful tool by itself.
I would highly recommend this book to anybody.
It turns out that Terence Parr seemed to have a similar experience, and decided to do something about it. ANTLR4 is an amazingly powerful tool for parsing. The reference is a great place to start to develop your own *.g4 ANTLR grammar file.
I found getting a parser using ParseTreeListeners was pretty easily accomplished, and the resulting JAVA code can be given to other non parser folks (the Listener code you write... not the auto generated stuff). In the past I always found anything associated with lexx/yacc/bison was too convoluted to explain to anyone. But ANTLR4 care and feeding is human managable.
The one issue I have with the language deals with large HUGE files which consume too much memory. I'm trying to figure a way to use parse tree listeners and "dump" the parse tree periodically. The solutions for large files currently appear to require you use grammar actions, which are great, and easier to develop than the parse tree listeners... but ANTLR4's advantage over ANTLR3 is that you don't have to put any parser actions into the grammar... I find that a bonus, but that seems to limit processing for large files.
There is just one shortcoming in this book: it is very difficult to find exactly the information you're looking for.
- Want to know how to import grammars? Well that's clearly under the "Matching an Arithmetic Expression Language" section of Chapter 4: A Quick Tour.
- Want to split a lexer and parser into separate files? I know I read that *somewhere* in this book, but now I can't find it. Let's turn to the index and look up "options", but there's no entry for "options" in the index. Ok, found it under the "Grammar" index heading.
Maybe I'm nitpicking, but when I'm buying a Capital-D Capital-R Definitive Reference of anything, I'm expecting to be able to navigate easily to the information that I need quickly. This book suffers shortcomings in these three things:
1) The book needs a more detailed table of contents that includes section subheadings, which is where all the useful detail headings exist.
2) The book needs a better cross-referenced index. Put an "O -> Options -> See Grammar Options" so that new readers aren't penalized for not yet having a firm grasp of a new vocabulary they're trying to learn. Good indexes are actually a helpful learning tool.
3) The Reference portion of the book could use it's own TOC, or a cheat-sheet with corresponding page reference within the book. Often, you forget the exact thing you're looking for, and seeing a grouped list of keywords is helpful when you're trying to get something to jump out at you and jog your memory.
As a result of these shortcomings, I'm more frequently turning to Google to find what I'm looking for because this book makes you work hard to find what part of the reference has the references you need to refer to. I would say that this is a flaw in any reference book, not a feature.