Deriving Grammars from Language Samples

Writing a grammar is a lot like writing software except that we work with rules instead of functions or procedures. (Remember that ANTLR generates a function for each rule in your grammar.) But, before focusing on the rule innards, it’s worth discussing the overall anatomy of a grammar and how to form an initial grammar skeleton. That’s what we’ll do in this section because it’s an important first step in any language project. If you’re itching to build and execute your first parser, you can revisit Chapter 4, ​A Quick Tour​ or jump to the first example in the next chapter: ​Parsing Comma-Separated Values​. Feel free to pop back and forth to the examples in the next chapter as we learn ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.