Home > other >  How can I add library import to antlr4 in go?
How can I add library import to antlr4 in go?

Time:12-15

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.

CodePudding user response:

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
}

CodePudding user response:

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

  • Related