码迷,mamicode.com
首页 > 编程语言 > 详细

Build Antlr4 projects with eclipse java project template.

时间:2015-07-20 12:38:38      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

from:https://shijinglu.wordpress.com/2015/01/22/build-antlr4-projects-with-eclipse-java-project-template/

Before 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.

技术分享

技术分享
2. Add java project facet to the project.

技术分享

技术分享

技术分享

3. Eclipse will automatically rebuild the project, see messages in the Console, pay attention to the default antlr-4.4-complete.jar path.

技术分享

4. Or you can move the antlr4 jar file to your comfortable directory and add it the eclipse build path from therein.

技术分享

技术分享

5. Add destination folder of generated java files to the project as source folder.

技术分享

技术分享

6. Add some code to test this project, <>

技术分享

技术分享

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

技术分享

技术分享

Build Antlr4 projects with eclipse java project template.

标签:

原文地址:http://www.cnblogs.com/dfyz/p/4660754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!