• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/local/bin/python
2# yply.py
3#
4# Author: David Beazley (dave@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
23import sys
24sys.path.insert(0, "../..")
25
26import ylex
27import yparse
28
29from ply import *
30
31if len(sys.argv) == 1:
32    print("usage : yply.py [-nocode] inputfile")
33    raise SystemExit
34
35if 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]
42else:
43    filename = sys.argv[1]
44
45yacc.parse(open(filename).read())
46
47print("""
48if __name__ == '__main__':
49    from ply import *
50    yacc.yacc()
51""")
52