How can I add library import to antlr4 in go?

huangapple go评论64阅读模式
英文:

How can I add library import to antlr4 in go?

问题

我最近开始学习antlr和它在go中的使用。在使用go库时,我遇到了导入的问题。例如,我在我的antlr文件开头写了以下内容:

grammar Test;

@header {
    import "strconv"
}

然后使用以下命令编译它:antlr4 -Dlanguage=Go Test.g4。然后我有两个文件,一个是使用这个包的parser文件,另一个是包含一个未使用的导入的lexer文件,这就是为什么我无法编译我的项目的原因。

我希望通过一些标志的帮助,我能够编译我的项目。

英文:

I started studying antlr and its work with go recently. And I have a problem with importing when using the library inside go. For example, I have at the beginning of my antlr file:

grammar Test;

@header {
    import "strconv"
}

And compile it with the following command antlr4 -Dlanguage=Go Test.g4. And then I have two files parser which uses this package and lexer which contains an import that is not used, which is why I cannot compile my project.

I expect that with the help of some flags I will be able to compile my project.

答案1

得分: 1

使用@parser::header只在解析器中包含它。

英文:

Use @parser::header to include it only in the parser.

答案2

得分: 0

你可以使用一个技巧来强制使用该包,例如通过写入var _= strconv.Atoi。然后头部将如下所示:

@header {
    import "strconv"
    var _ = strconv.Atoi
}
英文:

You can use a trick and force the package to be used, for example, by writing var _= strconv.Atoi. Then the header will look like this:

@header {
    import "strconv"
    var _ = strconv.Atoi
}

huangapple
  • 本文由 发表于 2022年12月15日 03:54:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/74803547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定