-
Inslall Oracle Java JDK и Intellij Idea, (you can skip this step), and run Intellij Idea
-
File-Setting-Plugins
Enter in the search field ANTLR and install plugin ANTLR v4 grammar plugin. Perhaps, you will need an additional search on all repositories.
- For the Maven project add into pom.xml in dependencies
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7</version>
</dependency>
in plugins
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
View details https://github.com/antlr/antlr4/blob/master/doc/java-target.md
- Next, create file manually a grammar file with the extension.g4 and add it into project. For example, this example is from the official site called Hello.g4
// Define a grammar called Hello
grammar Hello;
r : 'hello' ID ; // match keyword hello followed by an identifier
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
- Next, right-click on the second line of the file, which starts with "r" and select the menu item Test Rule r
- Click the grammar file with the right mouse button, select the menu item Configute ANTLR Recoqnizer and generate a parser
- Next click the file again with the right click and select the menu item Configute ANTLR,
and a window opens for configuring the generation of files
Fill the fields as in the picture and click OK
ANTLR generates files for recognition. However, although the output directory is specified, a new gen folder in the project root is often created, and java does not recognize these files.
The folder should either be marked with the right mouse button "Mark Directory As" on "Generated Sources Root" on the gen folder
It should be like this picture
- ANTLR generated such classes: Класс HelloParser.java -this is the description of the parser class of the corresponding grammar Hello:
public class HelloParser extends Parser { ... }
Класс HelloLexer.java - this is the description of the class of the lexer of the corresponding grammar HelloInit:
public class HelloLexer extends Lexer { ... }
Hello.tokens, HelloLexer.tokens - These are helper classes that contain information about tokens HelloListener.java, HelloBaseListener.java, HelloBaseVisitor, HelloVisitor - these are classes that contain method descriptions that allow you to perform certain actions when traversing a syntax tree
- Then add the class HelloWalker (although this class is not required, this code can be changed and added to the Main for outputting information)
public class HelloWalker extends HelloBaseListener {
public void enterR(HelloParser.RContext ctx ) {
System.out.println( "Entering R : " + ctx.ID().getText() );
}
public void exitR(HelloParser.RContext ctx ) {
System.out.println( "Exiting R" );
}
}
- And add class Main - entry point
public class Main {
public static void main( String[] args) throws Exception
{
HelloLexer lexer = new HelloLexer(new ANTLRInputStream("hello world"));
CommonTokenStream tokens = new CommonTokenStream( lexer );
HelloParser parser = new HelloParser( tokens );
ParseTree tree = parser.r();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk( new HelloWalker(), tree );
}
}
- Run the method main
Entering R : world
Exiting R