1 #!/usr/local/bin/python 2 # yply.py 3 # 4 # Author: David Beazley (dave (at] dabeaz.com) 5 # Date : October 2, 2006 6 # 7 # Converts a UNIX-yacc specification file into a PLY-compatible 8 # specification. To use, simply do this: 9 # 10 # % python yply.py [-nocode] inputfile.y >myparser.py 11 # 12 # The output of this program is Python code. In the output, 13 # any C code in the original file is included, but is commented. 14 # If you use the -nocode option, then all of the C code in the 15 # original file is discarded. 16 # 17 # Disclaimer: This just an example I threw together in an afternoon. 18 # It might have some bugs. However, it worked when I tried it on 19 # a yacc-specified C++ parser containing 442 rules and 855 parsing 20 # states. 21 # 22 23 import sys 24 sys.path.insert(0, "../..") 25 26 import ylex 27 import yparse 28 29 from ply import * 30 31 if len(sys.argv) == 1: 32 print("usage : yply.py [-nocode] inputfile") 33 raise SystemExit 34 35 if len(sys.argv) == 3: 36 if sys.argv[1] == '-nocode': 37 yparse.emit_code = 0 38 else: 39 print("Unknown option '%s'" % sys.argv[1]) 40 raise SystemExit 41 filename = sys.argv[2] 42 else: 43 filename = sys.argv[1] 44 45 yacc.parse(open(filename).read()) 46 47 print(""" 48 if __name__ == '__main__': 49 from ply import * 50 yacc.yacc() 51 """) 52