ANTLR Hello world


A simple hello world grammar can be found here:

// define a grammar called Hello
grammar Hello;
r   : 'hello' ID;
ID  : [a-z]+ ;
WS  : [ \t\r\n]+ -> skip ;

To build this .g4 sample you can run the following command from your operating systems terminal/command-line:

Java -jar antlr-4.5.3-complete.jar Hello.g4

//OR if you have setup an alias or use the recommended batch file

antlr4 Hello.g4

Building this example should result in the following output in the Hello.g4 file directory:

  1. Hello.tokens
  4. HelloLexer.tokens

When using these files in your own project be sure to include the ANTLR jar file. To compile all of these files using Java, in the same operating directory or by path run the following command:

javac *.java