summaryrefslogtreecommitdiff
path: root/ext/ply/example
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ply/example')
-rw-r--r--ext/ply/example/BASIC/basic.py9
-rw-r--r--ext/ply/example/BASIC/basiclex.py16
-rw-r--r--ext/ply/example/BASIC/basiclog.py79
-rw-r--r--ext/ply/example/BASIC/basinterp.py125
-rw-r--r--ext/ply/example/BASIC/basparse.py26
-rw-r--r--ext/ply/example/GardenSnake/GardenSnake.py28
-rw-r--r--ext/ply/example/ansic/clex.py20
-rw-r--r--ext/ply/example/ansic/cparse.py10
-rw-r--r--ext/ply/example/calc/calc.py24
-rw-r--r--ext/ply/example/calcdebug/calc.py113
-rwxr-xr-x[-rw-r--r--]ext/ply/example/classcalc/calc.py21
-rwxr-xr-x[-rw-r--r--]ext/ply/example/cleanup.sh0
-rw-r--r--ext/ply/example/closurecalc/calc.py130
-rw-r--r--ext/ply/example/hedit/hedit.py12
-rwxr-xr-x[-rw-r--r--]ext/ply/example/newclasscalc/calc.py23
-rw-r--r--ext/ply/example/optcalc/README2
-rw-r--r--ext/ply/example/optcalc/calc.py20
-rw-r--r--ext/ply/example/unicalc/calc.py9
-rw-r--r--ext/ply/example/yply/ylex.py12
-rw-r--r--ext/ply/example/yply/yparse.py14
-rwxr-xr-x[-rw-r--r--]ext/ply/example/yply/yply.py0
21 files changed, 520 insertions, 173 deletions
diff --git a/ext/ply/example/BASIC/basic.py b/ext/ply/example/BASIC/basic.py
index 3a07acdbf..b14483d2d 100644
--- a/ext/ply/example/BASIC/basic.py
+++ b/ext/ply/example/BASIC/basic.py
@@ -4,6 +4,9 @@
import sys
sys.path.insert(0,"../..")
+if sys.version_info[0] >= 3:
+ raw_input = input
+
import basiclex
import basparse
import basinterp
@@ -41,7 +44,7 @@ while 1:
prog = basparse.parse(line)
if not prog: continue
- keys = prog.keys()
+ keys = list(prog)
if keys[0] > 0:
b.add_statements(prog)
else:
@@ -58,8 +61,8 @@ while 1:
elif stat[0] == 'NEW':
b.new()
-
-
+
+
diff --git a/ext/ply/example/BASIC/basiclex.py b/ext/ply/example/BASIC/basiclex.py
index 727383f2b..3d27cdeeb 100644
--- a/ext/ply/example/BASIC/basiclex.py
+++ b/ext/ply/example/BASIC/basiclex.py
@@ -25,7 +25,7 @@ def t_ID(t):
if t.value in keywords:
t.type = t.value
return t
-
+
t_EQUALS = r'='
t_PLUS = r'\+'
t_MINUS = r'-'
@@ -41,7 +41,7 @@ t_GE = r'>='
t_NE = r'<>'
t_COMMA = r'\,'
t_SEMI = r';'
-t_INTEGER = r'\d+'
+t_INTEGER = r'\d+'
t_FLOAT = r'((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+))'
t_STRING = r'\".*?\"'
@@ -51,14 +51,10 @@ def t_NEWLINE(t):
return t
def t_error(t):
- print "Illegal character", t.value[0]
+ print("Illegal character %s" % t.value[0])
t.lexer.skip(1)
-lex.lex()
-
-
-
-
+lex.lex(debug=0)
@@ -66,6 +62,10 @@ lex.lex()
+
+
+
+
diff --git a/ext/ply/example/BASIC/basiclog.py b/ext/ply/example/BASIC/basiclog.py
new file mode 100644
index 000000000..ccfd7b967
--- /dev/null
+++ b/ext/ply/example/BASIC/basiclog.py
@@ -0,0 +1,79 @@
+# An implementation of Dartmouth BASIC (1964)
+#
+
+import sys
+sys.path.insert(0,"../..")
+
+if sys.version_info[0] >= 3:
+ raw_input = input
+
+import logging
+logging.basicConfig(
+ level = logging.INFO,
+ filename = "parselog.txt",
+ filemode = "w"
+)
+log = logging.getLogger()
+
+import basiclex
+import basparse
+import basinterp
+
+# If a filename has been specified, we try to run it.
+# If a runtime error occurs, we bail out and enter
+# interactive mode below
+if len(sys.argv) == 2:
+ data = open(sys.argv[1]).read()
+ prog = basparse.parse(data,debug=log)
+ if not prog: raise SystemExit
+ b = basinterp.BasicInterpreter(prog)
+ try:
+ b.run()
+ raise SystemExit
+ except RuntimeError:
+ pass
+
+else:
+ b = basinterp.BasicInterpreter({})
+
+# Interactive mode. This incrementally adds/deletes statements
+# from the program stored in the BasicInterpreter object. In
+# addition, special commands 'NEW','LIST',and 'RUN' are added.
+# Specifying a line number with no code deletes that line from
+# the program.
+
+while 1:
+ try:
+ line = raw_input("[BASIC] ")
+ except EOFError:
+ raise SystemExit
+ if not line: continue
+ line += "\n"
+ prog = basparse.parse(line,debug=log)
+ if not prog: continue
+
+ keys = list(prog)
+ if keys[0] > 0:
+ b.add_statements(prog)
+ else:
+ stat = prog[keys[0]]
+ if stat[0] == 'RUN':
+ try:
+ b.run()
+ except RuntimeError:
+ pass
+ elif stat[0] == 'LIST':
+ b.list()
+ elif stat[0] == 'BLANK':
+ b.del_line(stat[1])
+ elif stat[0] == 'NEW':
+ b.new()
+
+
+
+
+
+
+
+
+
diff --git a/ext/ply/example/BASIC/basinterp.py b/ext/ply/example/BASIC/basinterp.py
index 5850457cb..3e8a7774a 100644
--- a/ext/ply/example/BASIC/basinterp.py
+++ b/ext/ply/example/BASIC/basinterp.py
@@ -40,10 +40,11 @@ class BasicInterpreter:
if self.prog[lineno][0] == 'END' and not has_end:
has_end = lineno
if not has_end:
- print "NO END INSTRUCTION"
+ print("NO END INSTRUCTION")
self.error = 1
+ return
if has_end != lineno:
- print "END IS NOT LAST"
+ print("END IS NOT LAST")
self.error = 1
# Check loops
@@ -60,9 +61,9 @@ class BasicInterpreter:
self.loopend[pc] = i
break
else:
- print "FOR WITHOUT NEXT AT LINE" % self.stat[pc]
+ print("FOR WITHOUT NEXT AT LINE %s" % self.stat[pc])
self.error = 1
-
+
# Evaluate an expression
def eval(self,expr):
etype = expr[0]
@@ -79,33 +80,33 @@ class BasicInterpreter:
elif etype == 'VAR':
var,dim1,dim2 = expr[1]
if not dim1 and not dim2:
- if self.vars.has_key(var):
+ if var in self.vars:
return self.vars[var]
else:
- print "UNDEFINED VARIABLE", var, "AT LINE", self.stat[self.pc]
+ print("UNDEFINED VARIABLE %s AT LINE %s" % (var, self.stat[self.pc]))
raise RuntimeError
# May be a list lookup or a function evaluation
if dim1 and not dim2:
- if self.functions.has_key(var):
+ if var in self.functions:
# A function
return self.functions[var](dim1)
else:
# A list evaluation
- if self.lists.has_key(var):
+ if var in self.lists:
dim1val = self.eval(dim1)
if dim1val < 1 or dim1val > len(self.lists[var]):
- print "LIST INDEX OUT OF BOUNDS AT LINE", self.stat[self.pc]
+ print("LIST INDEX OUT OF BOUNDS AT LINE %s" % self.stat[self.pc])
raise RuntimeError
return self.lists[var][dim1val-1]
if dim1 and dim2:
- if self.tables.has_key(var):
+ if var in self.tables:
dim1val = self.eval(dim1)
dim2val = self.eval(dim2)
if dim1val < 1 or dim1val > len(self.tables[var]) or dim2val < 1 or dim2val > len(self.tables[var][0]):
- print "TABLE INDEX OUT OUT BOUNDS AT LINE", self.stat[self.pc]
+ print("TABLE INDEX OUT OUT BOUNDS AT LINE %s" % self.stat[self.pc])
raise RuntimeError
return self.tables[var][dim1val-1][dim2val-1]
- print "UNDEFINED VARIABLE", var, "AT LINE", self.stat[self.pc]
+ print("UNDEFINED VARIABLE %s AT LINE %s" % (var, self.stat[self.pc]))
raise RuntimeError
# Evaluate a relational expression
@@ -145,31 +146,31 @@ class BasicInterpreter:
elif dim1 and not dim2:
# List assignment
dim1val = self.eval(dim1)
- if not self.lists.has_key(var):
+ if not var in self.lists:
self.lists[var] = [0]*10
if dim1val > len(self.lists[var]):
- print "DIMENSION TOO LARGE AT LINE", self.stat[self.pc]
+ print ("DIMENSION TOO LARGE AT LINE %s" % self.stat[self.pc])
raise RuntimeError
self.lists[var][dim1val-1] = self.eval(value)
elif dim1 and dim2:
dim1val = self.eval(dim1)
dim2val = self.eval(dim2)
- if not self.tables.has_key(var):
+ if not var in self.tables:
temp = [0]*10
v = []
for i in range(10): v.append(temp[:])
self.tables[var] = v
# Variable already exists
if dim1val > len(self.tables[var]) or dim2val > len(self.tables[var][0]):
- print "DIMENSION TOO LARGE AT LINE", self.stat[self.pc]
+ print("DIMENSION TOO LARGE AT LINE %s" % self.stat[self.pc])
raise RuntimeError
self.tables[var][dim1val-1][dim2val-1] = self.eval(value)
# Change the current line number
def goto(self,linenum):
- if not self.prog.has_key(linenum):
- print "UNDEFINED LINE NUMBER %d AT LINE %d" % (linenum, self.stat[self.pc])
+ if not linenum in self.prog:
+ print("UNDEFINED LINE NUMBER %d AT LINE %d" % (linenum, self.stat[self.pc]))
raise RuntimeError
self.pc = self.stat.index(linenum)
@@ -183,7 +184,7 @@ class BasicInterpreter:
self.gosub = None # Gosub return point (if any)
self.error = 0 # Indicates program error
- self.stat = self.prog.keys() # Ordered list of all line numbers
+ self.stat = list(self.prog) # Ordered list of all line numbers
self.stat.sort()
self.pc = 0 # Current program counter
@@ -198,7 +199,7 @@ class BasicInterpreter:
while 1:
line = self.stat[self.pc]
instr = self.prog[line]
-
+
op = instr[0]
# END and STOP statements
@@ -225,11 +226,11 @@ class BasicInterpreter:
out += str(eval)
sys.stdout.write(out)
end = instr[2]
- if not (end == ',' or end == ';'):
+ if not (end == ',' or end == ';'):
sys.stdout.write("\n")
if end == ',': sys.stdout.write(" "*(15-(len(out) % 15)))
if end == ';': sys.stdout.write(" "*(3-(len(out) % 3)))
-
+
# LET statement
elif op == 'LET':
target = instr[1]
@@ -258,7 +259,7 @@ class BasicInterpreter:
initval = instr[2]
finval = instr[3]
stepval = instr[4]
-
+
# Check to see if this is a new loop
if not self.loops or self.loops[-1][0] != self.pc:
# Looks like a new loop. Make the initial assignment
@@ -284,21 +285,21 @@ class BasicInterpreter:
elif op == 'NEXT':
if not self.loops:
- print "NEXT WITHOUT FOR AT LINE",line
+ print("NEXT WITHOUT FOR AT LINE %s" % line)
return
-
+
nextvar = instr[1]
self.pc = self.loops[-1][0]
loopinst = self.prog[self.stat[self.pc]]
forvar = loopinst[1]
if nextvar != forvar:
- print "NEXT DOESN'T MATCH FOR AT LINE", line
+ print("NEXT DOESN'T MATCH FOR AT LINE %s" % line)
return
continue
elif op == 'GOSUB':
newline = instr[1]
if self.gosub:
- print "ALREADY IN A SUBROUTINE AT LINE", line
+ print("ALREADY IN A SUBROUTINE AT LINE %s" % line)
return
self.gosub = self.stat[self.pc]
self.goto(newline)
@@ -306,7 +307,7 @@ class BasicInterpreter:
elif op == 'RETURN':
if not self.gosub:
- print "RETURN WITHOUT A GOSUB AT LINE",line
+ print("RETURN WITHOUT A GOSUB AT LINE %s" % line)
return
self.goto(self.gosub)
self.gosub = None
@@ -333,7 +334,7 @@ class BasicInterpreter:
v.append(temp[:])
self.tables[vname] = v
- self.pc += 1
+ self.pc += 1
# Utility functions for program listing
def expr_str(self,expr):
@@ -358,74 +359,74 @@ class BasicInterpreter:
# Create a program listing
def list(self):
- stat = self.prog.keys() # Ordered list of all line numbers
+ stat = list(self.prog) # Ordered list of all line numbers
stat.sort()
for line in stat:
instr = self.prog[line]
op = instr[0]
if op in ['END','STOP','RETURN']:
- print line, op
+ print("%s %s" % (line, op))
continue
elif op == 'REM':
- print line, instr[1]
+ print("%s %s" % (line, instr[1]))
elif op == 'PRINT':
- print line, op,
+ _out = "%s %s " % (line, op)
first = 1
for p in instr[1]:
- if not first: print ",",
- if p[0] and p[1]: print '"%s"%s' % (p[0],self.expr_str(p[1])),
- elif p[1]: print self.expr_str(p[1]),
- else: print '"%s"' % (p[0],),
+ if not first: _out += ", "
+ if p[0] and p[1]: _out += '"%s"%s' % (p[0],self.expr_str(p[1]))
+ elif p[1]: _out += self.expr_str(p[1])
+ else: _out += '"%s"' % (p[0],)
first = 0
- if instr[2]: print instr[2]
- else: print
+ if instr[2]: _out += instr[2]
+ print(_out)
elif op == 'LET':
- print line,"LET",self.var_str(instr[1]),"=",self.expr_str(instr[2])
+ print("%s LET %s = %s" % (line,self.var_str(instr[1]),self.expr_str(instr[2])))
elif op == 'READ':
- print line,"READ",
+ _out = "%s READ " % line
first = 1
for r in instr[1]:
- if not first: print ",",
- print self.var_str(r),
+ if not first: _out += ","
+ _out += self.var_str(r)
first = 0
- print ""
+ print(_out)
elif op == 'IF':
- print line,"IF %s THEN %d" % (self.relexpr_str(instr[1]),instr[2])
+ print("%s IF %s THEN %d" % (line,self.relexpr_str(instr[1]),instr[2]))
elif op == 'GOTO' or op == 'GOSUB':
- print line, op, instr[1]
+ print("%s %s %s" % (line, op, instr[1]))
elif op == 'FOR':
- print line,"FOR %s = %s TO %s" % (instr[1],self.expr_str(instr[2]),self.expr_str(instr[3])),
- if instr[4]: print "STEP %s" % (self.expr_str(instr[4])),
- print
+ _out = "%s FOR %s = %s TO %s" % (line,instr[1],self.expr_str(instr[2]),self.expr_str(instr[3]))
+ if instr[4]: _out += " STEP %s" % (self.expr_str(instr[4]))
+ print(_out)
elif op == 'NEXT':
- print line,"NEXT", instr[1]
+ print("%s NEXT %s" % (line, instr[1]))
elif op == 'FUNC':
- print line,"DEF %s(%s) = %s" % (instr[1],instr[2],self.expr_str(instr[3]))
+ print("%s DEF %s(%s) = %s" % (line,instr[1],instr[2],self.expr_str(instr[3])))
elif op == 'DIM':
- print line,"DIM",
+ _out = "%s DIM " % line
first = 1
for vname,x,y in instr[1]:
- if not first: print ",",
+ if not first: _out += ","
first = 0
if y == 0:
- print "%s(%d)" % (vname,x),
+ _out += "%s(%d)" % (vname,x)
else:
- print "%s(%d,%d)" % (vname,x,y),
-
- print
+ _out += "%s(%d,%d)" % (vname,x,y)
+
+ print(_out)
elif op == 'DATA':
- print line,"DATA",
+ _out = "%s DATA " % line
first = 1
for v in instr[1]:
- if not first: print ",",
+ if not first: _out += ","
first = 0
- print v,
- print
+ _out += v
+ print(_out)
# Erase the current program
def new(self):
self.prog = {}
-
+
# Insert statements
def add_statements(self,prog):
for line,stat in prog.items():
diff --git a/ext/ply/example/BASIC/basparse.py b/ext/ply/example/BASIC/basparse.py
index 930af9a22..ccdeb16b6 100644
--- a/ext/ply/example/BASIC/basparse.py
+++ b/ext/ply/example/BASIC/basparse.py
@@ -39,12 +39,12 @@ def p_program_error(p):
p[0] = None
p.parser.error = 1
-#### Format of all BASIC statements.
+#### Format of all BASIC statements.
def p_statement(p):
'''statement : INTEGER command NEWLINE'''
if isinstance(p[2],str):
- print p[2],"AT LINE", p[1]
+ print("%s %s %s" % (p[2],"AT LINE", p[1]))
p[0] = None
p.parser.error = 1
else:
@@ -68,7 +68,7 @@ def p_statement_blank(p):
def p_statement_bad(p):
'''statement : INTEGER error NEWLINE'''
- print "MALFORMED STATEMENT AT LINE", p[1]
+ print("MALFORMED STATEMENT AT LINE %s" % p[1])
p[0] = None
p.parser.error = 1
@@ -121,7 +121,7 @@ def p_command_print_bad(p):
#### Optional ending on PRINT. Either a comma (,) or semicolon (;)
def p_optend(p):
- '''optend : COMMA
+ '''optend : COMMA
| SEMI
|'''
if len(p) == 2:
@@ -188,7 +188,7 @@ def p_optstep(p):
p[0] = None
#### NEXT statement
-
+
def p_command_next(p):
'''command : NEXT ID'''
@@ -392,30 +392,30 @@ def p_item_expr(p):
p[0] = ("",p[1])
#### Empty
-
+
def p_empty(p):
'''empty : '''
#### Catastrophic error handler
def p_error(p):
if not p:
- print "SYNTAX ERROR AT EOF"
+ print("SYNTAX ERROR AT EOF")
bparser = yacc.yacc()
-def parse(data):
+def parse(data,debug=0):
bparser.error = 0
- p = bparser.parse(data)
+ p = bparser.parse(data,debug=debug)
if bparser.error: return None
return p
-
-
-
-
+
+
+
+
diff --git a/ext/ply/example/GardenSnake/GardenSnake.py b/ext/ply/example/GardenSnake/GardenSnake.py
index ffa550fc6..2a7f45eb1 100644
--- a/ext/ply/example/GardenSnake/GardenSnake.py
+++ b/ext/ply/example/GardenSnake/GardenSnake.py
@@ -180,7 +180,7 @@ def track_tokens_filter(lexer, tokens):
at_line_start = False
indent = MAY_INDENT
token.must_indent = False
-
+
elif token.type == "NEWLINE":
at_line_start = True
if indent == MAY_INDENT:
@@ -235,7 +235,7 @@ def indentation_filter(tokens):
## if token.must_indent:
## print "must_indent",
## print
-
+
# WS only occurs at the start of the line
# There may be WS followed by NEWLINE so
# only track the depth here. Don't indent/dedent
@@ -294,7 +294,7 @@ def indentation_filter(tokens):
assert token is not None
for _ in range(1, len(levels)):
yield DEDENT(token.lineno)
-
+
# The top-level filter adds an ENDMARKER, if requested.
# Python's grammar uses it.
@@ -376,14 +376,14 @@ def p_file_input(p):
p[0] = p[1] + p[2]
else:
p[0] = p[1]
-
+
# funcdef: [decorators] 'def' NAME parameters ':' suite
# ignoring decorators
def p_funcdef(p):
"funcdef : DEF NAME parameters COLON suite"
p[0] = ast.Function(None, p[2], tuple(p[3]), (), 0, None, p[5])
-
+
# parameters: '(' [varargslist] ')'
def p_parameters(p):
"""parameters : LPAR RPAR
@@ -392,9 +392,9 @@ def p_parameters(p):
p[0] = []
else:
p[0] = p[2]
+
-
-# varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
+# varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
# highly simplified
def p_varargslist(p):
"""varargslist : varargslist COMMA NAME
@@ -409,7 +409,7 @@ def p_stmt_simple(p):
"""stmt : simple_stmt"""
# simple_stmt is a list
p[0] = p[1]
-
+
def p_stmt_compound(p):
"""stmt : compound_stmt"""
p[0] = [p[1]]
@@ -474,7 +474,7 @@ def p_suite(p):
p[0] = ast.Stmt(p[1])
else:
p[0] = ast.Stmt(p[3])
-
+
def p_stmts(p):
"""stmts : stmts stmt
@@ -536,7 +536,7 @@ def p_comparison(p):
p[0] = unary_ops[p[1]](p[2])
else:
p[0] = p[1]
-
+
# power: atom trailer* ['**' factor]
# trailers enables function calls. I only allow one level of calls
# so this is 'trailer'
@@ -605,7 +605,7 @@ def p_testlist_multi(p):
def p_test(p):
"test : comparison"
p[0] = p[1]
-
+
# arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
@@ -642,7 +642,7 @@ class GardenSnakeParser(object):
###### Code generation ######
-
+
from compiler import misc, syntax, pycodegen
class GardenSnakeCompiler(object):
@@ -658,13 +658,13 @@ class GardenSnakeCompiler(object):
return code
####### Test code #######
-
+
compile = GardenSnakeCompiler().compile
code = r"""
print('LET\'S TRY THIS \\OUT')
-
+
#Comment here
def x(a):
print('called with',a)
diff --git a/ext/ply/example/ansic/clex.py b/ext/ply/example/ansic/clex.py
index 12441a60b..37fdd8e66 100644
--- a/ext/ply/example/ansic/clex.py
+++ b/ext/ply/example/ansic/clex.py
@@ -26,7 +26,7 @@ tokens = reserved + (
'OR', 'AND', 'NOT', 'XOR', 'LSHIFT', 'RSHIFT',
'LOR', 'LAND', 'LNOT',
'LT', 'LE', 'GT', 'GE', 'EQ', 'NE',
-
+
# Assignment (=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=)
'EQUALS', 'TIMESEQUAL', 'DIVEQUAL', 'MODEQUAL', 'PLUSEQUAL', 'MINUSEQUAL',
'LSHIFTEQUAL','RSHIFTEQUAL', 'ANDEQUAL', 'XOREQUAL', 'OREQUAL',
@@ -39,7 +39,7 @@ tokens = reserved + (
# Conditional operator (?)
'CONDOP',
-
+
# Delimeters ( ) [ ] { } , . ; :
'LPAREN', 'RPAREN',
'LBRACKET', 'RBRACKET',
@@ -57,7 +57,7 @@ t_ignore = ' \t\x0c'
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
-
+
# Operators
t_PLUS = r'\+'
t_MINUS = r'-'
@@ -142,23 +142,23 @@ t_CCONST = r'(L)?\'([^\\\n]|(\\.))*?\''
# Comments
def t_comment(t):
- r' /\*(.|\n)*?\*/'
- t.lineno += t.value.count('\n')
+ r'/\*(.|\n)*?\*/'
+ t.lexer.lineno += t.value.count('\n')
# Preprocessor directive (ignored)
def t_preprocessor(t):
r'\#(.)*?\n'
- t.lineno += 1
-
+ t.lexer.lineno += 1
+
def t_error(t):
- print "Illegal character %s" % repr(t.value[0])
+ print("Illegal character %s" % repr(t.value[0]))
t.lexer.skip(1)
-
+
lexer = lex.lex(optimize=1)
if __name__ == "__main__":
lex.runmain(lexer)
-
+
diff --git a/ext/ply/example/ansic/cparse.py b/ext/ply/example/ansic/cparse.py
index d474378c8..c9b916455 100644
--- a/ext/ply/example/ansic/cparse.py
+++ b/ext/ply/example/ansic/cparse.py
@@ -155,7 +155,7 @@ def p_struct_declaration_list_1(t):
pass
def p_struct_declaration_list_2(t):
- 'struct_declaration_list : struct_declarator_list struct_declaration'
+ 'struct_declaration_list : struct_declaration_list struct_declaration'
pass
# init-declarator-list:
@@ -778,12 +778,12 @@ def p_unary_expression_5(t):
def p_unary_expression_6(t):
'unary_expression : SIZEOF LPAREN type_name RPAREN'
pass
-
+
#unary-operator
def p_unary_operator(t):
'''unary_operator : AND
| TIMES
- | PLUS
+ | PLUS
| MINUS
| NOT
| LNOT '''
@@ -837,7 +837,7 @@ def p_argument_expression_list(t):
pass
# constant:
-def p_constant(t):
+def p_constant(t):
'''constant : ICONST
| FCONST
| CCONST'''
@@ -849,7 +849,7 @@ def p_empty(t):
pass
def p_error(t):
- print "Whoa. We're hosed"
+ print("Whoa. We're hosed")
import profile
# Build the grammar
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()
diff --git a/ext/ply/example/calcdebug/calc.py b/ext/ply/example/calcdebug/calc.py
new file mode 100644
index 000000000..6732f9f32
--- /dev/null
+++ b/ext/ply/example/calcdebug/calc.py
@@ -0,0 +1,113 @@
+# -----------------------------------------------------------------------------
+# calc.py
+#
+# This example shows how to run the parser in a debugging mode
+# with output routed to a logging object.
+# -----------------------------------------------------------------------------
+
+import sys
+sys.path.insert(0,"../..")
+
+if sys.version_info[0] >= 3:
+ raw_input = input
+
+tokens = (
+ 'NAME','NUMBER',
+ )
+
+literals = ['=','+','-','*','/', '(',')']
+
+# Tokens
+
+t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
+
+def t_NUMBER(t):
+ r'\d+'
+ t.value = int(t.value)
+ return t
+
+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])
+ t.lexer.skip(1)
+
+# Build the lexer
+import ply.lex as lex
+lex.lex()
+
+# Parsing rules
+
+precedence = (
+ ('left','+','-'),
+ ('left','*','/'),
+ ('right','UMINUS'),
+ )
+
+# dictionary of names
+names = { }
+
+def p_statement_assign(p):
+ 'statement : NAME "=" expression'
+ names[p[1]] = p[3]
+
+def p_statement_expr(p):
+ 'statement : expression'
+ print(p[1])
+
+def p_expression_binop(p):
+ '''expression : expression '+' expression
+ | expression '-' expression
+ | expression '*' expression
+ | expression '/' expression'''
+ if p[2] == '+' : p[0] = p[1] + p[3]
+ elif p[2] == '-': p[0] = p[1] - p[3]
+ elif p[2] == '*': p[0] = p[1] * p[3]
+ elif p[2] == '/': p[0] = p[1] / p[3]
+
+def p_expression_uminus(p):
+ "expression : '-' expression %prec UMINUS"
+ p[0] = -p[2]
+
+def p_expression_group(p):
+ "expression : '(' expression ')'"
+ p[0] = p[2]
+
+def p_expression_number(p):
+ "expression : NUMBER"
+ p[0] = p[1]
+
+def p_expression_name(p):
+ "expression : NAME"
+ try:
+ p[0] = names[p[1]]
+ except LookupError:
+ print("Undefined name '%s'" % p[1])
+ p[0] = 0
+
+def p_error(p):
+ if p:
+ print("Syntax error at '%s'" % p.value)
+ else:
+ print("Syntax error at EOF")
+
+import ply.yacc as yacc
+yacc.yacc()
+
+import logging
+logging.basicConfig(
+ level=logging.INFO,
+ filename="parselog.txt"
+)
+
+while 1:
+ try:
+ s = raw_input('calc > ')
+ except EOFError:
+ break
+ if not s: continue
+ yacc.parse(s,debug=logging.getLogger())
diff --git a/ext/ply/example/classcalc/calc.py b/ext/ply/example/classcalc/calc.py
index b2f3f70f1..bf0d065e4 100644..100755
--- a/ext/ply/example/classcalc/calc.py
+++ b/ext/ply/example/classcalc/calc.py
@@ -12,7 +12,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,7 +53,7 @@ class Parser:
if not s: continue
yacc.parse(s)
-
+
class Calc(Parser):
tokens = (
@@ -77,7 +79,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
@@ -87,9 +89,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
@@ -107,7 +109,7 @@ class Calc(Parser):
def p_statement_expr(self, p):
'statement : expression'
- print p[1]
+ print(p[1])
def p_expression_binop(self, p):
"""
@@ -141,11 +143,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()
diff --git a/ext/ply/example/cleanup.sh b/ext/ply/example/cleanup.sh
index 3e115f41c..3e115f41c 100644..100755
--- a/ext/ply/example/cleanup.sh
+++ b/ext/ply/example/cleanup.sh
diff --git a/ext/ply/example/closurecalc/calc.py b/ext/ply/example/closurecalc/calc.py
new file mode 100644
index 000000000..6598f5844
--- /dev/null
+++ b/ext/ply/example/closurecalc/calc.py
@@ -0,0 +1,130 @@
+# -----------------------------------------------------------------------------
+# calc.py
+#
+# A calculator parser that makes use of closures. The function make_calculator()
+# returns a function that accepts an input string and returns a result. All
+# lexing rules, parsing rules, and internal state are held inside the function.
+# -----------------------------------------------------------------------------
+
+import sys
+sys.path.insert(0,"../..")
+
+if sys.version_info[0] >= 3:
+ raw_input = input
+
+# Make a calculator function
+
+def make_calculator():
+ import ply.lex as lex
+ import ply.yacc as yacc
+
+ # ------- Internal calculator state
+
+ variables = { } # Dictionary of stored variables
+
+ # ------- Calculator tokenizing rules
+
+ tokens = (
+ 'NAME','NUMBER',
+ )
+
+ literals = ['=','+','-','*','/', '(',')']
+
+ t_ignore = " \t"
+
+ t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
+
+ def t_NUMBER(t):
+ r'\d+'
+ t.value = int(t.value)
+ return 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])
+ t.lexer.skip(1)
+
+ # Build the lexer
+ lexer = lex.lex()
+
+ # ------- Calculator parsing rules
+
+ precedence = (
+ ('left','+','-'),
+ ('left','*','/'),
+ ('right','UMINUS'),
+ )
+
+ def p_statement_assign(p):
+ 'statement : NAME "=" expression'
+ variables[p[1]] = p[3]
+ p[0] = None
+
+ def p_statement_expr(p):
+ 'statement : expression'
+ p[0] = p[1]
+
+ def p_expression_binop(p):
+ '''expression : expression '+' expression
+ | expression '-' expression
+ | expression '*' expression
+ | expression '/' expression'''
+ if p[2] == '+' : p[0] = p[1] + p[3]
+ elif p[2] == '-': p[0] = p[1] - p[3]
+ elif p[2] == '*': p[0] = p[1] * p[3]
+ elif p[2] == '/': p[0] = p[1] / p[3]
+
+ def p_expression_uminus(p):
+ "expression : '-' expression %prec UMINUS"
+ p[0] = -p[2]
+
+ def p_expression_group(p):
+ "expression : '(' expression ')'"
+ p[0] = p[2]
+
+ def p_expression_number(p):
+ "expression : NUMBER"
+ p[0] = p[1]
+
+ def p_expression_name(p):
+ "expression : NAME"
+ try:
+ p[0] = variables[p[1]]
+ except LookupError:
+ print("Undefined name '%s'" % p[1])
+ p[0] = 0
+
+ def p_error(p):
+ if p:
+ print("Syntax error at '%s'" % p.value)
+ else:
+ print("Syntax error at EOF")
+
+
+ # Build the parser
+ parser = yacc.yacc()
+
+ # ------- Input function
+
+ def input(text):
+ result = parser.parse(text,lexer=lexer)
+ return result
+
+ return input
+
+# Make a calculator object and use it
+calc = make_calculator()
+
+while True:
+ try:
+ s = raw_input("calc > ")
+ except EOFError:
+ break
+ r = calc(s)
+ if r:
+ print(r)
+
+
diff --git a/ext/ply/example/hedit/hedit.py b/ext/ply/example/hedit/hedit.py
index 494f4fde5..2e80675f9 100644
--- a/ext/ply/example/hedit/hedit.py
+++ b/ext/ply/example/hedit/hedit.py
@@ -29,17 +29,17 @@ def t_H_EDIT_DESCRIPTOR(t):
r"\d+H.*" # This grabs all of the remaining text
i = t.value.index('H')
n = eval(t.value[:i])
-
+
# Adjust the tokenizing position
t.lexer.lexpos -= len(t.value) - (i+1+n)
-
+
t.value = t.value[i+1:i+1+n]
- return t
-
+ return t
+
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()
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()
diff --git a/ext/ply/example/optcalc/README b/ext/ply/example/optcalc/README
index 6d196f0ee..53dd5fcd5 100644
--- a/ext/ply/example/optcalc/README
+++ b/ext/ply/example/optcalc/README
@@ -5,5 +5,5 @@ To run:
- Then run 'python -OO calc.py'
-If working corretly, the second version should run the
+If working correctly, the second version should run the
same way.
diff --git a/ext/ply/example/optcalc/calc.py b/ext/ply/example/optcalc/calc.py
index 3a0ee6c9b..dd83351a0 100644
--- a/ext/ply/example/optcalc/calc.py
+++ b/ext/ply/example/optcalc/calc.py
@@ -8,6 +8,9 @@
import sys
sys.path.insert(0,"../..")
+if sys.version_info[0] >= 3:
+ raw_input = input
+
tokens = (
'NAME','NUMBER',
'PLUS','MINUS','TIMES','DIVIDE','EQUALS',
@@ -30,7 +33,7 @@ def t_NUMBER(t):
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
return t
@@ -39,11 +42,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(optimize=1)
@@ -65,7 +68,7 @@ def p_statement_assign(t):
def p_statement_expr(t):
'statement : expression'
- print t[1]
+ print(t[1])
def p_expression_binop(t):
'''expression : expression PLUS expression
@@ -95,11 +98,14 @@ def p_expression_name(t):
try:
t[0] = names[t[1]]
except LookupError:
- print "Undefined name '%s'" % t[1]
+ print("Undefined name '%s'" % t[1])
t[0] = 0
def p_error(t):
- print "Syntax error at '%s'" % t.value
+ if t:
+ print("Syntax error at '%s'" % t.value)
+ else:
+ print("Syntax error at EOF")
import ply.yacc as yacc
yacc.yacc(optimize=1)
diff --git a/ext/ply/example/unicalc/calc.py b/ext/ply/example/unicalc/calc.py
index d1f59f748..55fb48df4 100644
--- a/ext/ply/example/unicalc/calc.py
+++ b/ext/ply/example/unicalc/calc.py
@@ -41,11 +41,11 @@ t_ignore = u" \t"
def t_newline(t):
ur'\n+'
t.lexer.lineno += t.value.count("\n")
-
+
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
-
+
# Build the lexer
import ply.lex as lex
lex.lex()
@@ -100,7 +100,10 @@ def p_expression_name(p):
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()
diff --git a/ext/ply/example/yply/ylex.py b/ext/ply/example/yply/ylex.py
index 61bc0c7ef..84f2f7a73 100644
--- a/ext/ply/example/yply/ylex.py
+++ b/ext/ply/example/yply/ylex.py
@@ -42,7 +42,7 @@ def t_SECTION(t):
# Comments
def t_ccomment(t):
r'/\*(.|\n)*?\*/'
- t.lineno += t.value.count('\n')
+ t.lexer.lineno += t.value.count('\n')
t_ignore_cppcomment = r'//.*'
@@ -95,7 +95,7 @@ def t_code_error(t):
raise RuntimeError
def t_error(t):
- print "%d: Illegal character '%s'" % (t.lineno, t.value[0])
+ print "%d: Illegal character '%s'" % (t.lexer.lineno, t.value[0])
print t.value
t.lexer.skip(1)
@@ -104,9 +104,9 @@ lex.lex()
if __name__ == '__main__':
lex.runmain()
+
+
+
-
-
-
-
+
diff --git a/ext/ply/example/yply/yparse.py b/ext/ply/example/yply/yparse.py
index a4e46bef7..ab5b88451 100644
--- a/ext/ply/example/yply/yparse.py
+++ b/ext/ply/example/yply/yparse.py
@@ -25,7 +25,7 @@ def p_defsection(p):
print "precedence = ", repr(preclist)
print
print "# -------------- RULES ----------------"
- print
+ print
def p_rulesection(p):
'''rulesection : rules SECTION'''
@@ -78,12 +78,12 @@ def p_idlist(p):
p[1].append(p[3])
def p_tokenid(p):
- '''tokenid : ID
+ '''tokenid : ID
| ID NUMBER
| QLITERAL
| QLITERAL NUMBER'''
p[0] = p[1]
-
+
def p_optsemi(p):
'''optsemi : ';'
| empty'''
@@ -165,7 +165,7 @@ def p_rule_empty(p):
def p_rule_empty2(p):
'''rule : ID ':' morerules ';' '''
-
+
p[3].insert(0,[])
p[0] = (p[1],p[3])
@@ -173,10 +173,10 @@ def p_morerules(p):
'''morerules : morerules '|' rulelist
| '|' rulelist
| '|' '''
-
- if len(p) == 2:
+
+ if len(p) == 2:
p[0] = [[]]
- elif len(p) == 3:
+ elif len(p) == 3:
p[0] = [p[2]]
else:
p[0] = p[1]
diff --git a/ext/ply/example/yply/yply.py b/ext/ply/example/yply/yply.py
index a4398171e..a4398171e 100644..100755
--- a/ext/ply/example/yply/yply.py
+++ b/ext/ply/example/yply/yply.py