summaryrefslogtreecommitdiff
path: root/ext/ply/example/newclasscalc/calc.py
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ply/example/newclasscalc/calc.py')
-rwxr-xr-x[-rw-r--r--]ext/ply/example/newclasscalc/calc.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/ext/ply/example/newclasscalc/calc.py b/ext/ply/example/newclasscalc/calc.py
index 7f29bc821..a12e498b2 100644..100755
--- a/ext/ply/example/newclasscalc/calc.py
+++ b/ext/ply/example/newclasscalc/calc.py
@@ -14,7 +14,9 @@
import sys
sys.path.insert(0,"../..")
-import readline
+if sys.version_info[0] >= 3:
+ raw_input = input
+
import ply.lex as lex
import ply.yacc as yacc
import os
@@ -51,10 +53,10 @@ class Parser(object):
s = raw_input('calc > ')
except EOFError:
break
- if not s: continue
+ if not s: continue
yacc.parse(s)
-
+
class Calc(Parser):
tokens = (
@@ -80,7 +82,7 @@ class Calc(Parser):
try:
t.value = int(t.value)
except ValueError:
- print "Integer value too large", t.value
+ print("Integer value too large %s" % t.value)
t.value = 0
#print "parsed number %s" % repr(t.value)
return t
@@ -90,9 +92,9 @@ class Calc(Parser):
def t_newline(self, t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
-
+
def t_error(self, t):
- print "Illegal character '%s'" % t.value[0]
+ print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Parsing rules
@@ -110,7 +112,7 @@ class Calc(Parser):
def p_statement_expr(self, p):
'statement : expression'
- print p[1]
+ print(p[1])
def p_expression_binop(self, p):
"""
@@ -144,11 +146,14 @@ class Calc(Parser):
try:
p[0] = self.names[p[1]]
except LookupError:
- print "Undefined name '%s'" % p[1]
+ print("Undefined name '%s'" % p[1])
p[0] = 0
def p_error(self, p):
- print "Syntax error at '%s'" % p.value
+ if p:
+ print("Syntax error at '%s'" % p.value)
+ else:
+ print("Syntax error at EOF")
if __name__ == '__main__':
calc = Calc()