Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
虽然没有框架,但我的书的示例代码中有一些 JavaCC 和 JJTree 语法的测试示例。
Tom Copeland 是经典著作《使用 JavaCC 生成解析器》 的作者。所引用的示例代码是免费提供的。
下面是一个简单 Robot 语法的示例测试用例:
public class RobotTest { @Test public void tokenizeMoveCommand() { String cmd = "STEP 10"; SimpleCharStream cs = new SimpleCharStream(new StringReader(cmd)); RobotTokenManager ltm = new RobotTokenManager(cs); Token t = ltm.getNextToken(); assertEquals(RobotConstants.STEP, t.kind); t = ltm.getNextToken(); assertEquals(RobotConstants.NUM, t.kind); } @Test(expected = TokenMgrError.class) public void tokenizeFailure() { String cmd = "STOP 10"; SimpleCharStream cs = new SimpleCharStream(new StringReader(cmd)); RobotTokenManager ltm = new RobotTokenManager(cs); ltm.getNextToken(); } }
语法本身:
options { BUILD_PARSER=false; STATIC=false; } PARSER_BEGIN(Robot) public class Robot {} PARSER_END(Robot) TOKEN_MGR_DECLS: { public static void main(String[] args) throws Exception { java.io.Reader r = new java.io.FileReader(args[0]); SimpleCharStream scs = new SimpleCharStream(r); RobotTokenManager mgr = new RobotTokenManager(scs); for (Token t = mgr.getNextToken(); t.kind != EOF; t = mgr.getNextToken()) { System.out.println("Found a " + RobotConstants.tokenImage[t.kind] + ": " + t.image); } } } SKIP : { " " | "\n" | "\r" | "\r\n" } TOKEN : { <STEP : "STEP"> | <RIGHT : "RIGHT"> | <LEFT : "LEFT"> | <NUM : (["1"-"9"])+ (["0"-"9"])*> }
There's not a framework, but there are some examples of tests for both JavaCC and JJTree grammars in my book's example code.
Tom Copeland is the author of the classic Generating Parsers with JavaCC. The example code which is referred to is available for free.
Below is a sample test case for a simple Robot grammar:
The grammar itself:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(1)
虽然没有框架,但我的书的示例代码中有一些 JavaCC 和 JJTree 语法的测试示例。
Tom Copeland 是经典著作《使用 JavaCC 生成解析器》 的作者。所引用的示例代码是免费提供的。
下面是一个简单 Robot 语法的示例测试用例:
语法本身:
There's not a framework, but there are some examples of tests for both JavaCC and JJTree grammars in my book's example code.
Tom Copeland is the author of the classic Generating Parsers with JavaCC. The example code which is referred to is available for free.
Below is a sample test case for a simple Robot grammar:
The grammar itself: