ANTLR is a parser generator that allows you to create a parser by describing grammar in one of the main programming languages. He himself is written in java and works great with Java.
Walkthrough:
1) Install Oracle Java JDK and Intellij Idea, (you can skip this step if they are already delivered), and run Intellij Idea
2) File-Setting-Plugins
Enter ANTLR in the search field and install the ANTLR v4 grammar plugin. You may need an additional search in all repositories.
3) For the Maven project add to pom.xml or create a new project.
in dependencies
<dependency> <groupId>org.antlr</groupId> <artifactId>antlr4-runtime</artifactId> <version>4.7</version> </dependency>
and 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>
Details https://github.com/antlr/antlr4/blob/master/doc/java-target.md
4) Next, create and add manually the grammar file with the extension .g4. The file name must match the word after grammar in the first line. It is made up like this: take what you need to parse, and break it into separate tokens. For tokens we describe lexemes, for example, all English letters [a-zA-Z] ;, all numbers [0-9], etc. For example, the example content from the official site for the file Hello.g4 is taken
// Define a grammar called Hello grammar Hello; r : 'hello' ID ; // match keyword hello followed by an identifier ID : [az]+ ; // match lower-case identifiers WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
5) Next, right-click on the second line of the file that starts with r and select the menu item Test Rule r
The grammar checker windows will open below. In this case, the plugin shows an error, most likely due to the fact that this is a test example, but the parser is generated. You can read about it here https://github.com/antlr/antlr4/issues/118 , and ignore it for now. But in real projects, it would be better to pay attention to these errors.
6) Click on the grammar file with the right mouse button, select the Configute ANTLR Recoqnizer menu item and generate the parser
After this, a success message will appear in the lower right corner.
7) Next, again click on the file with the right mouse button and select the Configute ANTLR menu item,
and there is a window for configuring file generation
In this window, enter data about the destination folder and programming language, in our case Java, whether visitor or listener is needed, as well as other required information, and click OK
And ANTLR then generates the files for recognition. However, although the output directory is specified, a new gen folder is often created at the root of the project, and java does not recognize these files.
In order for java to see these files, you must either mark the folder with the right mouse button "Mark Directory As" to "Generated Sources Root" in the gen folder.
And it should turn out like this:
8) ANTLR generated the following classes:
The HelloParser.java class is a description of the parser class, that is, the parser that corresponds to the Hello grammar:
public class HelloParser extends Parser { ... }
The HelloLexer.java class is a description of the lexer class, or lexical analyzer, that corresponds to the HelloInit grammar:
public class HelloLexer extends Lexer { ... }
Hello.tokens, HelloLexer.tokens are helper classes that contain information about tokens HelloListener.java, HelloBaseListener.java, HelloBaseVisitor, HelloVisitor are classes that contain descriptions of methods that allow you to perform certain actions when traversing a syntax tree
9) After that we will add the class HelloWalker (although this class is not mandatory, this code can be changed and added to Main to display 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" ); } }
10) And finally, the Main class is the entry point to the program.
public class Main { public static void main( String[] args) throws Exception { HelloLexer lexer = new HelloLexer(CharStreams.fromString("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); } }
11) Run the main method, and get the successfully processed parsing at the console output
Entering R : world Exiting R
→ The project code is posted here.
Source: https://habr.com/ru/post/341138/
All Articles