请帮我从 java 和 ANTLR 创建解析树
请帮我解决这个问题: 我在页面上执行示例:http://www.antlr。 org/wiki/pages/viewpage.action?pageId=1760
为了使用这个示例,我在 ANTLRWork 1.4 上构建了语法 P 并生成具有类 PLexer
和 的代码PParser
。 但是当我在 java Jcreator 4.5 上运行此代码时:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.runtime.debug.*;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import ParserAndLexer.*;
public class TestP {
public static void main (String[] args) throws Exception{
ANTLRInputStream input=new ANTLRInputStream(System.in);
PLexer lexer=new PLexer(input);
CommonTokenStream tokens=new CommonTokenStream(lexer);
ParseTreeBuilder builder=new ParseTreeBuilder("prog");
PParser parser=new PParser(tokens,builder);
parser.prog();
System.out.println(builder.getTree().toStringTree());
}
}
出现错误:
D:\CAO HOC\TAI LIEU THAM KHAO\ANTLR-EBNF\CREATE PARSTREE\TestP.java:14:
cannot find symbol
symbol : constructor PParser(org.antlr.runtime.CommonTokenStream,org.antlr.runtime.debug.ParseTreeBuilder)
location: class ParserAndLexer.PParser
PParser parser=new PParser(tokens,builder);
当我在 PParser(tokens,builder)
中删除一个参数 builder
时未找到该错误。但我不知道如何运行才能得到以下结果:
java TestP
int i;
i=4;
(<grammar prog> (prog (decl (type int) i ;) (stat i = (expr 4))))
Please help me this problem:
I do the example on the page: http://www.antlr.org/wiki/pages/viewpage.action?pageId=1760
in order use this example, i have built the grammar P on ANTLRWork 1.4 and generate code to have class PLexer
and PParser
.
But when i run this code on java Jcreator 4.5:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.runtime.debug.*;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import ParserAndLexer.*;
public class TestP {
public static void main (String[] args) throws Exception{
ANTLRInputStream input=new ANTLRInputStream(System.in);
PLexer lexer=new PLexer(input);
CommonTokenStream tokens=new CommonTokenStream(lexer);
ParseTreeBuilder builder=new ParseTreeBuilder("prog");
PParser parser=new PParser(tokens,builder);
parser.prog();
System.out.println(builder.getTree().toStringTree());
}
}
the error appear:
D:\CAO HOC\TAI LIEU THAM KHAO\ANTLR-EBNF\CREATE PARSTREE\TestP.java:14:
cannot find symbol
symbol : constructor PParser(org.antlr.runtime.CommonTokenStream,org.antlr.runtime.debug.ParseTreeBuilder)
location: class ParserAndLexer.PParser
PParser parser=new PParser(tokens,builder);
When I drop one argument builder
in PParser(tokens,builder)
then that error not found. but i don't know how to run to have the following result:
java TestP
int i;
i=4;
(<grammar prog> (prog (decl (type int) i ;) (stat i = (expr 4))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Nguyen,
为了完成这项工作,我发现当您生成解析器和词法分析器时,您需要包含“-debug”参数,如下所示:
这样,就会构建额外的构造函数。
Nguyen,
To make this work, I found that when you generate the parser and lexer you need to include the "-debug" argument like so:
That way, the extra constructor will be built.