pmacs3/miniparse.py

28 lines
649 B
Python

import parser
import sys
import token
from pprint import pprint
def proc(asttup):
queue = [asttup]
pairs = []
while queue:
node = queue.pop(0)
if not node:
continue
elif node[0] in token.tok_name:
pairs.append((token.tok_name[node[0]], node[1]))
else:
for i in xrange(0, len(node) - 1):
queue.insert(i, node[i + 1])
return pairs
if __name__ == "__main__":
for name in sys.argv[1:]:
f = open(name, 'r')
code = f.read()
f.close()
ast = parser.suite(code)
pairs = proc(ast.totuple())
pprint(pairs)