如何将自定义java类导入我的Antlr语法? [英] How to import a custom java class to my Antlr Grammar?

查看:126
本文介绍了如何将自定义java类导入我的Antlr语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在名为Test的目录中,我在文件中有一个java类 Word.java ,如下所示:

In a directory called Test I have a java class in a file Word.java like this:

public class Word {
  public String value;

  public Word(String v) {
    value = v;
  }
}

在同一目录下我的antlr4语法Test.g4像这样:

And in the same directory my antlr4 grammar Test.g4 like this:

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)); } )+
  ;

(在这个例子中,我只是使用Word而不是String来测试导入的东西。语法并且行动不是我的问题)

(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)

现在,当我尝试运行 java org.antlr.v4.runtime.misc.TestRig Test时这是结果:

Now, when I try to run java org.antlr.v4.runtime.misc.TestRig Test this is the outcome:


blablabla

blablabla

引起:java.lang.ClassNotFoundException:Test.Word

Caused by: java.lang.ClassNotFoundException: Test.Word

blablabla

blablabla

这让我发疯,如何在.g4文件中导入我的类?我知道可以加载默认的java包但是如何导入我的自定义java类?

This is driving me crazy, how can I import my class in the .g4 file? I know that default java packages can be load but how do I import my custom java class?

tl; dr:我创建了一个java类并且想要使用它在我的antlr语法中。我怎么能这样做?

推荐答案

你的 CLASSPATH 必须包含包含Test的目录。

Your CLASSPATH will have to include dir containing Test.

这篇关于如何将自定义java类导入我的Antlr语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆