ANTLR4 + Python从字符串而不是路径解析

ec-*_*c-m 5 python parsing antlr antlr4

我正在使用ANTLR4和Python,我目前正在使用以下代码进行解析:

lexer = MyGrammarLexer(FileStream(path))
stream = CommonTokenStream(lexer)
parser = MyGrammarParser(stream)
return parser.start().object
Run Code Online (Sandbox Code Playgroud)

但是,我想将此代码更改为直接从给定字符串而不是给定路径进行解析.从而将第一行改为类似的东西

lexer = MyGrammarLexer(a_given_string)
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

Bar*_*ers 7

看看Pyhton2Python3运行时的来源,我会说使用一个InputStream代替:

lexer = MyGrammarLexer(InputStream(a_given_string))
Run Code Online (Sandbox Code Playgroud)