Search code examples
pythonparsingply

How to prevent table regeneration in PLY


I am using PLY in a command line application that I package as a Python egg to be installed via pip. Everytime I run my script from the command line, I see the following message:

"Generating LALR tables"

Additionally, parser.out and parsetab.py files are written to the directory from which the script is invoked. Is there some way to ship these files with the application so that it does not regenerate the tables each and every time?


Solution

  • What I ultimately wound up doing was turning off optimization. I was going through the PLY 3.4 source and I found this little nugget in the lexer code:

    # If in optimize mode, we write the lextab
    if lextab and optimize:
        lexobj.writetab(lextab,outputdir)
    
    return lexobj
    

    By changing the code that builds the lexer and parser to:

    self.lexer = lex.lex(module=self, optimize=False, debug=False, **kwargs)

    and

    self.lexer = lex.lex(module=self, optimize=False, debug=False, **kwargs)

    I avoided all file write-outs. The debugger writes .out files into the directory and the Python files are the result of the optimize flag.

    While this works for the time being, I cannot say I am entirely happy with this approach. Presumably, having some way to keep optimization on and, at the same time, keep the working directory clean would be a superior solution would result in better performance. If someone else has a better methodology, I am more than open to it.