Search code examples
pythonparsingyacclexply

How to run a parser from a separate file?


I'm trying to keep my parser in it's own file and call that parser on some text in the main file/program.

My lexer is in it's own file: MDLexer.py I've put the parser in it's own file: MDParser.py

MDParser imports MDLexer like this: from MDLexer import tokens

Is there a similar import statement to import my Parser into a third file?

Any help appreciated.

Clarification: I've tried the following:

import MDParser as p
...
parser = p.yacc.yacc()

However this gives me the following error:

Traceback (most recent call last):
  File "MDtoAST.py", line 35, in <module>
    parser = p.yacc.yacc()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/ply/yacc.py", line 3276, in yacc
    raise YaccError('Unable to build parser')
ply.yacc.YaccError: Unable to build parser

Solution

  • If it were me, I'd put this line in MDParser.py

    parser = yacc.yacc()
    

    and make my main program look like this:

    import MDParser as p
    p.parser.parse('some string')