ANTLR Getting started with ANTLR Hello world

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any ANTLR Question?