The Definitive ANTLR Reference: Building Domain-Specific Languages (Pragmatic Programmers)

The Definitive ANTLR Reference: Building Domain-Specific Languages (Pragmatic Programmers)

Terence Parr

Language: English

Pages: 376

ISBN: 0978739256

Format: PDF / Kindle (mobi) / ePub


ANTLR v3 is the most powerful, easy-to-use parser generator built to date, and represents the culmination of more than 15 years of research by Terence Parr. This book is the essential reference guide to using this completely rebuilt version of ANTLR, with its amazing new LL() parsing technology, tree construction facilities, StringTemplate code generation template engine, and sophisticated ANTLRWorks GUI development environment. Learn to use ANTLR directly from the author!

ANTLR is a parser generator-a program that generates code to translate a specified input language into a nice, tidy data structure. You might think that parser generators are only used to build compilers. But in fact, programmers usually use parser generators to build translators and interpreters for domain-specific languages such as proprietary data formats, common network protocols, text processing languages, and domain-specific programming languages.

Domain-specific languages are important to software development because they represent a more natural, high fidelity, robust, and maintainable means of encoding a problem than simply writing software in a general-purpose language. For example, NASA uses domain-specific command languages for space missions to improve reliability, reduce risk, reduce cost, and increase the speed of development. Even the first Apollo guidance control computer from the 1960s used a domain-specific language that supported vector computations.

This book is the definitive guide to using the completely rebuilt ANTLR v3 and describes all features in detail, including the amazing new LL(

) parsing technology, tree construction facilities, StringTemplate code generation template engine, and sophisticated ANTLRWorks GUI development environment. You'll learn all about ANTLR grammar syntax, resolving grammar ambiguities, parser fault tolerance and error reporting, embedding actions to interpret or translate languages, building intermediate-form trees, extracting information from trees, generating source code, and how to use the ANTLR Java API.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

find ac- cessible. The next chapter demonstrates how LL(*) augmented with predicates can resolve some difficult language recognition problems. Chapter 12 Using Semantic and Syntactic Predicates LL(*) is a powerful extension to LL(k) that makes it much easier to write natural grammars and build grammars for difficult languages. The pre- vious chapter explained how LL(*) uses DFAs to scan arbitrarily far ahead looking for input symbols and sequences that distinguish alter- natives. LL(k),

rules we’ll need: Download tour/basic/Expr.g ID : ('a'..'z' |'A'..'Z' )+ ; INT : '0'..'9' + ; NEWLINE:'\r' ? '\n' ; WS : (' ' |'\t' |'\n' |'\r' )+ {skip();} ; Rule WS (whitespace) is the only one with an action (skip();) that tells ANTLR to throw out what it just matched and look for another token. The easiest way to work with ANTLR grammars is to use ANTLRWorks,1 which provides a sophisticated development environment (see also Sec- tion 1.5, ANTLRWorks Grammar Development

essentially a list of rules describing the structure of a particular language. From this list of rules, ANTLR generates a recursive-descent parser that rec- ognizes sentences in that language (recursive-descent parsers are the kind of parsers you write by hand). The language might be a program- ming language or simply a data format, but ANTLR does not intuitively know anything about the language described by the grammar. ANTLR is merely building a recognizer that can answer this question:

Bookshelf and the linking g device are trademarks 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

and recov- erFromMismatchSet( ), and alter how the parser responds to thrown exceptions. Download errors/Bail.g grammar Bail; @members { protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { throw new MismatchedTokenException(ttype, input); } public void recoverFromMismatchedSet(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { throw e; } } // Alter code generation so catch-clauses get replace with

Download sample

Download