def save_grammar(self, *args):
filename = asksaveasfilename(filetypes=self.GRAMMAR_FILE_TYPES,
defaultextension='.cfg')
if not filename: return
try:
if filename.endswith('.pickle'):
pickle.dump((self._chart, self._tokens), open(filename, 'w'))
else:
file = open(filename, 'w')
prods = self._grammar.productions()
start = [p for p in prods if p.lhs() == self._grammar.start()]
rest = [p for p in prods if p.lhs() != self._grammar.start()]
for prod in start: file.write('%s\n' % prod)
for prod in rest: file.write('%s\n' % prod)
file.close()
except Exception, e:
tkMessageBox.showerror('Error Saving Grammar',
'Unable to open file: %r' % filename)
评论列表
文章目录