重庆网站备案规定,旅游+网站建设,网页设计规范怎么写,健身网站开发开题报告文章目录 安装 pycparser 模块模块开发者网址获取抽象语法树1. 需要导入的模块2. 获取 不关注预处理相关 c语言文件的抽象语法树ast3. 获取 预处理后的c语言文件的抽象语法树ast 语法树组成1. 数据类型定义 Typedef2. 类型声明 TypeDecl3. 标识符类型 IdentifierType4. 变量声明… 文章目录 安装 pycparser 模块模块开发者网址获取抽象语法树1. 需要导入的模块2. 获取 不关注预处理相关 c语言文件的抽象语法树ast3. 获取 预处理后的c语言文件的抽象语法树ast 语法树组成1. 数据类型定义 Typedef2. 类型声明 TypeDecl3. 标识符类型 IdentifierType4. 变量声明 Decl5. 常量 Constant6. 函数定义 FuncDef7. 函数声明 FuncDecl8. 函数参数列表 ParamList9. 代码块 Compound to do 感谢这两篇文章对于我学习之初的帮助 https://blog.csdn.net/u011079613/article/details/122462729 https://blog.csdn.net/qq_38808667/article/details/118059074 安装 pycparser 模块
pip install pycparser -i https://mirrors.aliyun.com/pypi/simple/模块开发者网址
https://github.com/eliben/pycparser
获取抽象语法树
1. 需要导入的模块
# parser_file 用于处理c语言文件
from pycparser import parse_file
from pycparser import CParser
# c语言有错误时会引出此错误
from pycparser.plyparser import ParseError
# c_ast.py 文件下包含了抽象语法树的节点类
from pycparser.c_ast import *2. 获取 不关注预处理相关 c语言文件的抽象语法树ast
文件中需删除 #开头 预处理代码不能有注释代码
方法1
ast parse_file(filename, use_cpp False)方法2
with open(filename, encodingutf-8,) as f:txt f.read()
ast CParser().parse(txt) # 使用此方法需要 删除头文件3. 获取 预处理后的c语言文件的抽象语法树ast
获取c语言文件的抽象语法树ast如果要处理 #include 等语句需要下载fake_libc_include文件夹让编译器预处理常用的方法添加其到代码的抽象语法树中 点击此处下载 fake_libc_include cpp_args必须加上 -E , 否则返回的抽象语法树是个空列表
ast parse_file(filename, use_cpp True, cpp_pathrC:\MinGW\bin\gcc.exe, cpp_args[-E, r-Iutils/fake_libc_include])
使用 parse_file 类获取 预处理后的c语言文件的抽象语法树ast
parse_file 参数说明filename需要解析的 .c 文件名use_cpp是否使用本地c语言编译器预处理代码去掉其中的#命令头文件、宏定义、pragma值False/Truecpp_path本地c语言编译器路径cpp_argsfake_libc_include文件夹路径需要在路径添加 -I 指明所包头文件路径; use_cppTrue 时使用
语法树组成
抽象语法树 ast 类型为 class pycparser.c_ast.FileAST
其解析的具体内容通过 print(ast.ext) 查看ext 数据类型为列表
FileAST 下级节点只有 3 种可能 :
Typedef typedef 数据类型定义Decl 变量声明FuncDef 函数声明
示例 test.c
typedef int uint32;
int g 0;
int add(int a, int b)
{int c 0;c a b;return c;
}
int main(void)
{printf(hello world);return 0;
}cparser.py
# parser_file 用于处理c语言文件
from pycparser import parse_file
from pycparser import CParser
# c语言有错误时会引出此错误
from pycparser.plyparser import ParseError
# c_ast.py 文件下包含了抽象语法树的节点类
from pycparser.c_ast import *filename test.cast parse_file(filename, use_cpp False)print(type(ast))for eachNode in ast.ext:print(eachNode.__class__.__name__) # 打印节点类型名#print(eachNode) # 打印节点内容
输出
1. 数据类型定义 Typedef
Typedef 数据结构类型 class pycparser.c_ast.Typedef
数据类型定义 Typedef 属性如下
Typedef.name str Typedef 定义对象Typedef.quals [str] 限定符号列表: const, volatileTypedef.storage [str] 存储说明符列表: extern, register, etc.Typedef.type Node TypeDecl节点Typedef.coord str定义对象所在行列 Typedef.coord.column str定义对象所在列Typedef.coord.line str定义对象所在行Typedef.coord.file str定义对象所在文件
示例 test.c typedef const int cuint32;cparser.py
# parser_file 用于处理c语言文件
from pycparser import parse_file
from pycparser import CParser
# c语言有错误时会引出此错误
from pycparser.plyparser import ParseError
# c_ast.py 文件下包含了抽象语法树的节点类
from pycparser.c_ast import *filename test.cast parse_file(filename, use_cpp False)print(type(ast.ext[0]))print(name , ast.ext[0].name) # Typedef 定义对象
print(quals , ast.ext[0].quals)
print(storage , ast.ext[0].storage)
print(type , ast.ext[0].type)
print(coord , ast.ext[0].coord)
输出
2. 类型声明 TypeDecl
Typedef 的下一级 类型声明 TypeDecl 是以typedef语句格式为中心
类型声明 TypeDecl 属性如下
TypeDecl.declname str typedef定义对象TypeDecl.quals [str] 限定符号列表: const, volatileTypeDecl.align [str] 暂不清楚TypeDecl.type Node IdentifierType节点TypeDecl.coord str定义对象所在行列 TypeDecl.coord.column str定义对象所在列TypeDecl.coord.line str定义对象所在行TypeDecl.coord.file str定义对象所在文件
示例 test.c typedef const int cuint32;cparser.py
# parser_file 用于处理c语言文件
from pycparser import parse_file
from pycparser import CParser
# c语言有错误时会引出此错误
from pycparser.plyparser import ParseError
# c_ast.py 文件下包含了抽象语法树的节点类
from pycparser.c_ast import *filename test.cast parse_file(filename, use_cpp False)print(type(ast.ext[0].type))my_typeDecl ast.ext[0].typeprint(name , my_typeDecl.declname) # Typedef 定义对象
print(quals , my_typeDecl.quals)
print(type , my_typeDecl.type)
print(storage , my_typeDecl.align)
print(coord , my_typeDecl.coord)
print(coord.column , my_typeDecl.coord.column) # 定义对象所在列
print(coord.line , my_typeDecl.coord.line) # 定义对象所在行
print(coord.file , my_typeDecl.coord.file) # 定义对象所在文件输出
3. 标识符类型 IdentifierType
TypeDecl 的下一级 标识符类型 IdentifierType 是简单标识符比如 void, char 定义之类 原数据类型 class pycparser.c_ast.IdentifierType
标识符类型 IdentifierType 属性如下
IdentifierType.name [str] 标识符字符串列表IdentifierType.coord str定义对象所在行列 IdentifierType.coord.column str定义对象所在列IdentifierType.coord.line str定义对象所在行IdentifierType.coord.file str定义对象所在文件
4. 变量声明 Decl
Decl 数据结构类型 class pycparser.c_ast.Decl
变量声明 Decl 属性如下
Decl.name str (被声明的变量名Decl.quals [str] 限定符号列表: const, volatileDecl.align [str] 暂不清楚Decl.storage [str] 存储说明符列表: extern, register, static等Decl.funcspec [str] 函数说明符列表: C99的inlineDecl.type Node TypeDecl 节点Decl.init Node 初始化值Constant节点Decl.bitsize Node 位域bit field大小或者为NoneDecl.coord str定义对象所在行列 Decl.coord.column str定义对象所在列Decl.coord.line str定义对象所在行Decl.coord.file str定义对象所在文件
示例 test.c typedef const int cuint32;static const int g 0;cparser.py
# parser_file 用于处理c语言文件
from pycparser import parse_file
from pycparser import CParser
# c语言有错误时会引出此错误
from pycparser.plyparser import ParseError
# c_ast.py 文件下包含了抽象语法树的节点类
from pycparser.c_ast import *filename test.cast parse_file(filename, use_cpp False)print(type(ast.ext[1]))my_ext ast.ext[1]print(name , ast.ext[1].name) # Typedef 定义对象
print(quals , ast.ext[1].quals)
print(align , ast.ext[1].align)
print(storage , ast.ext[1].storage)
print(funcspec , ast.ext[1].funcspec)
print(type , ast.ext[1].type)
print(init , ast.ext[1].init)
print(bitsize , ast.ext[1].bitsize)
print(coord , ast.ext[1].coord)输出
5. 常量 Constant
常量 Constant 属性如下
Constant.type str 基本数据类型int等Constant.value str 数值Constant.coord str定义对象所在行列 Constant.coord.column str定义对象所在列Constant.coord.line str定义对象所在行Constant.coord.file str定义对象所在文件
6. 函数定义 FuncDef
FuncDef 方法定义不同于 FuncDecl有具体的函数实现过程
函数定义 FuncDef 属性如下
FuncDef.decl Node 一般是包含Decl的节点param_declsNone 暂不清楚FuncDef.body Node 函数实现的代码块 一般是包含Compound 的节点FuncDef.coord str标识符字符串所在行列 FuncDef.coord.column str定义对象所在列FuncDef.coord.line str定义对象所在行FuncDef.coord.file str定义对象所在文件
7. 函数声明 FuncDecl
FuncDecl 既可以单独存在也可以是函数定义的一部分
函数定义 FuncDecl 属性如下
FuncDecl.args Node 一般是包含ParamList的节点FuncDecl.type [str] 一般是包含TypeDecl的节点
8. 函数参数列表 ParamList
以 list 形式可遍历 参数 函数定义 ParamList 属性如下
ParamList.params [str]有哪些参数 一般是包含Decl的节点
9. 代码块 Compound
以 list 形式可遍历 代码块内容
函数定义 Compound 属性如下
Compound .block_items [str]有哪些参数 一般是包含 Decl Assignment 和 Return的节点
to do
解析任意编程语言 tree-sitter