首页 > 解决方案 > Golang Antlr 找不到解析器的路径

问题描述

我正在尝试使用 Antlr 生成的 Golang 解析器。但是当我尝试在我的主文件中访问它时,它给了我以下错误:build command-line-arguments: cannot find module for path path/to/parser我的主文件是这样的:

package main

import (
    "fmt"

    "./parser"
    "github.com/antlr/antlr4/runtime/Go/antlr"
)

func main() {
    fmt.Println("Hello, World!")
    is := antlr.NewInputStream("1 + 2 * 3")

    // Create the Lexer
    lexer := parser.NewHelloWorldLexer(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())
    }
}

标签: goantlr

解决方案


这是使用 go 模块的解决方案

你的项目结构应该是这样的

.
├── go.mod
├── main.go
└── parser
    └── parser.go

去.mod

module example.com/projectname

go 1.15
...

main.go

package main

import (
    "fmt"

    "example.com/projectname/parser"

    "github.com/antlr/antlr4/runtime/Go/antlr"
)
...

解析器

package parser

import (
    "github.com/antlr/antlr4/runtime/Go/antlr"
)
...

推荐阅读