The parser Module

(Optional) The parser module provides an interface to Pythons built-in parser and compiler.

Example 13-19 compiles a simple expression into an abstract syntax tree (AST), turns the AST into a nested list, dumps the contents of the tree (where each node contains either a grammar symbol or a token), increments all numbers by one, and, finally, turns the list back into a code object. At least thats what I think it does.

Example 13-19. Using the parser Module

import parser
import symbol, token

def dump_and_modify(node):
 name = symbol.sym_name.get(node[0])
 if name is None:
 name = token.tok_name.get(node[0])
 print name,
 for i in range(1, len(node)):
 item = node[i]
 if type(item) is type([]):
 print repr(item)
 if name == "NUMBER":
 # increment all numbers!
 node[i] = repr(int(item)+1)

ast = parser.expr("1 + 3")

list = ast.tolist()


ast = parser.sequence2ast(list)

print eval(parser.compileast(ast))

eval_input testlist test and_test not_test comparison
expr xor_expr and_expr shift_expr arith_expr term factor
power atom NUMBER 1
term factor power atom NUMBER 3

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh © 2008-2020.
If you may any questions please contact us: