diff options
Diffstat (limited to 'ext/ply/example/calc/calc.py')
-rw-r--r-- | ext/ply/example/calc/calc.py | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/ext/ply/example/calc/calc.py b/ext/ply/example/calc/calc.py index 987ce8019..b92378043 100644 --- a/ext/ply/example/calc/calc.py +++ b/ext/ply/example/calc/calc.py @@ -8,6 +8,9 @@ import sys sys.path.insert(0,"../..") +if sys.version_info[0] >= 3: + raw_input = input + tokens = ( 'NAME','NUMBER', ) @@ -20,11 +23,7 @@ t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' def t_NUMBER(t): r'\d+' - try: - t.value = int(t.value) - except ValueError: - print "Integer value too large", t.value - t.value = 0 + t.value = int(t.value) return t t_ignore = " \t" @@ -32,11 +31,11 @@ t_ignore = " \t" def t_newline(t): r'\n+' t.lexer.lineno += t.value.count("\n") - + def t_error(t): - print "Illegal character '%s'" % t.value[0] + print("Illegal character '%s'" % t.value[0]) t.lexer.skip(1) - + # Build the lexer import ply.lex as lex lex.lex() @@ -58,7 +57,7 @@ def p_statement_assign(p): def p_statement_expr(p): 'statement : expression' - print p[1] + print(p[1]) def p_expression_binop(p): '''expression : expression '+' expression @@ -87,11 +86,14 @@ def p_expression_name(p): try: p[0] = names[p[1]] except LookupError: - print "Undefined name '%s'" % p[1] + print("Undefined name '%s'" % p[1]) p[0] = 0 def p_error(p): - print "Syntax error at '%s'" % p.value + if p: + print("Syntax error at '%s'" % p.value) + else: + print("Syntax error at EOF") import ply.yacc as yacc yacc.yacc() |