本文主要是介绍Keil C51 插件 检测所有if语句,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
此插件解决的问题
Keil 插件 -- Python 代码
import chardet, sys, glob, re
from pathlib import Pathfrom collections import deque# 变量名字典 key--数据名 value--数据s类型
variable_dic = {}# 初始化一个空的二维数组
matrix = []
# 列表的头数据
header_list = []
# 列表的值
value_list = []# if (V_Data>35&&V_Data<790) //零上温度校准\r
# if (V_Data==Temp_Tabe[i][1])\r
# if ((Usart1_Receive_Buff[4]==0x01)&&(Usart1_Receive_Buff[Receive_Count-1]==Xor_Data1))\r# uint AAA
# uint AAA,BBB
# uint code AAA,BBB -- 变量名和类型添加
def type_variale_name_split(row_text):type_header = row_text.split()[0]if 'xdata' in row_text or 'code' in row_text:type_header = ' '.join(row_text.split()[0:2])if ',' in row_text: # uchar data A,B uint a,c,dfor item in row_text.split(','):T = list(map(str.strip, item.split()))[-1]add_key(list(map(str.strip, item.split()))[-1], type_header)else: # uchar data A uint Brow_text_list = row_text.split()if 'xdata' in row_text or 'code' in row_text:add_key(row_text_list[2].strip(),type_header)else:add_key(row_text_list[1].strip(),type_header)# uint AA=0
# uint AA=0,BB=0
# uint xdata AAA=0
# uint xdata AAA[BBB]={0} -- 变量名和类型添加
def type_variable_name_equal_split(row_text):# 裁剪定义的数组if '{' in row_text:row_text = row_text.split('{')[0]type_header = row_text.split()[0]if 'xdata' in row_text or 'code' in row_text:type_header = ' '.join(row_text.split()[0:2])# uint AA=0,BB=0# uint code AA=0,BB=0,CC=1# uint code A=0;if ',' in row_text:for item in row_text.split(','):type_header_variable = item.split('=')[0]# uint AAif type_header in type_header_variable:add_key(type_header_variable.split(type_header)[1].strip(), type_header)else: # AAadd_key(type_header_variable.strip(), type_header)else: # uint AA=0# uchar xdata B=0type_header_variable_else = row_text.split('=')[0]type_header_variable_else_list = list(map(str.strip, type_header_variable_else.split()))add_key(type_header_variable_else_list[-1],type_header)# 变量名和类型添加
def add_key(key, value):# 若key存在if key in variable_dic:returnelse:variable_dic[key] = value# 添加if语句 -- if 语句分割添加
def add_variable_list(string):# (V_Data>35&&V_Data<790)try:cut_if_string = string.split('if')[1].strip()if '(' not in cut_if_string:returnpattern_header = r'\([^!=|<>]+'list_header = re.findall(pattern_header, cut_if_string)[0].split('(')[-1].strip()# 若存在, 则添加if list_header in variable_dic:list_header = variable_dic[list_header] + ' ' + list_headerelse:list_header = '未查询到 ' + list_header# 若不存在, 则不添加wait_list = [list_header, string]# 若头文件存在for row in matrix:if list_header == row[0]:row.append(string)return# 若不存在, 则添加matrix.append(wait_list)except IndexError:print("报错了")# 获取一个文件的内容
def get_bit_file_content(filename, default_encoding='gb2312'):file = ''content = ''try:file = open(filename, 'rb')content = file.read() # 以字节方式读取except:print("文件" + filename + "打开失败")finally:file.close()source_encoding = chardet.detect(content)['encoding']string = content.decode(source_encoding if source_encoding else default_encoding, 'ignore')return string# 获取所有的if语句分割后添加到二维数组中
def get_all_if_matrix(filename):string = get_bit_file_content(filename)# 正则 if(a==0)pattern_type = r"(\bif\b.+)"matches = re.findall(pattern_type, string)# ['if (V_Data>35&&V_Data<790) //零上温度校准\r', 'if (V_Data==Temp_Tabe[i][1])\r',]for item in matches:add_variable_list(item)# 获取所有 变量名和类型 添加到字典中
def get_all_variable_dict(filename):string = get_bit_file_content(filename)# 正则 int AAA=0pattern_int = r"(?:\n\b(?:sbit|bit|uchar|uint|ulong|int|long|float|char|double)\b[^;)]+)"matches_int = re.findall(pattern_int,string)# 变量名添加for m_int in matches_int:# 剔除函数if '(' in m_int:continue# 剔除 =0m_int = m_int.lstrip('\n').strip()if '=' in m_int:# uint AAA=0,B=0type_variable_name_equal_split(m_int)else:# uint AAtype_variale_name_split(m_int)if __name__ == "__main__":try:filename = sys.argvfilelist = sum(list(map(glob.glob, filename[1:])), []) #获取所有文件名print('检测到的文件:')for item in filelist:print(item)# 获取所有的变量和类型list(map(get_all_variable_dict, filelist))# 获取所有的if语句list(map(get_all_if_matrix, filelist))# 获取桌面路径desktop_path = Path.home() / "Desktop"# print(desktop_path / 'output_all_if.txt')with open(desktop_path / 'output_if.txt', 'w') as fp:for row in matrix:fp.write(row[0]+'\n')for a_bit in row[1:]:fp.write(' '+a_bit)fp.write('\n')print('查询完成,文件保存至:')print(" " + str(desktop_path / 'output_all_if.txt'))except Exception as e:print(str(e))
需要修改的地方
上面代码,所需要的包,自行导入
Python 生成 exe
pip install pyinstaller -- 自行安装包
pyinstaller -Fw .\Select_All_if.py
Keil C51 插件安装
"$E*.c" "$E*.h" -- 表示当前的所有文件
效果
不会改的,自行留言
这篇关于Keil C51 插件 检测所有if语句的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!