ANTLR v4 Grammar

Learn the basic constructs of the ANTLR’s language, g4.

This section will teach us how to drive grammar from language specifications. Starting with ANTLR, we will identify common language patterns and, finally, dig into details of building language applications.

Language design

First, we need to understand the overall structure of language before we can build grammar. Languages are based on grammatical structures. For example, “the cat (subject) chased (verb) the mouse (object)” is a valid grammatical structure in the English language. The process of writing grammar is similar to that of writing software, except that we write rules, rather than functions or procedures. However, we have to figure out which rules we need and which rule is the start rule (such as the main method).

Let’s start with imagining some rules for a comma-separated value (CSV) as an example where a file has a sequence of rows ...