Changeset 2416


Ignore:
Timestamp:
Mar 9, 2010 6:45:22 PM (9 years ago)
Author:
jwatson
Message:

Modified the AMPL dat file parser to instantiate the lexer and yaccer objects only once, for the lifetime of the module.

They were being inadvertently created at each invocation, which pyomo users wouldn't notice. But PySP users, who can be creating 1K or more instances, do!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/data/parse_datacmds.py

    r2330 r2416  
    1919from pyutilib.misc import flatten
    2020from pyutilib.ply import t_newline, t_ignore, _find_column, p_error, ply_init
    21 
    22 tabmodule = 'parse_table_datacmds'
    23 outputdir = os.path.dirname(os.path.abspath(__file__))+os.sep
    2421
    2522## -----------------------------------------------------------
     
    312309
    313310#
     311# the ampl dat file lexer and yaccer only need to be
     312# created once, so have the corresponding objects
     313# accessible at module scope.
     314#
     315
     316tabmodule = 'parse_table_datacmds'
     317outputdir = os.path.dirname(os.path.abspath(__file__))+os.sep
     318
     319ampl_dat_lexer = None
     320ampl_dat_yaccer = None
     321
     322#
    314323# The function that performs the parsing
    315324#
    316325def parse_data_commands(data=None, filename=None, debug=0):
     326
    317327    global debugging
    318     #
    319     # Always remove the parser.out file, which is generated to create debugging
    320     #
    321     if os.path.exists("parser.out"):        #pragma:nocover
    322        os.remove("parser.out")
    323     if debug > 0:                           #pragma:nocover
    324         #
    325         # Remove the parsetab.py* files.  These apparently need to be removed
    326         # to ensure the creation of a parser.out file.
    327         #
    328         if os.path.exists("parsetab.py"):
    329            os.remove("parsetab.py")
    330         if os.path.exists("parsetab.pyc"):
    331            os.remove("parsetab.pyc")
    332         debugging=True
    333     #
    334     # Build lexer
    335     #
    336     lex.lex()
     328    global ampl_dat_lexer
     329    global ampl_dat_yaccer
     330
     331    # if the lexer/yaccer haven't been initialized, do so.
     332    if ampl_dat_lexer is None:
     333       #
     334       # Always remove the parser.out file, which is generated to create debugging
     335       #
     336       if os.path.exists("parser.out"):        #pragma:nocover
     337           os.remove("parser.out")
     338       if debug > 0:                           #pragma:nocover
     339           #
     340           # Remove the parsetab.py* files.  These apparently need to be removed
     341           # to ensure the creation of a parser.out file.
     342           #
     343           if os.path.exists("parsetab.py"):
     344              os.remove("parsetab.py")
     345           if os.path.exists("parsetab.pyc"):
     346              os.remove("parsetab.pyc")
     347           debugging=True
     348
     349       ampl_dat_lexer = lex.lex()
     350       ampl_dat_yaccer = yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir)
     351           
    337352    #
    338353    # Initialize parse object
     
    340355    global _parse_info
    341356    _parse_info = []
    342     #
    343     # Build yaccer
    344     #
    345     yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir)
     357
    346358    #
    347359    # Parse the file
     
    351363        _parsedata=data
    352364        ply_init(_parsedata)
    353         yacc.parse(data,debug=debug)
     365        ampl_dat_yaccer.parse(data, lexer=ampl_dat_lexer, debug=debug)
    354366    elif not filename is None:
    355367        f = open(filename)
     
    358370        _parsedata=data
    359371        ply_init(_parsedata)
    360         yacc.parse(data, debug=debug)
     372        ampl_dat_yaccer.parse(data, lexer=ampl_dat_lexer, debug=debug)
    361373    else:
    362374        _parse_info = None
Note: See TracChangeset for help on using the changeset viewer.