Build Antlr4 projects with eclipse java project template.
2015-07-20 10:35
627 查看
from:https://shijinglu.wordpress.com/2015/01/22/build-antlr4-projects-with-eclipse-java-project-template/
Posted on January 22, 2015 by shijingluBefore taking any action, remember to install antlr4 plugin for eclipse, it can be easily found from “Eclipse Marketplace”.
1. Create an antlr4 project. This will create an simple “Hello” antlr4 project.
![](https://i2.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image.png)
![](https://i0.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image1.png)
2. Add java project facet to the project.
![](https://i0.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image2.png)
![](https://i2.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image3.png)
![](https://i2.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image4.png)
3. Eclipse will automatically rebuild the project, see messages in the Console, pay attention to the default antlr-4.4-complete.jar path.
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image5.png)
4. Or you can move the antlr4 jar file to your comfortable directory and add it the eclipse build path from therein.
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image6.png)
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image8.png)
5. Add destination folder of generated java files to the project as source folder.
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image8.png)
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image8.png)
6. Add some code to test this project, <>
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image9.png)
![](https://i2.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image10.png)
// // import ANTLR's runtime libraries import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; public class HelloRunner { public static void main( String[] args) throws Exception { // create a CharStream that reads from standard input ANTLRInputStream input = new ANTLRInputStream( System.in); // create a lexer that feeds off of input CharStream HelloLexer lexer = new HelloLexer(input); // create a buffer of tokens pulled from the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); // create a parser that feeds off the tokens buffer HelloParser parser = new HelloParser(tokens); ParseTree tree = parser.r(); // begin parsing at rule 'r' System.out.println(tree.toStringTree(parser)); // print LISP-style tree } }
7. Runt this HelloRunner.java as java application
![](https://i1.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image9.png)
![](https://i0.wp.com/54.174.23.86/wp-content/uploads/2015/01/word-image12.png)
相关文章推荐
- Java 反射机制 举例
- JAVA中AES加密方法实例分析
- java:浅谈java环境变量
- 在服务器上发布Java Web项目
- SpringMVC:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax;
- [LeetCode][Java] Restore IP Addresses
- Java 内存分配浅析
- Java生成非对称型加密公钥和私钥的方法
- 【JavaWeb】(8)监听器
- java装饰者模式理解
- JAVA Date类与String类
- Java模拟QQ桌面截图功能实现方法
- JAVA数据类型转换
- MyEclipse方法注解自动生成
- Java Properties 类读取和修改配置文件信息
- JAVA常用类之——Date,Calendar,DateFomat
- Java Properties 类读取配置文件信息
- Java常见英语单词
- struts运用jfreechart生成统计图教程
- Java通过JNI调用dll详细过程(转)