Executing ANTLR and Testing Recognizers
Hereâs a simple grammar that recognizes phrases like hello parrt and hello world:
â | âgrammarâ Helloâ;â â// Define a grammar called Helloâ |
â | r â:â â'hello'â ID â;â â// match keyword hello followed by an identifierâ |
â | ID â:â â[âaâ-âzâ]+â â;â â// match lower-case identifiersâ |
â | WS â:â â[â â\âtâ\ârâ\ânâ]+â â->â skip â;â â// skip spaces, tabs, newlines, \r (Windows)â |
To keep things tidy, letâs put grammar file Hello.g4 in its own directory, such as /tmp/test. Then we can run ANTLR on it and compile the results.
â | â$ ââcdââ ââ/tmp/testâ |
â | â$ ââ# copy-n-paste Hello.g4 ... |
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.