forked from seehuhn/wisent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwisent.py
executable file
·142 lines (122 loc) · 4.55 KB
/
wisent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/env python
# wisent - a Python parser generator
#
# Copyright (C) 2008, 2009 Jochen Voss <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import sys
# FIX PATH
from os.path import basename
from optparse import OptionParser
from grammar import read_grammar
from automaton import Automaton
from helpers import open_executable
from version import VERSION
######################################################################
# command line options
getopt = OptionParser("usage: %prog [options] grammar")
getopt.remove_option("-h")
getopt.add_option("-d", "--debug", action="store", type="string",
dest="debug", default="",
help="enable debugging (p=parser)",
metavar="CHARS")
getopt.add_option("-e", "--example", action="store", dest="example_fname",
help="store example source code into NAME",
metavar="NAME")
getopt.add_option("-h", "--help", action="store_true", dest="help_flag",
help="show this message")
getopt.add_option("-o", "--output", action="store", dest="output_fname",
help="set the output file name (default is stdout)",
metavar="NAME")
getopt.add_option("-r", "--replace", action="store_true", dest="replace_flag",
help="replace nonterminals by numbers")
getopt.add_option("-V","--version",action="store_true",dest="version_flag",
help="show version information")
(options,args)=getopt.parse_args()
if options.help_flag:
getopt.print_help()
print ""
print "Please report bugs to <[email protected]>."
raise SystemExit(0)
if options.version_flag:
print """wisent %s
Copyright (C) 2008, 2009 Jochen Voss <[email protected]>
Wisent comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by law. You may redistribute copies of Wisent under
the terms of the GNU General Public License. For more
information about these matters, see the file named COPYING."""%VERSION
raise SystemExit(0)
######################################################################
# collect file names and other info
params = {}
progname = basename(sys.argv[0])
if len(args) < 1:
f_in = None
elif len(args) > 1:
getopt.error("too many command line arguments")
else:
f_in = args[0]
f_out = options.output_fname
f_ex = options.example_fname
if "p" in options.debug:
params["parser_comment"] = True
params["parser_debugprint"] = True
params["replace_nonterminals"] = options.replace_flag
######################################################################
# read the grammar
if f_in is None:
text = sys.stdin.read()
else:
params.setdefault("fname", f_in)
try:
fd = open(f_in, "rb")
text = fd.read()
fd.close()
except IOError, e:
msg = '%s: error while reading "%s": %s'%(f_in, f_in, e.strerror)
print >>sys.stderr, msg
raise SystemExit(1)
def check(g, params):
a = Automaton(g, params)
a.check()
return a
a = read_grammar(unicode(text, "utf-8").splitlines(), params, check)
del text
######################################################################
# emit the parser
if f_out is None:
a.write_parser(sys.stdout, params)
else:
params.setdefault("parser_name", f_out)
try:
fd = open(f_out, "w")
a.write_parser(fd, params)
fd.close()
except IOError, e:
msg = '%s: error while writing "%s": %s'%(progname, f_out, e.strerror)
print >>sys.stderr, msg
raise SystemExit(1)
######################################################################
# emit the example source code
if f_ex is not None:
params.setdefault("example_name", f_ex)
try:
fd = open_executable(f_ex, "w")
a.g.write_example(fd, params)
fd.close()
except IOError, e:
msg = '%s: error while writing "%s": %s'%(progname, f_ex, e.strerror)
print >>sys.stderr, msg
raise SystemExit(1)