[英]How to import a custom java class to my Antlr Grammar?
In a directory called Test I have a java class in a file Word.java
like this: 在名为Test的目录中,我在Word.java
文件中有一个java类,如下所示:
public class Word {
public String value;
public Word(String v) {
value = v;
}
}
And in the same directory my antlr4 grammar Test.g4 like this: 在同一目录下我的antlr4语法Test.g4是这样的:
grammar Test;
@header {
import Test.Word;
}
@members {
ArrayList<Word> words = new ArrayList<Word>();
}
program
@after { System.out.println(words.toString()); }
: (ID { words.add(new Word($ID.text)); } )+
;
(In this example I'm just using Word instead of String to test the import thing. The grammar and the actions are not my problem yet) (在这个例子中,我只是使用Word而不是String来测试导入的东西。语法和动作不是我的问题)
Now, when I try to run java org.antlr.v4.runtime.misc.TestRig Test
this is the outcome: 现在,当我尝试运行java org.antlr.v4.runtime.misc.TestRig Test
这就是结果:
blablabla blablabla
Caused by: java.lang.ClassNotFoundException: Test.Word 引起:java.lang.ClassNotFoundException:Test.Word
blablabla blablabla
This is driving me crazy, how can I import my class in the .g4 file? 这让我发疯,如何在.g4文件中导入我的类? I know that default java packages can be load but how do I import my custom java class? 我知道可以加载默认的java包,但是如何导入我的自定义java类?
tl;dr: I created a java class and want to use it inside my antlr grammar. tl; dr:我创建了一个java类,想在我的antlr语法中使用它。 How can I do that? 我怎样才能做到这一点?
您的CLASSPATH必须包含包含Test的dir。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.