입력/수정 코드 입력 소스 코드를 수정하려고합니다. 입력 코드의 모든 줄 뒤에 printf
문을 추가하려고합니다.pycparser AST |를 수정할 수 없습니다. AST를 C 코드로 변환
예 : 내 입력 인 경우 -
void foo(){
// Sample input code
int a = 0, b = 0;
a++;
if(a<5)
b++;
b--;
}
내가로 이어지는 문
printf('Hi');
를 추가 할 것
- 초기 단계로
void foo(){
int a = 0, b = 0;
printf('Hi');
a++;
printf('Hi');
if(a<5){
b++;
printf('Hi');
}
printf('Hi');
b--;
printf('Hi');
}
, 나는 단순히 변수 test
를 선언하고 처음에 삽입하는 시도 시도 무작위 소스 코드에 의해 생성 된 AST의 여기 ast
객체에 AST를 추출 한 후, 내가 관여 된 파이썬 코드의 -
for i in range(0,len(ast.ext)):
## Look for a function named 'foo'
if(type(ast.ext[i]) == c_ast.FuncDef and ast.ext[i].decl.name == 'foo'):
## Store the list of AST node objects in functionBody
functionBody = ast.ext[i].body
## Create a Decl object for the variable test
id_obj = c_ast.ID('test')
identifier_obj = c_ast.IdentifierType(['int'])
typedecl_obj = c_ast.TypeDecl(id_obj.name,[],identifier_obj)
decl_obj = c_ast.Decl(id_obj.name,[],[],[],typedecl_obj,[],[])
## Append the object to a list.
## Concatenate to a copy of existing list of AST objects
lst1 = []
lst1.append(decl_obj)
lst2 = []
lst2 = copy.deepcopy(functionBody.block_items)
lst3 = []
lst3 = lst1+lst2
## Create a modified AST and print content
functionBody1 = c_ast.Compound(lst3)
functionBody1.show()
내가 결과 구조에 변화를 찾을 functionBody1
내가 그 show()
를 사용하려고 할 때마다 다음과 같은 오류를 얻을 수 방법.
'list' object has no attribute 'show'
내가 어디에서 트랙을 벗어나려는 생각이 있으십니까?
감사
lst2와 lst3이 []로 초기화 된 다음 다른 것을 할당 한 이유가 있습니까? 너는 lst1에 그렇게하지 않는다. –
@IraBaxter _lst1_을 _ [] _으로 초기화했습니다. 그리고 그럴 이유가 없습니다. 이 변수들이 _list_ 유형임을 나타냅니다. – Shash
수정되지 않은 함수의 텍스트를 다시 생성 할 수 있습니까? –