登录
首页 >  Golang >  Go问答

antlr 重新声明为导入的包名称

来源:stackoverflow

时间:2024-04-26 09:27:37 445浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《antlr 重新声明为导入的包名称》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我构建了一个目录 src,其中包含以下内容:

1
2
3
4
5
6
7
src/
  antlr/
   basic_levex.go
   basic_parse.go
   basicparser_base_visitor.go
   basicparser_visitor.go
  example1.go

antlr 文件看起来正确,并构建了名为 basiclexer.g4 和 basicparser.g4 的文件。

我的 example1.go 文件如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main
 
// example1.go
 
import (
    "./antlr"
    "fmt"
    "github.com/antlr/antlr4/runtime/go/antlr"
)
 
func main() {
    // setup the input
    is := antlr.newinputstream("1 + 2 * 3")
 
    // create the lexer
    lexer := antlr.newbasiclexer(is)
 
    // read all tokens
    for {
        t := lexer.nexttoken()
        if t.gettokentype() == antlr.tokeneof {
            break
        }
        fmt.printf("%s (%q)\n",
            lexer.symbolicnames[t.gettokentype()], t.gettext())
    }
}

我在编译时遇到的错误是

1
2
3
4
# command-line-arguments
./example1.go:8:2: antlr redeclared as imported package name
    previous declaration at ./example1.go:6:2
./example1.go:16:11: undefined: "github.com/antlr/antlr4/runtime/Go/antlr".NewBasicLexer

我真的不知道出了什么问题。如何修复它以便将来不再发生?


解决方案


让我们依次看看您的两个问题

重复导入

1
2
./example1.go:8:2: antlr redeclared as imported package name
    previous declaration at ./example1.go:6:2

错误消息很好地解释了发生的情况,包括以下行: 在第 8 行,您尝试导入与第 6 行已导入的名称相同的内容。 除此之外,Go Does not support relative import,就像Flimzy在评论中指出的那样。 要解决此问题,请删除第 6 行上的相对导入。

未定义的函数

1
./example1.go:16:11: undefined: "github.com/antlr/antlr4/runtime/Go/antlr".NewBasicLexer

您尝试使用的功能不存在。

在本例中,它实际上称为 newbaselexer,而不是 newbasiclexer,如您在 the code 中看到的那样

好了,本文到此结束,带大家了解了《antlr 重新声明为导入的包名称》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>