0

Using these steps I'm trying to generate the parse tree for Antlr4 Python3.g4 grammar file, to parse python3 code, I've generated my python parser using ANTLR. But I'm unsure how to pass in a python file as the InputStream doesn't accept this.

I've currently managed to pass it in as a text file:

def main():
    with open('testingText.txt') as file:
        data = file.read()


    input_stream = InputStream(data)
    lexer = Python3Lexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = Python3Parser(stream)
    tree = parser.single_input()
    print(tree.toStringTree(recog=parser))

But I get errors to do with 'mismatched input <EOF>' and sometimes 'no viable alternative as input <EOF>'

I would like to pass in a .py file, and I'm not sure what to do about the <EOF> issues

0

1 Answer 1

1

To be sure, I'd need to know what testingText.txt contains, but I'm pretty sure that the parser expects a line break at the end of the file and testingText.txt does not contains a trailing line break.

You could try this:

with open('testingText.txt') as file:
    data = f'{file.read()}\n'

EDIT

And if testingText.txt contains:

class myClass:
    x=5
    print("hello world")

parse it like this:

from antlr4 import *
from Python3Lexer import Python3Lexer
from Python3Parser import Python3Parser


def main():
    with open('testingText.txt') as file:
        data = f'{file.read()}\n'

    input_stream = InputStream(data)
    lexer = Python3Lexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = Python3Parser(stream)
    tree = parser.file_input()
    print(tree.toStringTree(recog=parser))


if __name__ == '__main__':
    main()

E.g. use tree = parser.file_input() and not tree = parser.single_input().

5
  • hi when I do this, it now says 'missing NEWLINE at '<EOF>'
    – Fari
    Commented Feb 4, 2023 at 13:59
  • What does testingText.txt contain?
    – Bart Kiers
    Commented Feb 4, 2023 at 16:22
  • class myClass: x=5 print("hello world")
    – Fari
    Commented Feb 4, 2023 at 16:41
  • but they have spaces and are each on new lines
    – Fari
    Commented Feb 4, 2023 at 16:41
  • That input is no single_input but a file_input. So do tree = parser.file_input() instead.
    – Bart Kiers
    Commented Feb 4, 2023 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.