Path: blob/main/third_party/ply/example/yply/yply.py
7087 views
#!/usr/local/bin/python1# yply.py2#3# Author: David Beazley ([email protected])4# Date : October 2, 20065#6# Converts a UNIX-yacc specification file into a PLY-compatible7# specification. To use, simply do this:8#9# % python yply.py [-nocode] inputfile.y >myparser.py10#11# The output of this program is Python code. In the output,12# any C code in the original file is included, but is commented.13# If you use the -nocode option, then all of the C code in the14# original file is discarded.15#16# Disclaimer: This just an example I threw together in an afternoon.17# It might have some bugs. However, it worked when I tried it on18# a yacc-specified C++ parser containing 442 rules and 855 parsing19# states.20#2122import sys23sys.path.insert(0,"../..")2425import ylex26import yparse2728from ply import *2930if len(sys.argv) == 1:31print "usage : yply.py [-nocode] inputfile"32raise SystemExit3334if len(sys.argv) == 3:35if sys.argv[1] == '-nocode':36yparse.emit_code = 037else:38print "Unknown option '%s'" % sys.argv[1]39raise SystemExit40filename = sys.argv[2]41else:42filename = sys.argv[1]4344yacc.parse(open(filename).read())4546print """47if __name__ == '__main__':48from ply import *49yacc.yacc()50"""5152535455