January 2013
Intermediate to advanced
328 pages
8h 5m
English
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 or download the file into /tmp/test |
| | $ antlr4 Hello.g4 # Generate parser and lexer using antlr4 alias from before |
| | $ ... |
Read now
Unlock full access