..
2023-08-08
Setting up ANTLR with IntelliJ IDEA
- Install the ANTLR plugin
- Open a new project and create a simple grammar file
g.g4
grammar g;
s : 'hello' ID;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
- This grammar has 3 rules - 2 lexer rule and 1 parser rule
- Lexer rules are written in uppercase whereas parser rules are written in lowercase
- You can test this grammar by clicking on the button at the bottom
- Given the following input, a parse tree will be generated
hello c
ANTLR Code Generation
- You can generate codes for a given grammar by right clicking anywhere
with the
.g4
file open and then selecting Generate ANTLR Recognizer
- By default this will generate the code in a seperate folder. To change this behavior right click -> Configure ANTLR and choose the current working directory as the output path
-
Note that relative paths like
./
or../
may not work. You have to select the absolute path to the src directory -
By default the code generated is in Java. This too can be changed following a similar method to above.
Sample ANTLR Project
- Create a new Java Project
- Create a
Hello.g4
file inside thesrc/
directory
lexer grammar Hello;
IF: 'if';
ID : ('a'..'z')('a'..'z'| '0'..'9'|'_')*;
WS : [ \r\n\t]+ -> skip;
- You can either configure antlr to generate files directly to the
src
directory or mark thegen
directory as Generated Sources Root
-
Now press
⌘ + ;
(or whatever the shortcut is in Windows) to open the Project Structure -
Go to Libraries
-
Click on the
+
icon and then click on From Maven -
Search for
org.antlr:antlr4-runtime:x.x.x
-
Please check your plugin version to see what is bundled with it
-
Both of them should match
- create a
Main.java
file with the following
import java.io.IOException;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Token;
public class Main {
public static void main(String[] args) throws IOException {
CharStream input = CharStreams.fromFileName(args[0]);
Hello lex = new Hello(input);
Token token;
while ((token = lex.nextToken()).getType() != -1) {
System.out.printf("%s:%d:%d: token <%s, %s>\n", args[0],
token.getLine(), token.getStartIndex(),
token.getType(), token.getText());
}
}
}
- Create a file
input
with
if186 if addfggh 33455 erry
ggjhj 1234dfff ffggg
- Build and run to get the following
input:1:0: token <2, if186>
input:1:6: token <1, if>
input:1:9: token <2, addfggh>
input:1:23: token <2, erry>
input:2:28: token <2, ggjhj>
input:2:38: token <2, dfff>
input:2:43: token <2, ffggg>
line 1:17 token recognition error at: '3'
line 1:18 token recognition error at: '3'
line 1:19 token recognition error at: '4'
line 1:20 token recognition error at: '5'
line 1:21 token recognition error at: '5'
line 2:6 token recognition error at: '1'
line 2:7 token recognition error at: '2'
line 2:8 token recognition error at: '3'
line 2:9 token recognition error at: '4'
Process finished with exit code 0