more Py3 fixes
This commit is contained in:
parent
96490105f2
commit
b2fef34742
3 changed files with 12 additions and 5 deletions
|
|
@ -17,5 +17,5 @@ parser Calculator:
|
|||
)* {{ return v }}
|
||||
|
||||
# A term is either a number or an expression surrounded by parentheses
|
||||
rule term: NUM {{ return atoi(NUM) }}
|
||||
rule term: NUM {{ return int(NUM) }}
|
||||
| "\\(" expr "\\)" {{ return expr }}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@
|
|||
#
|
||||
|
||||
import os, sys, re, types
|
||||
#from six import string_types
|
||||
PY2 = sys.version_info[0] == 2
|
||||
if PY2:
|
||||
string_types = (basestring,)
|
||||
else:
|
||||
string_types = (str,)
|
||||
|
||||
try: from yapps import runtime, parsetree, grammar
|
||||
except ImportError:
|
||||
|
|
@ -27,7 +33,7 @@ def generate(inputfile, outputfile=None, dump=0, **flags):
|
|||
and an output filename (defaulting to X.py)."""
|
||||
|
||||
inputfilename = inputfile if isinstance(
|
||||
inputfile, types.StringTypes ) else inputfile.name
|
||||
inputfile, string_types ) else inputfile.name
|
||||
if not outputfile:
|
||||
if inputfilename.endswith('.g'):
|
||||
outputfile = inputfilename[:-2] + '.py'
|
||||
|
|
@ -38,7 +44,7 @@ def generate(inputfile, outputfile=None, dump=0, **flags):
|
|||
preparser, postparser = None, None # Code before and after the parser desc
|
||||
|
||||
# Read the entire file
|
||||
if isinstance(inputfile, types.StringTypes):
|
||||
if isinstance(inputfile, string_types):
|
||||
inputfile = open(inputfilename)
|
||||
s = inputfile.read()
|
||||
|
||||
|
|
@ -67,7 +73,7 @@ def generate(inputfile, outputfile=None, dump=0, **flags):
|
|||
if dump:
|
||||
t.dump_information()
|
||||
else:
|
||||
if isinstance(outputfile, types.StringTypes):
|
||||
if isinstance(outputfile, string_types):
|
||||
outputfile = open(outputfile, 'w')
|
||||
t.output = outputfile
|
||||
t.generate_output()
|
||||
|
|
|
|||
|
|
@ -272,6 +272,7 @@ class Generator:
|
|||
self.calculate()
|
||||
self.write(self.preparser)
|
||||
self.write("# Begin -- grammar generated by Yapps\n")
|
||||
self.write("from __future__ import print_function\n")
|
||||
self.write("import sys, re\n")
|
||||
self.write("from yapps import runtime\n")
|
||||
self.write("\n")
|
||||
|
|
@ -313,7 +314,7 @@ class Generator:
|
|||
self.write(INDENT*3, "f = open(argv[2],'r')\n")
|
||||
self.write(INDENT*2, "else:\n")
|
||||
self.write(INDENT*3, "f = stdin\n")
|
||||
self.write(INDENT*2, "print parse(argv[1], f.read())\n")
|
||||
self.write(INDENT*2, "print(parse(argv[1], f.read()))\n")
|
||||
self.write(INDENT, "else: print ('Args: <rule> [<filename>]', file=sys.stderr)\n")
|
||||
self.write("# End -- grammar generated by Yapps\n")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue