Grammar Structure
A grammar is essentially a grammar declaration followed by a list of rules but has the following general form:
â | â/** Optional Javadoc-style comment */â |
âââ â | âgrammarâ Nameâ;â |
â | âoptionsâ â{...}â |
â | âimportâ â...â â;â |
â | âtokensâ â{...}â |
â | â@actionNameâ â{...}â |
â | |
â | rule1 â// parser and lexer rules, possibly intermingledâ |
â | â...â |
â | ruleN |
The filename containing grammar X must be called Xg4. You can specify options, imports, token specifications, and actions in any order. There can be at most one each of options, imports, and token specifications. All of those elements are optional except for the header ââ â and at least one rule. Rules take ...
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.