gpt4 book ai didi

java - 从 Java 生成 Antlr4 Lexer

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:41 25 4
gpt4 key购买 nike

我知道,大多数人让 Antlr 从命令行生成,但我想从 Java 调用它。我也不能调用 Tool 类的 main,因为这个类使用 System.exit,因为这也会退出我的程序。

到目前为止我尝试的是以下内容:

        String g4Content = "My Grammar Content";            

String[] arg0 = {pathOfG4File, "-package", "mypackage"};

Tool tool = new Tool(arg0);

GrammarRootAST ast = tool.parseGrammarFromString(g4Content)
Grammar grammar = tool.createGrammar(ast);

tool.process(grammar, true)

我得到以下异常:

java.lang.NoSuchMethodError: org.antlr.v4.parse.BlockSetTransformer.inContext(Ljava/lang/String;)Z
at org.antlr.v4.parse.BlockSetTransformer.setAlt(BlockSetTransformer.java:467)
at org.antlr.v4.parse.BlockSetTransformer.topdown(BlockSetTransformer.java:370)
at org.antlr.v4.parse.BlockSetTransformer.topdown(BlockSetTransformer.java:24)
at org.antlr.runtime.tree.TreeRewriter$2.rule(TreeRewriter.java:108)
at org.antlr.runtime.tree.TreeRewriter.applyOnce(TreeRewriter.java:61)
at org.antlr.runtime.tree.TreeRewriter$1.pre(TreeRewriter.java:92)
at org.antlr.runtime.tree.TreeVisitor.visit(TreeVisitor.java:28)
at org.antlr.runtime.tree.TreeVisitor.visit(TreeVisitor.java:33)
at org.antlr.runtime.tree.TreeVisitor.visit(TreeVisitor.java:33)
at org.antlr.runtime.tree.TreeVisitor.visit(TreeVisitor.java:33)
at org.antlr.runtime.tree.TreeVisitor.visit(TreeVisitor.java:33)
at org.antlr.runtime.tree.TreeRewriter.downup(TreeRewriter.java:95)
at org.antlr.runtime.tree.TreeRewriter.downup(TreeRewriter.java:86)
at org.antlr.v4.tool.GrammarTransformPipeline.reduceBlocksToSets(GrammarTransformPipeline.java:89)
at org.antlr.v4.tool.GrammarTransformPipeline.process(GrammarTransformPipeline.java:78)
at org.antlr.v4.Tool.process(Tool.java:360)
at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080)
at de.ustutt.sidewise.editor.generator.BnfModelGenerator.doGenerate(BnfModelGenerator.java:53)
at org.eclipse.xtext.builder.BuilderParticipant.handleChangedContents(BuilderParticipant.java:524)
at org.eclipse.xtext.builder.BuilderParticipant.handleChangedContents(BuilderParticipant.java:513)
at org.eclipse.xtext.builder.BuilderParticipant.doGenerate(BuilderParticipant.java:498)
at org.eclipse.xtext.builder.BuilderParticipant.doBuild(BuilderParticipant.java:263)
at org.eclipse.xtext.builder.BuilderParticipant.build(BuilderParticipant.java:221)
at org.eclipse.xtext.builder.impl.RegistryBuilderParticipant$DeferredBuilderParticipant.build(RegistryBuilderParticipant.java:161)
at org.eclipse.xtext.builder.impl.RegistryBuilderParticipant.build(RegistryBuilderParticipant.java:69)
at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:252)
at org.eclipse.xtext.builder.impl.XtextBuilder.fullBuild(XtextBuilder.java:280)
at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:117)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:734)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:205)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:245)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:300)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:303)
at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:359)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:382)
at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:144)
at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:235)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

最佳答案

这是一个已知的工作实现:

private void compileGrammar(IResource resource, IProgressMonitor monitor) {
if (resource != null && resource instanceof IFile && (resource.getName().endsWith(".g4"))) {
IFile file = (IFile) resource;

try {
String outputDirectory = determineBuildFolder(file).toString();
Log.info(this, "Building [file=" + file.getFullPath() + "]");
Log.info(this, "Output to [dir= " + outputDirectory + "]");
monitor.worked(1);

Tool tool = new Tool(new String[] { "-o", outputDirectory });
tool.removeListeners();
ErrorListener toolErrs = new ErrorListener();
tool.addListener(toolErrs);
monitor.worked(1);

// Prep and process the grammar file
String loc = file.getLocation().toPortableString();
Grammar g = tool.loadGrammar(loc);
tool.process(g, true);
monitor.worked(1);

if (toolErrs.hasErrors()) {
for (ANTLRMessage err : toolErrs.getErrList()) {
Log.error(this, err.toString());
AntlrDTCore.getDefault().showConsoleMessage(err.toString(), AntlrDTCore.MSG_ERROR);
}
}
if (toolErrs.hasWarnings()) {
for (ANTLRMessage warn : toolErrs.getWarnList()) {
Log.warn(this, warn.toString());
AntlrDTCore.getDefault().showConsoleMessage(warn.toString(), AntlrDTCore.MSG_WARNING);
}
}

postCompileCleanup(file, monitor);
monitor.worked(1);
} catch (Exception e) {
Log.error(this, "Build failed.", e);
}
}
}

顺便说一句,看起来您正在使用 XText - 最后我检查它使用的是经过修改的 Antlr3 实现。

关于java - 从 Java 生成 Antlr4 Lexer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com