1#----------------------------------------------------------------- 2# pycparser: rewrite_ast.py 3# 4# Tiny example of rewriting a AST node 5# 6# Eli Bendersky [https://eli.thegreenplace.net/] 7# License: BSD 8#----------------------------------------------------------------- 9from __future__ import print_function 10import sys 11 12from pycparser import c_parser 13 14text = r""" 15void func(void) 16{ 17 x = 1; 18} 19""" 20 21parser = c_parser.CParser() 22ast = parser.parse(text) 23print("Before:") 24ast.show(offset=2) 25 26assign = ast.ext[0].body.block_items[0] 27assign.lvalue.name = "y" 28assign.rvalue.value = 2 29 30print("After:") 31ast.show(offset=2) 32