ANTLR4 + Python parsing from string instead of path
Asked Answered
U

1

7

I am using ANTLR4 with Python and I am currently using the following code for parsing:

lexer = MyGrammarLexer(FileStream(path))
stream = CommonTokenStream(lexer)
parser = MyGrammarParser(stream)
return parser.start().object

However, I would like to change this code to parse directly from a given string instead of a given path. Thus changing the first line to something similar to

lexer = MyGrammarLexer(a_given_string)

How do I do this?

Ulda answered 17/1, 2017 at 9:49 Comment(0)
B
10

Looking at the sources of the Python2 and Python3 runtimes, I'd say use an InputStream instead:

lexer = MyGrammarLexer(InputStream(a_given_string))
Beautify answered 17/1, 2017 at 10:12 Comment(1)
Awesome! Thank you.Linguistician

© 2022 - 2024 — McMap. All rights reserved.