You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
612 B

#-----------------------------------------------------------------
# pycparser: rewrite_ast.py
#
# Tiny example of rewriting a AST node
#
# Eli Bendersky [https://eli.thegreenplace.net/]
# License: BSD
#-----------------------------------------------------------------
from __future__ import print_function
import sys
from pycparser import c_parser
text = r"""
void func(void)
{
x = 1;
}
"""
parser = c_parser.CParser()
ast = parser.parse(text)
print("Before:")
ast.show(offset=2)
assign = ast.ext[0].body.block_items[0]
assign.lvalue.name = "y"
assign.rvalue.value = 2
print("After:")
ast.show(offset=2)