Search code examples
pythonparsingantlrantlr4

ANTLR not generating ParserBase


I am new to ANTLR and was looking into the CPP14 grammar here. I tried using

antlr4 -Dlanguage=Python3 CPP14Lexer.g4

antlr4 -Dlanguage=Python3 CPP14Parser.g4

Then use the following driver code:

import sys
from antlr4 import *
from CPP14Lexer import CPP14Lexer
from CPP14Parser import CPP14Parser

def main(argv):
    input_stream = FileStream(argv[1])
    lexer = CPP14Lexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = CPP14Parser(stream)
    tree = parser.start_()

if __name__ == '__main__':
    main(sys.argv)

And, try running it to parse a Hello World code in cpp, but I get the following error:

python Driver.py example.cc                                                                                   
Traceback (most recent call last):
  File "/Users/vedantamohapatra/antlr/Driver.py", line 4, in <module>
    from CPP14Parser import CPP14Parser
  File "/Users/vedantamohapatra/antlr/CPP14Parser.py", line 14, in <module>
    from CPP14ParserBase import CPP14ParserBase
ModuleNotFoundError: No module named 'CPP14ParserBase'

How should I go about correcting this?


Solution

  • I think you should first run transformGrammar.py, and then also CPP14ParserBase.py before running your driver file.