The Definitive ANTLR 4 Reference (英語) ペーパーバック – 2013/1/22
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.
ANTLR3 is powerful in its own right, but you have to do more work to get an abstract syntax tree from your parsed input. That, and target language constructs appear in an ANTLR3 grammar. In ANTLR4, you no longer need to build an AST grammar from the input grammar because the listener/visitor implementations ANTLR4 emits relieve you of that. And, an ANTL4 grammar is generally target language agnostic, which means target language constructs no longer show up in the grammar (unless you really need them to). Which means at some point, you'll be able to specify the target language as option to the grammar compilation. Currently, Java is the default target language.
So if you're looking to get started with ANTLR, start with version4.
As for the material in the book: it's superb. It's extremely well written, completely lacks pith, and the concepts are paid out in exactly the right order at exactly the right pace. And there are plenty of examples. There aren't many books written this well.
ANTLR4 is highly evolved from where it started years ago. You'll enjoy working with it. I certainly am.
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.
If you want to use ANTLR, buy all of Terence's books that are applicable. He's been doing ANTLR for free for years and the books are top notch.
When I do Java lexing or parsing, I always use ANTLR 3 or 4. At the present time, I still use ANTLR 3 too because it has more targets for code generation. ANTLR 4's grammars are improved over ANTLR 3. I still miss the tree manipulation from ANTLR 3 though.
If you're forced to use Java, use ANTLR.