ANTLR Hello world


Example

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
  2. HelloBaseListener.java
  3. HelloLexer.java
  4. HelloLexer.tokens
  5. HelloListener.java
  6. HelloParser.java

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