pmacs3/miniparse.py

28 lines
648 B
Python
Raw Permalink Normal View History

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 range(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)