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

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


src/

  antlr/

   basic_levex.go

   basic_parse.go

   basicparser_base_visitor.go

   basicparser_visitor.go

  example1.go

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


我的示例1.go文件如下所示:


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

    }

}

我在编译时遇到的错误是


# 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

我真的不知道出了什么问题。如何修复它,使其将来不会发生?


精慕HU
浏览 255回答 1
1回答

米脂

让我们一个接一个地看看你的两个问题重复导入./example1.go:8:2: antlr redeclared as imported package name    previous declaration at ./example1.go:6:2错误消息很好地解释了发生了什么,包括以下行:在第8行上,您尝试导入的名称与已在第6行上导入的名称相同。除此之外,Go不支持相对进口,就像Flimzy在评论中指出的那样。要解决此问题,请删除第 6 行上的相对导入。未定义的函数./example1.go:16:11: undefined: "github.com/antlr/antlr4/runtime/Go/antlr".NewBasicLexer您尝试使用的函数不存在。在这种情况下,它实际上是被调用的,而不是您在代码中看到的NewBaseLexerNewBasicLexer
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go