ANTLR Getting started with ANTLR

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 Insert
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees.

Antlr Versions
Antlr is separated in two big parts, the grammar (grammar files) and the generated code files, which derive from the grammar based on target language. The antlr versions are in the format of V1.V2.V3 :

  • V1: Change in V1 means that new syntax of features were introduced in grammar files
  • V2: Change in V2 means that new features or major fixes were introduced in the generated files (e.g addition of new functions)
  • V3: stands for bug fixes or minor improvements

Runtime Libraries and Code Generation Targets
The Antlr tool is written in Java, however it is able to generate parsers and lexers in various languages. To run the parser and lexer you will also need having the runtime library of antlr alongside with the parser and lexer code. The supported target language (and runtime libraries) are the following:

  • Java

  • C#

  • Python (2 and 3)

  • JavaScript

Versions

VersionRelease Date
2.01997-05-01
3.02011-01-19
4.02013-01-21
4.12013-07-01
4.22014-02-05
4.2.12014-03-25
4.2.22014-04-07
4.32014-06-19
4.42014-07-16
4.52015-01-23
4.5.12016-07-16
4.5.22016-01-30
4.5.32016-03-31
4.62016-12-15
4.72017-03-30

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
  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?