Build Antlr4 projects with eclipse java project template.

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.

word-image

word-image
2. Add java project facet to the project.

word-image

word-image

word-image

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

word-image

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

word-image

word-image

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

word-image

word-image

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

word-image

word-image

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

word-image

word-image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值