问题记录 --Error parsing column 1 (Function_Num=10 - String)”

本文探讨了在使用C#进行数据库查询时遇到的Errorparsingcolumn##错误,详细分析了这一错误的原因在于model类型与数据库字段类型的不匹配,并提供了相应的解决思路。

当C#查询数据库出现Error parsing column ## 的时候,首先去看看数据库里面该字段是什么类型,然后在看看你在创建model 的时候是什么类型,如果model的类型和数据库字段类型不符合就会出现这种错误。

转载于:https://www.cnblogs.com/dashanboke/p/10144463.html

import tkinter as tk from tkinter import scrolledtext, ttk, messagebox import logging from datetime import datetime import traceback class SimpleCLexer: def __init__(self): self.tokens = [] def tokenize(self, input_str): tokens = [] pos = 0 line = 1 column = 0 length = len(input_str) # 定义C语言的关键词和类型 keywords = { 'void', 'int', 'char', 'float', 'double', 'short', 'long', 'signed', 'unsigned', 'struct', 'union', 'enum', 'typedef', 'static', 'extern', 'auto', 'register', 'const', 'volatile', 'return', 'if', 'else', 'switch', 'case', 'default', 'for', 'while', 'do', 'break', 'continue', 'goto', 'sizeof' } # 扩展类型别名识别 types = {'U1', 'U2', 'U4', 'S1', 'S2', 'S4', 'BOOL', 'BYTE', 'WORD', 'DWORD'} while pos < length: char = input_str[pos] # 跳过空白字符 if char in ' \t': pos += 1 column += 1 continue # 处理换行 if char == '\n': line += 1 column = 0 pos += 1 continue # 处理单行注释 if pos + 1 < length and input_str[pos:pos+2] == '//': end = input_str.find('\n', pos) if end == -1: end = length pos = end continue # 处理多行注释 if pos + 1 < length and input_str[pos:pos+2] == '/*': end = input_str.find('*/', pos + 2) if end == -1: end = length else: end += 2 pos = end continue # 处理标识符 if char.isalpha() or char == '_': start = pos pos += 1 while pos < length and (input_str[pos].isalnum() or input_str[pos] == '_'): pos += 1 token_text = input_str[start:pos] token_type = 'IDENTIFIER' # 检查是否为关键字或类型 if token_text in keywords: token_type = 'KEYWORD' elif token_text in types: token_type = 'TYPE' tokens.append({ 'type': token_type, 'text': token_text, 'line': line, 'column': column }) column += (pos - start) continue # 处理数字 if char.isdigit(): start = pos pos += 1 while pos < length and (input_str[pos].isdigit() or input_str[pos] in '.xXabcdefABCDEF'): pos += 1 tokens.append({ 'type': 'NUMBER', 'text': input_str[start:pos], 'line': line, 'column': column }) column += (pos - start) continue # 处理字符串 if char == '"': start = pos pos += 1 while pos < length and input_str[pos] != '"': if input_str[pos] == '\\' and pos + 1 < length: pos += 2 else: pos += 1 if pos < length and input_str[pos] == '"': pos += 1 tokens.append({ 'type': 'STRING', 'text': input_str[start:pos], 'line': line, 'column': column }) column += (pos - start) continue # 处理字符 if char == "'": start = pos pos += 1 while pos < length and input_str[pos] != "'": if input_str[pos] == '\\' and pos + 1 < length: pos += 2 else: pos += 1 if pos < length and input_str[pos] == "'": pos += 1 tokens.append({ 'type': 'CHAR', 'text': input_str[start:pos], 'line': line, 'column': column }) column += (pos - start) continue # 处理运算符和标点符号 operators = { '(', ')', '{', '}', '[', ']', ';', ',', '.', '->', '++', '--', '&', '*', '+', '-', '~', '!', '/', '%', '<<', '>>', '<', '>', '<=', '>=', '==', '!=', '^', '|', '&&', '||', '?', ':', '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '&=', '^=', '|=', ',' } # 尝试匹配最长的运算符 matched = False for op_len in range(3, 0, -1): if pos + op_len <= length and input_str[pos:pos+op_len] in operators: tokens.append({ 'type': 'OPERATOR', 'text': input_str[pos:pos+op_len], 'line': line, 'column': column }) pos += op_len column += op_len matched = True break if matched: continue # 无法识别的字符 tokens.append({ 'type': 'UNKNOWN', 'text': char, 'line': line, 'column': column }) pos += 1 column += 1 return tokens class FunctionAnalyzer: def __init__(self): self.function_name = "" self.parameters = [] self.global_vars = [] self.function_calls = [] self.in_function = False self.in_function_body = False self.brace_depth = 0 self.variable_declarations = {} self.macro_definitions = set() self.storage_classes = {"static", "extern", "auto", "register"} self.local_vars = [] # 局部变量列表 self.struct_tags = set() # 存储识别的结构体标签 self.recorded_locals = set() # 新增:跟踪已记录的局部变量 self.recorded_globals = set() self.recorded_params = set() # 定义允许的类型 self.basic_types = {'void', 'int', 'char', 'float', 'double', 'short', 'long', 'signed', 'unsigned'} self.type_aliases = {"U1", "U2", "U4", "S1", "S2", "S4"} self.allowed_types = self.basic_types | self.type_aliases self.allowed_types.add('struct') # 添加结构体支持 self.debug_level = 2 # 1=基本, 2=详细 def analyze(self, tokens): self.tokens = tokens self.pos = 0 self.current_line = 0 self.brace_depth = 0 # 识别宏定义 self._identify_macros() while self.pos < len(self.tokens): token = self.tokens[self.pos] self.current_line = token['line'] # 检测结构体类型声明(只识别,不处理定义) if token['text'] == 'struct' and self.pos + 1 < len(self.tokens): next_token = self.tokens[self.pos + 1] if next_token['type'] == 'IDENTIFIER': # 仅记录结构体标签,不跳过token self.struct_tags.add(next_token['text']) self.allowed_types.add(next_token['text']) # 跟踪大括号深度 if token['text'] == '{': self.brace_depth += 1 if self.in_function and self.brace_depth == 1: self.in_function_body = True elif token['text'] == '}': self.brace_depth -= 1 if self.brace_depth == 0 and self.in_function: self.in_function = False self.in_function_body = False # 检测函数定义 if token['text'] in self.storage_classes or token['text'] in self.allowed_types: if self._is_function_definition(): self._handle_function_definition() continue # 检测变量声明 - 只在函数体内处理 if self.in_function_body and token['text'] in self.allowed_types: # 检查下一个token是否是标识符(变量名) if self.pos + 1 < len(self.tokens) and \ self.tokens[self.pos + 1]['type'] == 'IDENTIFIER': # 确保不是函数返回类型 if self.pos + 2 < len(self.tokens) and self.tokens[self.pos + 2]['text'] != '(': self._handle_variable_declaration() continue # 检测函数调用 if token['type'] == 'IDENTIFIER' and self.pos + 1 < len(self.tokens): next_token = self.tokens[self.pos + 1] if next_token['text'] == '(': self._handle_function_call() continue # 检测变量使用 if token['type'] == 'IDENTIFIER': self._handle_identifier_use(token) self.pos += 1 return self def _identify_macros(self): """识别宏定义(全大写标识符)""" for token in self.tokens: if token['type'] == 'IDENTIFIER' and token['text'].isupper(): self.macro_definitions.add(token['text']) def _is_function_definition(self): pos = self.pos storage_class = None # 检测存储类说明符 if self.tokens[pos]['text'] in self.storage_classes: storage_class = self.tokens[pos]['text'] pos += 1 # 检测返回类型 if pos >= len(self.tokens) or self.tokens[pos]['text'] not in self.allowed_types: return False return_type = self.tokens[pos]['text'] pos += 1 # 处理指针声明 if pos < len(self.tokens) and self.tokens[pos]['text'] == '*': return_type += '*' pos += 1 # 检测函数名 if pos < len(self.tokens) and self.tokens[pos]['type'] == 'IDENTIFIER': func_name = self.tokens[pos]['text'] pos += 1 else: return False # 检测参数列表开头的'(' if pos < len(self.tokens) and self.tokens[pos]['text'] == '(': pos += 1 else: return False # 检测函数体开头的'{' (允许最多5个token的间隔) found_brace = False for i in range(min(5, len(self.tokens) - pos)): if self.tokens[pos + i]['text'] == '{': found_brace = True break return found_brace def _handle_function_definition(self): start_pos = self.pos storage_class = None # 处理存储类说明符 if self.tokens[self.pos]['text'] in self.storage_classes: storage_class = self.tokens[self.pos]['text'] self.pos += 1 # 获取返回类型 return_type = self.tokens[self.pos]['text'] self.pos += 1 # 处理指针声明 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '*': return_type += '*' self.pos += 1 # 获取函数名 if self.pos < len(self.tokens) and self.tokens[self.pos]['type'] == 'IDENTIFIER': func_name = self.tokens[self.pos]['text'] self.pos += 1 else: self.pos = start_pos return # 记录函数名 self.function_name = func_name self.variable_declarations[func_name] = True # 跳过 '(' if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '(': self.pos += 1 # 提取参数 params = [] current_param = [] depth = 1 param_line = self.current_line while self.pos < len(self.tokens) and depth > 0: token = self.tokens[self.pos] if token['text'] == '(': depth += 1 elif token['text'] == ')': depth -= 1 if depth == 0: break elif token['text'] == ',' and depth == 1: # 提取参数类型和名称 param_type, param_name = self._extract_param_info(current_param) if param_type and param_name: params.append({ 'type': param_type, 'name': param_name, 'line': param_line }) self.variable_declarations[param_name] = True current_param = [] param_line = token['line'] self.pos += 1 continue current_param.append(token) self.pos += 1 # 处理最后一个参数 if current_param: param_type, param_name = self._extract_param_info(current_param) if param_type and param_name: params.append({ 'type': param_type, 'name': param_name, 'line': param_line }) self.variable_declarations[param_name] = True # 记录参数 self.parameters = params # 记录参数为参数类型 for param in params: self.recorded_params.add(param['name']) self.variable_declarations[param['name']] = True # 查找函数体开头的'{' while self.pos < len(self.tokens) and self.tokens[self.pos]['text'] != '{': self.pos += 1 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '{': self.in_function = True self.brace_depth = 0 self.pos += 1 return [p['name'] for p in params] if params else [] def _extract_param_info(self, tokens): """从参数token列表中提取类型和名称""" param_type = [] param_name = None for token in tokens: if token['type'] in ('KEYWORD', 'TYPE') or token['text'] in self.allowed_types: param_type.append(token['text']) elif token['type'] == 'IDENTIFIER' and not token['text'].isupper(): param_name = token['text'] return ' '.join(param_type), param_name def _handle_variable_declaration(self): start_pos = self.pos current_line = self.current_line # 获取变量类型 var_type = self.tokens[self.pos]['text'] # 处理结构体声明 is_struct = False if var_type == 'struct': is_struct = True # 收集结构体类型名 struct_type = [] self.pos += 1 while self.pos < len(self.tokens) and self.tokens[self.pos]['text'] != ';': token = self.tokens[self.pos] if token['text'] == '{': # 跳过结构体定义 depth = 1 self.pos += 1 while self.pos < len(self.tokens) and depth > 0: if self.tokens[self.pos]['text'] == '{': depth += 1 elif self.tokens[self.pos]['text'] == '}': depth -= 1 self.pos += 1 continue elif token['type'] == 'IDENTIFIER': struct_type.append(token['text']) self.pos += 1 if struct_type: var_type = 'struct ' + ' '.join(struct_type) else: var_type = 'struct' else: self.pos += 1 # 处理指针声明 while self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '*': var_type += '*' self.pos += 1 var_names = [] # 处理变量名和声明 while self.pos < len(self.tokens): token = self.tokens[self.pos] # 结束声明 if token['text'] == ';': self.pos += 1 break # 标识符 - 变量名 if token['type'] == 'IDENTIFIER' and not token['text'].isupper(): var_name = token['text'] # 跳过宏定义 if var_name not in self.macro_definitions: var_names.append(var_name) self.variable_declarations[var_name] = True self.pos += 1 continue # 逗号 - 多个变量声明 elif token['text'] == ',': self.pos += 1 # 处理指针声明 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '*': var_type += '*' self.pos += 1 continue # 数组声明 - 跳过数组大小 elif token['text'] == '[': self.pos += 1 depth = 1 while self.pos < len(self.tokens) and depth > 0: t = self.tokens[self.pos] if t['text'] == '[': depth += 1 elif t['text'] == ']': depth -= 1 self.pos += 1 continue # 初始化 - 跳过初始化表达式 elif token['text'] == '=': self.pos += 1 depth = 0 while self.pos < len(self.tokens): t = self.tokens[self.pos] if t['text'] in {'(', '['}: depth += 1 elif t['text'] in {')', ']'}: depth -= 1 elif t['text'] in {',', ';'} and depth == 0: break self.pos += 1 continue self.pos += 1 # 添加到变量列表 for var_name in var_names: # 检查是否在参数列表中 is_param = any(param['name'] == var_name for param in self.parameters) if not is_param and var_name not in self.macro_definitions: var_info = { 'type': var_type, 'name': var_name, 'line': current_line, 'is_struct': is_struct } if self.in_function_body: # 添加到局部变量 var_info['scope'] = 'local' self.local_vars.append(var_info) self.recorded_locals.add(var_name) else: # 添加到全局变量 var_info['scope'] = 'global' self.global_vars.append(var_info) self.recorded_globals.add(var_name) self.variable_declarations[var_name] = True def _handle_identifier_use(self, token): var_name = token['text'] line = token['line'] # 跳过已处理的标识符 skip_conditions = ( var_name in self.variable_declarations, var_name in self.macro_definitions, var_name == self.function_name, var_name in self.struct_tags # 跳过结构体标签 ) if any(skip_conditions): return # 函数体内使用的标识符 if self.in_function_body: # 如果未声明,则视为全局变量 if var_name not in self.recorded_locals and var_name not in self.recorded_globals: if var_name not in self.recorded_globals: self.global_vars.append({ 'name': var_name, 'line': line, 'scope': 'global' }) self.variable_declarations[var_name] = True self.recorded_globals.add(var_name) else: # 函数体外使用的标识符 if var_name not in self.recorded_globals: self.global_vars.append({ 'name': var_name, 'line': line, 'scope': 'global' }) self.variable_declarations[var_name] = True self.recorded_globals.add(var_name) def _handle_function_call(self): # 提取函数名 func_name = self.tokens[self.pos]['text'] line = self.current_line self.pos += 2 # 跳过函数名和 '(' # 提取参数 params = [] depth = 1 current_param = [] # 保存所有参数token用于后续分析 param_tokens = [] while self.pos < len(self.tokens) and depth > 0: token = self.tokens[self.pos] if token['text'] == '(': depth += 1 elif token['text'] == ')': depth -= 1 if depth == 0: break elif token['text'] == ',' and depth == 1: params.append(''.join([t['text'] for t in current_param]).strip()) param_tokens.extend(current_param) current_param = [] self.pos += 1 continue current_param.append(token) self.pos += 1 if current_param: params.append(''.join([t['text'] for t in current_param]).strip()) param_tokens.extend(current_param) # 跳过 ')' 如果还在范围内 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == ')': self.pos += 1 # 处理参数中的标识符 for token in param_tokens: if token['type'] == 'IDENTIFIER' and not token['text'].isupper(): self._handle_identifier_use(token) # 确定返回类型 return_type = "unknown" if func_name.startswith("vd_"): return_type = "void" elif func_name.startswith(("u1_", "u2_", "u4_", "s1_", "s2_", "s4_")): prefix = func_name.split("_")[0] return_type = prefix.upper() # 添加到函数调用列表 self.function_calls.append({ 'name': func_name, 'return_type': return_type, 'params': ", ".join(params), 'line': line }) class FunctionParserApp: def __init__(self, root): self.root = root self.root.title("C语言函数解析器") self.root.geometry("1000x800") self.root.configure(bg="#f0f0f0") self.setup_logging() # 创建样式 style = ttk.Style() style.configure("TFrame", background="#f0f0f0") style.configure("TLabelFrame", background="#f0f0f0", font=("Arial", 10, "bold")) style.configure("TButton", font=("Arial", 10), padding=5) style.configure("TCombobox", padding=5) style.configure("TProgressbar", thickness=10) # 主框架 main_frame = ttk.Frame(root) main_frame.pack(fill="both", expand=True, padx=15, pady=15) # 创建输入区域 input_frame = ttk.LabelFrame(main_frame, text="输入C语言函数体") input_frame.pack(fill="both", expand=True, padx=5, pady=5) self.input_text = scrolledtext.ScrolledText(input_frame, width=100, height=15, font=("Consolas", 11), bg="#ffffff") self.input_text.pack(fill="both", expand=True, padx=10, pady=10) # 按钮区域 btn_frame = ttk.Frame(main_frame) btn_frame.pack(fill="x", padx=5, pady=5) # 解析按钮 parse_btn = ttk.Button(btn_frame, text="解析函数", command=self.parse_function) parse_btn.pack(side="left", padx=5) # 保存日志按钮 save_log_btn = ttk.Button(btn_frame, text="保存日志", command=self.save_logs) save_log_btn.pack(side="right", padx=5) # 进度条 self.progress = ttk.Progressbar(btn_frame, orient="horizontal", length=300, mode="determinate") self.progress.pack(side="left", padx=10, fill="x", expand=True) # 调试级别控制 debug_frame = ttk.Frame(btn_frame) debug_frame.pack(side="left", padx=10) ttk.Label(debug_frame, text="调试级别:").pack(side="left") self.debug_level = tk.IntVar(value=1) ttk.Combobox(debug_frame, textvariable=self.debug_level, values=[1, 2], width=3).pack(side="left") # 示例按钮 example_btn = ttk.Button(btn_frame, text="加载示例", command=self.load_example) example_btn.pack(side="right", padx=5) # 创建输出区域 output_frame = ttk.LabelFrame(main_frame, text="解析结果") output_frame.pack(fill="both", expand=True, padx=5, pady=5) self.output_text = scrolledtext.ScrolledText(output_frame, width=100, height=15, font=("Consolas", 11), bg="#ffffff") self.output_text.pack(fill="both", expand=True, padx=10, pady=10) self.output_text.config(state=tk.DISABLED) # 日志区域 log_frame = ttk.LabelFrame(main_frame, text="日志信息") log_frame.pack(fill="both", expand=True, padx=5, pady=5) self.log_text = scrolledtext.ScrolledText(log_frame, width=100, height=6, font=("Consolas", 10), bg="#f8f8f8") self.log_text.pack(fill="both", expand=True, padx=10, pady=10) self.log_text.config(state=tk.DISABLED) # 示例函数体 self.example_code = """static void Diag21_PID_C9(U1 u1_a_num) { U1 u1_t_cmplt; U1 u1_t_cnt; struct SensorData sensor; if((U1)DIAG_CNT_ZERO == u1_t_swrstcnt) /* Determine if a software reset is in progress */ { for(u1_t_cnt = (U1)DIAG21_ZERO; u1_t_cnt < (U1)DIAG21_PIDC9_FLAG; u1_t_cnt ++) { u1_t_cmplt = u1_g_InspSoftwareVersion(u4_g_cmd, &u4_g_data, (U1)TRUE); } vd_s_Diag21_U2ToU1(u2_g_buf, u1_g_data, (U1)DIAG21_PIDC9_FLAG); } else { /* Do Nothing */ } }""" # 加载示例 self.load_example() def setup_logging(self): """配置日志系统""" self.log_filename = f"parser_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" # 创建文件处理器 file_handler = logging.FileHandler(self.log_filename, encoding='utf-8') file_handler.setLevel(logging.INFO) file_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) # 配置根日志器 root_logger = logging.getLogger() root_logger.setLevel(logging.INFO) root_logger.addHandler(file_handler) def log_to_gui(self, message, level="info"): """将日志信息显示在GUI中""" try: self.log_text.config(state=tk.NORMAL) timestamp = datetime.now().strftime("%H:%M:%S") self.log_text.insert(tk.END, f"[{timestamp}] {message}\n") self.log_text.see(tk.END) self.log_text.config(state=tk.DISABLED) if level == "info": logging.info(message) elif level == "warning": logging.warning(message) elif level == "error": logging.error(message) except Exception as e: logging.error(f"GUI日志错误: {str(e)}") def save_logs(self): """保存日志到文件""" try: log_content = self.log_text.get("1.0", tk.END) filename = f"saved_log_{datetime.now().strftime('%H%M%S')}.txt" with open(filename, "w", encoding='utf-8') as f: f.write(log_content) self.log_to_gui(f"日志已保存到: {filename}", "info") messagebox.showinfo("保存成功", f"日志已保存到:\n{filename}") except Exception as e: self.log_to_gui(f"保存日志失败: {str(e)}", "error") messagebox.showerror("保存失败", f"无法保存日志:\n{str(e)}") def update_progress(self, value): """更新进度条""" self.progress['value'] = value self.root.update_idletasks() def load_example(self): """加载示例函数体""" self.input_text.delete(1.0, tk.END) self.input_text.insert(tk.END, self.example_code) self.log_to_gui("已加载示例函数体") def parse_function(self): """使用内置解析器解析C语言函数体""" try: code = self.input_text.get(1.0, tk.END) if not code.strip(): self.log_to_gui("错误: 没有输入函数体", "error") messagebox.showerror("错误", "请输入要解析的C语言函数体") return self.log_to_gui("开始解析函数体...") self.output_text.config(state=tk.NORMAL) self.output_text.delete(1.0, tk.END) self.update_progress(0) # 使用内置词法分析器 self.log_to_gui("执行词法分析...") lexer = SimpleCLexer() tokens = lexer.tokenize(code) self.update_progress(30) # 使用内置语法分析器 self.log_to_gui("执行语法分析...") analyzer = FunctionAnalyzer() analyzer.debug_level = self.debug_level.get() analyzer.analyze(tokens) # 显示结果 self.log_to_gui("生成解析报告...") self.display_results( analyzer.local_vars, analyzer.global_vars, analyzer.function_calls, analyzer.function_name, analyzer.parameters ) self.update_progress(100) self.output_text.config(state=tk.DISABLED) self.log_to_gui("解析完成!") messagebox.showinfo("完成", "函数体解析成功完成!") except Exception as e: self.log_to_gui(f"解析错误: {str(e)}", "error") self.log_to_gui(f"错误详情: {traceback.format_exc()}", "error") messagebox.showerror("解析错误", f"发生错误:\n{str(e)}") self.update_progress(0) def display_results(self, local_vars, global_vars, function_calls, func_name, func_params): """增强版结果显示,包含所有变量信息""" # 显示函数签名 self.output_text.insert(tk.END, "=== 函数签名 ===\n", "header") if func_name: self.output_text.insert(tk.END, f"函数名: {func_name}\n") if func_params: param_list = [] for param in func_params: param_list.append(f"{param['type']} {param['name']}") self.output_text.insert(tk.END, f"参数: {', '.join(param_list)}\n\n") else: self.output_text.insert(tk.END, "参数: 无\n\n") else: self.output_text.insert(tk.END, "警告: 无法识别函数签名\n\n") # 显示所有找到的变量 self.output_text.insert(tk.END, "=== 所有变量分析 ===\n", "header") self.output_text.insert(tk.END, "类型 | 名称 | 作用域 | 行号 | 类别\n", "subheader") self.output_text.insert(tk.END, "-" * 60 + "\n") # 显示参数 for param in func_params: self.output_text.insert(tk.END, f"参数 | {param['name']} | 参数 | {param['line']} | 基本类型\n") # 显示局部变量 for var in local_vars: category = "结构体" if var.get('is_struct', False) else "基本类型" self.output_text.insert(tk.END, f"变量 | {var['name']} | 局部 | {var['line']} | {category}\n") # 显示全局变量 for var in global_vars: # 检查是否是结构体变量 category = "结构体" if var.get('is_struct', False) else "基本类型" self.output_text.insert(tk.END, f"变量 | {var['name']} | 全局 | {var['line']} | {category}\n") # 显示函数调用 for func in function_calls: self.output_text.insert(tk.END, f"函数调用 | {func['name']} | 调用 | {func['line']} | 函数\n") self.output_text.insert(tk.END, "\n") # 显示局部变量 if local_vars: self.output_text.insert(tk.END, "=== 局部变量 ===\n", "header") # 基本类型局部变量 basic_locals = [v for v in local_vars if not v.get('is_struct', False)] if basic_locals: self.output_text.insert(tk.END, "基本类型变量:\n") for var in basic_locals: self.output_text.insert(tk.END, f"{var['type']} {var['name']} (行号: {var['line']})\n") # 结构体局部变量 struct_locals = [v for v in local_vars if v.get('is_struct', False)] if struct_locals: self.output_text.insert(tk.END, "\n结构体变量:\n") for var in struct_locals: self.output_text.insert(tk.END, f"{var['type']} {var['name']} (行号: {var['line']})\n") else: self.output_text.insert(tk.END, "未找到局部变量\n\n") # 显示使用的全局变量 if global_vars: self.output_text.insert(tk.END, "=== 使用的全局变量 ===\n", "header") # 基本类型全局变量 basic_globals = [v for v in global_vars if not v.get('is_struct', False)] if basic_globals: self.output_text.insert(tk.END, "基本类型变量:\n") for var in basic_globals: self.output_text.insert(tk.END, f"{var['name']} (行号: {var['line']})\n") # 结构体全局变量 struct_globals = [v for v in global_vars if v.get('is_struct', False)] if struct_globals: self.output_text.insert(tk.END, "\n结构体变量:\n") for var in struct_globals: self.output_text.insert(tk.END, f"{var['name']} (行号: {var['line']})\n") self.output_text.insert(tk.END, "\n") else: self.output_text.insert(tk.END, "未使用全局变量\n\n") # 显示函数调用 if function_calls: self.output_text.insert(tk.END, "=== 函数调用 ===\n", "header") for func in function_calls: self.output_text.insert(tk.END, f"函数名: {func['name']} (行号: {func['line']})\n") self.output_text.insert(tk.END, f"返回类型: {func['return_type']}\n") self.output_text.insert(tk.END, f"参数: {func['params']}\n") self.output_text.insert(tk.END, "-" * 50 + "\n") else: self.output_text.insert(tk.END, "未调用任何函数\n\n") # 添加变量统计 self.output_text.insert(tk.END, "=== 解析统计 ===\n", "header") self.output_text.insert(tk.END, f"参数数量: {len(func_params)}\n") self.output_text.insert(tk.END, f"局部变量数量: {len(local_vars)}\n") self.output_text.insert(tk.END, f"全局变量数量: {len(global_vars)}\n") self.output_text.insert(tk.END, f"函数调用数量: {len(function_calls)}\n") # 结构体统计 struct_locals = [v for v in local_vars if v.get('is_struct', False)] struct_globals = [v for v in global_vars if v.get('is_struct', False)] self.output_text.insert(tk.END, f"结构体变量数量: {len(struct_locals) + len(struct_globals)}\n") self.output_text.insert(tk.END, f"总变量数量: {len(func_params) + len(local_vars) + len(global_vars) + len(function_calls)}\n") # 配置标签样式 self.output_text.tag_config("header", font=("Arial", 12, "bold"), foreground="#2c3e50") self.output_text.tag_config("subheader", font=("Arial", 10, "bold"), foreground="#34495e") if __name__ == "__main__": root = tk.Tk() app = FunctionParserApp(root) root.mainloop() === 函数签名 === 函数名: Diag21_PID_C9 参数: U1 u1_a_num === 所有变量分析 === 类型 | 名称 | 作用域 | 行号 | 类别 ------------------------------------------------------------ 参数 | u1_a_num | 参数 | 1 | 基本类型 变量 | u1_t_cmplt | 全局 | 3 | 基本类型 变量 | u1_t_cnt | 全局 | 4 | 基本类型 变量 | sensor | 全局 | 5 | 基本类型 变量 | u1_t_swrstcnt | 全局 | 7 | 基本类型 变量 | u4_g_cmd | 全局 | 11 | 基本类型 变量 | u4_g_data | 全局 | 11 | 基本类型 变量 | u2_g_buf | 全局 | 13 | 基本类型 变量 | u1_g_data | 全局 | 13 | 基本类型 函数调用 | u1_g_InspSoftwareVersion | 调用 | 11 | 函数 函数调用 | vd_s_Diag21_U2ToU1 | 调用 | 13 | 函数 未找到局部变量 === 使用的全局变量 === 基本类型变量: u1_t_cmplt (行号: 3) u1_t_cnt (行号: 4) sensor (行号: 5) u1_t_swrstcnt (行号: 7) u4_g_cmd (行号: 11) u4_g_data (行号: 11) u2_g_buf (行号: 13) u1_g_data (行号: 13) === 函数调用 === 函数名: u1_g_InspSoftwareVersion (行号: 11) 返回类型: U1 参数: u4_g_cmd, &u4_g_data, (U1)TRUE -------------------------------------------------- 函数名: vd_s_Diag21_U2ToU1 (行号: 13) 返回类型: void 参数: u2_g_buf, u1_g_data, (U1)DIAG21_PIDC9_FLAG -------------------------------------------------- === 解析统计 === 参数数量: 1 局部变量数量: 0 全局变量数量: 8 函数调用数量: 2 结构体变量数量: 0 总变量数量: 11 1、去除调试级别功能 2、去除保存日志功能 3、修改局部变量识别全局变量识别结构体识别数组识别等方式,先找出所有变量全部列出来,再找到函数体内的已经声明的局部变量,剩下的没有声明的就是全局变量。结构体只会在结构体内进行声明,而不会定义。
07-19
import tkinter as tk from tkinter import scrolledtext, ttk, messagebox import os import subprocess import sys import urllib.request import logging from datetime import datetime import tempfile import antlr4 from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker class SimpleCLexer(antlr4.Lexer): def __init__(self, input=None): super().__init__(input) self.tokens = [] self._interp = None def getAllTokens(self): if not self.tokens: self.tokens = self.tokenize(self._input) return self.tokens def tokenize(self, input_str): # 简化的词法分析器实现 tokens = [] pos = 0 line = 1 column = 0 # 定义C语言的关键词和类型 keywords = { 'void', 'int', 'char', 'float', 'double', 'short', 'long', 'signed', 'unsigned', 'struct', 'union', 'enum', 'typedef', 'static', 'extern', 'auto', 'register', 'const', 'volatile', 'return', 'if', 'else', 'switch', 'case', 'default', 'for', 'while', 'do', 'break', 'continue', 'goto', 'sizeof' } types = {'U1', 'U2', 'U4', 'S1', 'S2', 'S4'} while pos < len(input_str): # 跳过空白字符 if input_str[pos] in ' \t': pos += 1 column += 1 continue # 处理换行 if input_str[pos] == '\n': line += 1 column = 0 pos += 1 continue # 处理注释 if input_str.startswith('//', pos): end = input_str.find('\n', pos) if end == -1: end = len(input_str) pos = end continue if input_str.startswith('/*', pos): end = input_str.find('*/', pos) if end == -1: end = len(input_str) else: end += 2 pos = end continue # 处理标识符 if input_str[pos].isalpha() or input_str[pos] == '_': start = pos pos += 1 while pos < len(input_str) and (input_str[pos].isalnum() or input_str[pos] == '_'): pos += 1 token_text = input_str[start:pos] token_type = 'IDENTIFIER' # 检查是否为关键字或类型 if token_text in keywords: token_type = 'KEYWORD' elif token_text in types: token_type = 'TYPE' tokens.append({ 'type': token_type, 'text': token_text, 'line': line, 'column': column }) column += (pos - start) continue # 处理数字 if input_str[pos].isdigit(): start = pos pos += 1 while pos < len(input_str) and (input_str[pos].isdigit() or input_str[pos] in '.xXabcdefABCDEF'): pos += 1 tokens.append({ 'type': 'NUMBER', 'text': input_str[start:pos], 'line': line, 'column': column }) column += (pos - start) continue # 处理字符串 if input_str[pos] == '"': start = pos pos += 1 while pos < len(input_str) and input_str[pos] != '"': if input_str[pos] == '\\' and pos + 1 < len(input_str): pos += 2 else: pos += 1 if pos < len(input_str) and input_str[pos] == '"': pos += 1 tokens.append({ 'type': 'STRING', 'text': input_str[start:pos], 'line': line, 'column': column }) column += (pos - start) continue # 处理字符 if input_str[pos] == "'": start = pos pos += 1 while pos < len(input_str) and input_str[pos] != "'": if input_str[pos] == '\\' and pos + 1 < len(input_str): pos += 2 else: pos += 1 if pos < len(input_str) and input_str[pos] == "'": pos += 1 tokens.append({ 'type': 'CHAR', 'text': input_str[start:pos], 'line': line, 'column': column }) column += (pos - start) continue # 处理运算符和标点符号 operators = { '(', ')', '{', '}', '[', ']', ';', ',', '.', '->', '++', '--', '&', '*', '+', '-', '~', '!', '/', '%', '<<', '>>', '<', '>', '<=', '>=', '==', '!=', '^', '|', '&&', '||', '?', ':', '=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '&=', '^=', '|=', ',' } # 尝试匹配最长的运算符 matched = False for op_len in range(3, 0, -1): if pos + op_len <= len(input_str) and input_str[pos:pos+op_len] in operators: tokens.append({ 'type': 'OPERATOR', 'text': input_str[pos:pos+op_len], 'line': line, 'column': column }) pos += op_len column += op_len matched = True break if matched: continue # 无法识别的字符 tokens.append({ 'type': 'UNKNOWN', 'text': input_str[pos], 'line': line, 'column': column }) pos += 1 column += 1 return tokens class FunctionAnalyzer: def __init__(self): self.function_name = "" self.parameters = set() self.local_vars = [] self.global_vars = [] self.function_calls = [] self.current_function = None self.in_function = False self.in_global_scope = True self.variable_declarations = {} self.control_structures = {"if", "for", "while", "switch", "return", "else"} self.current_line = 0 self.macro_definitions = set() self.parameter_lines = {} self.type_aliases = {"U1", "U2", "U4", "S1", "S2", "S4"} self.in_function_body = False self.brace_depth = 0 self.recorded_globals = set() def analyze(self, tokens): self.tokens = tokens self.pos = 0 self.current_line = 0 # 第一步:识别宏定义(全大写标识符) self._identify_macros() # 第二步:分析函数体 while self.pos < len(self.tokens): token = self.tokens[self.pos] self.current_line = token['line'] # 检测函数定义 if token['text'] in {'void', 'int', 'char', 'float', 'double', 'U1', 'U2', 'U4', 'S1', 'S2', 'S4', 'static'}: if self._is_function_definition(): self._handle_function_definition() continue # 在函数体内检测变量声明 if self.in_function_body: if token['text'] in {'U1', 'U2', 'U4', 'S1', 'S2', 'S4', 'int', 'char', 'float', 'double'}: self._handle_variable_declaration() continue # 检测函数调用 if token['type'] == 'IDENTIFIER' and self.pos + 1 < len(self.tokens): next_token = self.tokens[self.pos + 1] if next_token['text'] == '(': self._handle_function_call() continue # 检测变量使用 if token['type'] == 'IDENTIFIER': self._handle_identifier_use(token) # 跟踪大括号深度 if token['text'] == '{': self.brace_depth += 1 if self.in_function and self.brace_depth == 1: self.in_function_body = True elif token['text'] == '}': self.brace_depth -= 1 if self.brace_depth == 0 and self.in_function: self.in_function = False self.in_function_body = False self.pos += 1 return self def _identify_macros(self): """识别宏定义(全大写标识符)""" for token in self.tokens: if token['type'] == 'IDENTIFIER' and token['text'].isupper(): self.macro_definitions.add(token['text']) def _is_function_definition(self): # 检查是否是函数定义 pos = self.pos depth = 0 found_name = False # 跳过可能的static修饰符 if self.tokens[pos]['text'] == 'static': pos += 1 # 检查返回类型 if self.tokens[pos]['text'] not in {'void', 'int', 'char', 'float', 'double', 'U1', 'U2', 'U4', 'S1', 'S2', 'S4'}: return False pos += 1 # 检查函数名 if pos < len(self.tokens) and self.tokens[pos]['type'] == 'IDENTIFIER': found_name = True pos += 1 else: return False # 检查左括号 if pos < len(self.tokens) and self.tokens[pos]['text'] == '(': depth = 1 pos += 1 else: return False # 检查参数列表和右括号 while pos < len(self.tokens) and depth > 0: token = self.tokens[pos] if token['text'] == '(': depth += 1 elif token['text'] == ')': depth -= 1 if depth == 0: # 检查后面是否是 { if pos + 1 < len(self.tokens) and self.tokens[pos + 1]['text'] == '{': return True return False pos += 1 return False def _handle_function_definition(self): # 跳过可能的static修饰符 if self.tokens[self.pos]['text'] == 'static': self.pos += 1 # 提取返回类型 return_type = self.tokens[self.pos]['text'] self.pos += 1 # 获取函数名 if self.pos < len(self.tokens) and self.tokens[self.pos]['type'] == 'IDENTIFIER': func_name = self.tokens[self.pos]['text'] self.pos += 1 # 跳过左括号 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '(': self.pos += 1 depth = 1 current_param = [] params = [] # 提取参数 while self.pos < len(self.tokens) and depth > 0: token = self.tokens[self.pos] if token['text'] == '(': depth += 1 elif token['text'] == ')': depth -= 1 if depth == 0: break elif token['text'] == ',' and depth == 1: # 参数结束 if current_param: # 提取参数名(最后一个标识符) param_name = None for t in reversed(current_param): if t['type'] == 'IDENTIFIER': param_name = t['text'] break if param_name: params.append(param_name) self.parameter_lines[param_name] = token['line'] current_param = [] self.pos += 1 continue current_param.append(token) self.pos += 1 # 处理最后一个参数 if current_param: param_name = None for t in reversed(current_param): if t['type'] == 'IDENTIFIER': param_name = t['text'] break if param_name: params.append(param_name) self.parameter_lines[param_name] = token['line'] # 记录参数 self.parameters = set(params) # 将参数视为已声明的变量 for param in params: self.variable_declarations[param] = True # 进入函数体 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == '{': self.in_function = True self.in_function_body = False # 将在遇到第一个{时设置为True self.brace_depth = 0 self.current_function = func_name self.function_name = func_name self.pos += 1 def _handle_variable_declaration(self): # 提取类型 var_type = self.tokens[self.pos]['text'] self.pos += 1 # 获取变量名 var_names = [] while self.pos < len(self.tokens): token = self.tokens[self.pos] # 标识符 - 变量名 if token['type'] == 'IDENTIFIER': var_names.append(token['text']) self.pos += 1 # 逗号 - 多个变量声明 elif token['text'] == ',': self.pos += 1 continue # 结束声明 elif token['text'] == ';': self.pos += 1 break # 赋值 - 跳过赋值表达式 elif token['text'] == '=': self.pos += 1 # 跳过整个赋值表达式 depth = 0 while self.pos < len(self.tokens): t = self.tokens[self.pos] if t['text'] in {'(', '['}: depth += 1 elif t['text'] in {')', ']'}: depth -= 1 elif t['text'] in {',', ';'} and depth == 0: break self.pos += 1 # 数组声明 - 跳过数组大小 elif token['text'] == '[': self.pos += 1 depth = 1 while self.pos < len(self.tokens) and depth > 0: t = self.tokens[self.pos] if t['text'] == '[': depth += 1 elif t['text'] == ']': depth -= 1 self.pos += 1 else: self.pos += 1 # 添加到局部变量 for var_name in var_names: self.local_vars.append({ 'type': var_type, 'name': var_name, 'line': self.current_line }) self.variable_declarations[var_name] = True def _handle_function_call(self): # 提取函数名 func_name = self.tokens[self.pos]['text'] line = self.current_line self.pos += 2 # 跳过函数名和 '(' # 提取参数 params = [] depth = 1 current_param = [] while self.pos < len(self.tokens) and depth > 0: token = self.tokens[self.pos] if token['text'] == '(': depth += 1 elif token['text'] == ')': depth -= 1 if depth == 0: break elif token['text'] == ',' and depth == 1: params.append(''.join([t['text'] for t in current_param]).strip()) current_param = [] self.pos += 1 continue current_param.append(token) self.pos += 1 if current_param: params.append(''.join([t['text'] for t in current_param]).strip()) # 跳过 ')' 如果还在范围内 if self.pos < len(self.tokens) and self.tokens[self.pos]['text'] == ')': self.pos += 1 # 确定返回类型 return_type = "unknown" if func_name.startswith("vd_"): return_type = "void" elif func_name.startswith(("u1_", "u2_", "u4_", "s1_", "s2_", "s4_")): prefix = func_name.split("_")[0] return_type = prefix.upper() # 添加到函数调用列表 self.function_calls.append({ 'name': func_name, 'return_type': return_type, 'type': "function", 'params': ", ".join(params), 'line': line }) def _handle_identifier_use(self, token): var_name = token['text'] line = token['line'] # 跳过宏定义(全大写) if var_name in self.macro_definitions: return # 跳过已声明的变量(局部变量) if var_name in self.variable_declarations: return # 跳过参数 if var_name in self.parameters: return # 跳过函数调用(函数名已经在函数调用处理中记录) for call in self.function_calls: if call['name'] == var_name: return # 跳过控制结构 if var_name in self.control_structures: return # 跳过类型别名 if var_name in self.type_aliases: return # 跳过已经记录过的全局变量 if var_name in self.recorded_globals: return # 添加到全局变量 self.global_vars.append({ 'name': var_name, 'line': line }) self.recorded_globals.add(var_name) class FunctionParserApp: def __init__(self, root): self.root = root self.root.title("C语言函数解析器 (内置解析器版)") self.root.geometry("900x700") self.setup_logging() # 创建输入区域 input_frame = tk.LabelFrame(root, text="输入C语言函数体", padx=5, pady=5) input_frame.pack(fill="both", expand=True, padx=10, pady=5) self.input_text = scrolledtext.ScrolledText(input_frame, width=100, height=20) self.input_text.pack(fill="both", expand=True, padx=5, pady=5) # 按钮区域 btn_frame = tk.Frame(root) btn_frame.pack(fill="x", padx=10, pady=5) # 解析按钮 parse_btn = tk.Button(btn_frame, text="解析函数", command=self.parse_function, bg="#4CAF50", fg="white") parse_btn.pack(side="left", padx=5) # 保存日志按钮 save_log_btn = tk.Button(btn_frame, text="保存日志", command=self.save_logs) save_log_btn.pack(side="right", padx=5) # 进度条 self.progress = ttk.Progressbar(btn_frame, orient="horizontal", length=300, mode="determinate") self.progress.pack(side="left", padx=10, fill="x", expand=True) # 创建输出区域 output_frame = tk.LabelFrame(root, text="解析结果", padx=5, pady=5) output_frame.pack(fill="both", expand=True, padx=10, pady=5) self.output_text = scrolledtext.ScrolledText(output_frame, width=100, height=15) self.output_text.pack(fill="both", expand=True, padx=5, pady=5) self.output_text.config(state=tk.DISABLED) # 日志区域 log_frame = tk.LabelFrame(root, text="日志信息", padx=5, pady=5) log_frame.pack(fill="both", expand=True, padx=10, pady=5) self.log_text = scrolledtext.ScrolledText(log_frame, width=100, height=8) self.log_text.pack(fill="both", expand=True, padx=5, pady=5) self.log_text.config(state=tk.DISABLED) # 添加示例按钮 example_btn = tk.Button(btn_frame, text="加载示例", command=self.load_example) example_btn.pack(side="right", padx=5) # 示例函数体 self.example_code = """static void Diag21_PID_C9(U1 u1_a_num) { U1 u1_t_cmplt; U1 u1_t_cnt; if((U1)DIAG_CNT_ZERO == u1_t_swrstcnt) /* Determine if a software reset is in progress */ { for(u1_t_cnt = (U1)DIAG21_ZERO; u1_t_cnt < (U1)DIAG21_PIDC9_FLAG; u1_t_cnt ++) { u1_t_cmplt = u1_g_InspSoftwareVersion(u4_g_cmd, &u4_g_data, (U1)TRUE); } vd_s_Diag21_U2ToU1(u2_g_buf, u1_g_data, (U1)DIAG21_PIDC9_FLAG); } else { /* Do Nothing */ } }""" def setup_logging(self): """配置日志系统""" self.log_filename = f"parser_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" logging.basicConfig( filename=self.log_filename, level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" ) logging.info("应用程序启动") def log_to_gui(self, message, level="info"): """将日志信息显示在GUI中""" self.log_text.config(state=tk.NORMAL) timestamp = datetime.now().strftime("%H:%M:%S") self.log_text.insert(tk.END, f"[{timestamp}] {message}\n") self.log_text.see(tk.END) self.log_text.config(state=tk.DISABLED) if level == "info": logging.info(message) elif level == "warning": logging.warning(message) elif level == "error": logging.error(message) def save_logs(self): """保存日志到文件""" try: log_content = self.log_text.get("1.0", tk.END) filename = f"saved_log_{datetime.now().strftime('%H%M%S')}.txt" with open(filename, "w") as f: f.write(log_content) self.log_to_gui(f"日志已保存到: {filename}", "info") messagebox.showinfo("保存成功", f"日志已保存到:\n{filename}") except Exception as e: self.log_to_gui(f"保存日志失败: {str(e)}", "error") messagebox.showerror("保存失败", f"无法保存日志:\n{str(e)}") def update_progress(self, value): """更新进度条""" self.progress['value'] = value self.root.update_idletasks() def load_example(self): """加载示例函数体""" self.input_text.delete(1.0, tk.END) self.input_text.insert(tk.END, self.example_code) self.log_to_gui("已加载示例函数体") def parse_function(self): """使用内置解析器解析C语言函数体""" try: code = self.input_text.get(1.0, tk.END) if not code.strip(): self.log_to_gui("错误: 没有输入函数体", "error") messagebox.showerror("错误", "请输入要解析的C语言函数体") return self.log_to_gui("开始解析函数体...") self.output_text.config(state=tk.NORMAL) self.output_text.delete(1.0, tk.END) self.update_progress(0) # 使用内置词法分析器 self.log_to_gui("执行词法分析...") lexer = SimpleCLexer() tokens = lexer.tokenize(code) self.update_progress(30) # 使用内置语法分析器 self.log_to_gui("执行语法分析...") analyzer = FunctionAnalyzer() analyzer.analyze(tokens) self.update_progress(70) # 显示结果 self.log_to_gui("生成解析报告...") self.display_results( analyzer.local_vars, analyzer.global_vars, analyzer.function_calls, analyzer.function_name, analyzer.parameters ) self.update_progress(100) self.output_text.config(state=tk.DISABLED) self.log_to_gui("解析完成!") messagebox.showinfo("完成", "函数体解析成功完成!") except Exception as e: self.log_to_gui(f"解析错误: {str(e)}", "error") messagebox.showerror("解析错误", f"发生错误:\n{str(e)}") self.update_progress(0) def display_results(self, local_vars, global_vars, function_calls, func_name, func_params): """显示解析结果""" # 显示函数签名 if func_name: self.output_text.insert(tk.END, "=== 函数签名 ===\n") self.output_text.insert(tk.END, f"函数名: {func_name}\n") self.output_text.insert(tk.END, f"参数: {', '.join(func_params)}\n\n") else: self.output_text.insert(tk.END, "=== 函数签名 ===\n") self.output_text.insert(tk.END, "警告: 无法识别函数签名\n\n") self.log_to_gui("无法识别函数签名", "warning") # 显示局部变量 if local_vars: self.output_text.insert(tk.END, "=== 局部变量 ===\n") for var in local_vars: self.output_text.insert(tk.END, f"{var['type']} {var['name']} (行号: {var['line']})\n") self.log_to_gui(f"找到局部变量: {var['type']} {var['name']}") self.output_text.insert(tk.END, "\n") else: self.output_text.insert(tk.END, "未找到局部变量\n\n") self.log_to_gui("未找到局部变量", "warning") # 显示使用的全局变量 if global_vars: self.output_text.insert(tk.END, "=== 使用的全局变量 ===\n") for var in global_vars: self.output_text.insert(tk.END, f"{var['name']} (行号: {var['line']})\n") self.log_to_gui(f"找到全局变量: {var['name']}") self.output_text.insert(tk.END, "\n") else: self.output_text.insert(tk.END, "未使用全局变量\n\n") self.log_to_gui("未使用全局变量", "warning") # 显示函数调用 if function_calls: self.output_text.insert(tk.END, "=== 函数调用 ===\n") for func in function_calls: self.output_text.insert(tk.END, f"函数名: {func['name']} (行号: {func['line']})\n") self.output_text.insert(tk.END, f"返回类型: {func['return_type']}\n") self.output_text.insert(tk.END, f"函数类型: {func['type']}\n") self.output_text.insert(tk.END, f"参数: {func['params']}\n") self.output_text.insert(tk.END, "-" * 50 + "\n") self.log_to_gui(f"找到函数调用: {func['name']}") else: self.output_text.insert(tk.END, "未调用任何函数\n\n") self.log_to_gui("未调用任何函数", "warning") # 显示统计信息 self.output_text.insert(tk.END, "=== 解析统计 ===\n") self.output_text.insert(tk.END, f"局部变量数量: {len(local_vars)}\n") self.output_text.insert(tk.END, f"使用的全局变量数量: {len(global_vars)}\n") self.output_text.insert(tk.END, f"函数调用数量: {len(function_calls)}\n") self.output_text.insert(tk.END, "\n") if __name__ == "__main__": root = tk.Tk() app = FunctionParserApp(root) root.mainloop() === 函数签名 === 警告: 无法识别函数签名 未找到局部变量 === 使用的全局变量 === u1_t_cmplt (行号: 4) u1_t_cnt (行号: 5) u1_t_swrstcnt (行号: 7) === 函数调用 === 函数名: u1_g_InspSoftwareVersion (行号: 11) 返回类型: U1 函数类型: function 参数: u4_g_cmd, &u4_g_data, (U1)TRUE 函数名: vd_s_Diag21_U2ToU1 (行号: 13) 返回类型: void 函数类型: function 参数: u2_g_buf, u1_g_data, (U1)DIAG21_PIDC9_FLAG === 解析统计 === 局部变量数量: 0 使用的全局变量数量: 3 函数调用数量: 2 请修改上述问题,并且将每一步的解析结果都记录到日志,我会将记录的日志发给你,你找到原因并修改
07-18
import numpy as np import matplotlib.pyplot as plt from pymatgen.io.vasp import Vasprun from pymatgen.core.structure import Structure from scipy.spatial import cKDTree from tqdm import tqdm import matplotlib as mpl import warnings import os import csv import argparse import multiprocessing import time import sys 忽略可能的警告 warnings.filterwarnings(“ignore”, category=UserWarning) 专业绘图设置 plt.style.use(‘seaborn-v0_8-whitegrid’) mpl.rcParams.update({ ‘font.family’: ‘serif’, ‘font.serif’: [‘Times New Roman’, ‘DejaVu Serif’], ‘font.size’: 12, ‘axes.labelsize’: 14, ‘axes.titlesize’: 16, ‘xtick.labelsize’: 12, ‘ytick.labelsize’: 12, ‘figure.dpi’: 600, ‘savefig.dpi’: 600, ‘figure.figsize’: (8, 6), ‘lines.linewidth’: 2.0, ‘legend.fontsize’: 10, ‘legend.framealpha’: 0.8, ‘mathtext.default’: ‘regular’, ‘axes.linewidth’: 1.5, ‘xtick.major.width’: 1.5, ‘ytick.major.width’: 1.5, ‘xtick.major.size’: 5, ‘ytick.major.size’: 5, }) def identify_atom_types(struct): “”“原子类型识别函数”“” # 初始化数据结构 atom_types = { “phosphate_oxygens”: {“P-O/P=O”: [], “P-OH”: []}, “phosphate_hydrogens”: [], “water_oxygens”: [], “water_hydrogens”: [], “hydronium_oxygens”: [], “hydronium_hydrogens”: [], “fluoride_atoms”: [i for i, site in enumerate(struct) if site.species_string == “F”], “aluminum_atoms”: [i for i, site in enumerate(struct) if site.species_string == “Al”] } # 构建全局KDTree all_coords = np.array([site.coords for site in struct]) kdtree = cKDTree(all_coords, boxsize=struct.lattice.abc) # 识别磷酸基团 p_atoms = [i for i, site in enumerate(struct) if site.species_string == "P"] phosphate_oxygens = [] for p_idx in p_atoms: # 查找P周围的O原子 (距离 < 1.6Å) neighbors = kdtree.query_ball_point(all_coords[p_idx], r=1.6) p_o_indices = [idx for idx in neighbors if idx != p_idx and struct[idx].species_string == "O"] phosphate_oxygens.extend(p_o_indices) # 识别所有H原子并确定归属 hydrogen_owners = {} h_atoms = [i for i, site in enumerate(struct) if site.species_string == "H"] for h_idx in h_atoms: neighbors = kdtree.query_ball_point(all_coords[h_idx], r=1.2) candidate_os = [idx for idx in neighbors if idx != h_idx and struct[idx].species_string == "O"] if not candidate_os: continue min_dist = float('inf') owner_o = None for o_idx in candidate_os: dist = struct.get_distance(h_idx, o_idx) if dist < min_dist: min_dist = dist owner_o = o_idx hydrogen_owners[h_idx] = owner_o # 分类磷酸氧:带H的为P-OH,不带H的为P-O/P=O for o_idx in phosphate_oxygens: has_hydrogen = any(owner_o == o_idx for h_idx, owner_o in hydrogen_owners.items()) if has_hydrogen: atom_types["phosphate_oxygens"]["P-OH"].append(o_idx) else: atom_types["phosphate_oxygens"]["P-O/P=O"].append(o_idx) # 识别水和水合氢离子 all_o_indices = [i for i, site in enumerate(struct) if site.species_string == "O"] non_phosphate_os = [o_idx for o_idx in all_o_indices if o_idx not in phosphate_oxygens] o_h_count = {} for h_idx, owner_o in hydrogen_owners.items(): o_h_count[owner_o] = o_h_count.get(owner_o, 0) + 1 for o_idx in non_phosphate_os: h_count = o_h_count.get(o_idx, 0) attached_hs = [h_idx for h_idx, owner_o in hydrogen_owners.items() if owner_o == o_idx] if h_count == 2: atom_types["water_oxygens"].append(o_idx) atom_types["water_hydrogens"].extend(attached_hs) elif h_count == 3: atom_types["hydronium_oxygens"].append(o_idx) atom_types["hydronium_hydrogens"].extend(attached_hs) # 识别磷酸基团的H原子 for o_idx in atom_types["phosphate_oxygens"]["P-OH"]: attached_hs = [h_idx for h_idx, owner_o in hydrogen_owners.items() if owner_o == o_idx] atom_types["phosphate_hydrogens"].extend(attached_hs) return atom_types def get_hbond_config(): “”“返回氢键配置,包含距离和角度阈值”“” return [ { “name”: “P-O/P=O···Hw”, “donor_type”: “water_oxygens”, “acceptor_type”: “P-O/P=O”, “h_type”: “water_hydrogens”, “distance_threshold”: 2.375, “angle_threshold”: 143.99, “color”: “#1f77b4” }, { “name”: “P-O/P=O···Hh”, “donor_type”: “hydronium_oxygens”, “acceptor_type”: “P-O/P=O”, “h_type”: “hydronium_hydrogens”, “distance_threshold”: 2.275, “angle_threshold”: 157.82, “color”: “#ff7f0e” }, { “name”: “P-O/P=O···Hp”, “donor_type”: “P-OH”, “acceptor_type”: “P-O/P=O”, “h_type”: “phosphate_hydrogens”, “distance_threshold”: 2.175, “angle_threshold”: 155.00, “color”: “#2ca02c” }, { “name”: “P-OH···Ow”, “donor_type”: “P-OH”, “acceptor_type”: “water_oxygens”, “h_type”: “phosphate_hydrogens”, “distance_threshold”: 2.275, “angle_threshold”: 155.13, “color”: “#d62728” }, { “name”: “Hw···Ow”, “donor_type”: “water_oxygens”, “acceptor_type”: “water_oxygens”, “h_type”: “water_hydrogens”, “distance_threshold”: 2.375, “angle_threshold”: 138.73, “color”: “#9467bd” }, { “name”: “Hh···Ow”, “donor_type”: “hydronium_oxygens”, “acceptor_type”: “water_oxygens”, “h_type”: “hydronium_hydrogens”, “distance_threshold”: 2.225, “angle_threshold”: 155.31, “color”: “#8c564b” }, { “name”: “Hw···F”, “donor_type”: “water_oxygens”, “acceptor_type”: “fluoride_atoms”, “h_type”: “water_hydrogens”, “distance_threshold”: 2.225, “angle_threshold”: 137.68, “color”: “#e377c2” }, { “name”: “Hh···F”, “donor_type”: “hydronium_oxygens”, “acceptor_type”: “fluoride_atoms”, “h_type”: “hydronium_hydrogens”, “distance_threshold”: 2.175, “angle_threshold”: 154.64, “color”: “#7f7f7f” }, { “name”: “Hp···F”, “donor_type”: “P-OH”, “acceptor_type”: “fluoride_atoms”, “h_type”: “phosphate_hydrogens”, “distance_threshold”: 2.275, “angle_threshold”: 153.71, “color”: “#bcbd22” } ] def calculate_angle(struct, donor_idx, h_idx, acceptor_idx): “”“计算D-H···A键角 (角度制),使用笛卡尔向量表示并处理周期性”“” # 获取分数坐标 frac_coords = struct.frac_coords lattice = struct.lattice # 获取氢原子H的分数坐标 h_frac = frac_coords[h_idx] # 计算供体D相对于H的分数坐标差 d_frac = frac_coords[donor_idx] dh_frac = d_frac - h_frac # 计算受体A相对于H的分数坐标差 a_frac = frac_coords[acceptor_idx] ah_frac = a_frac - h_frac # 应用周期性修正 (将分数坐标差限制在[-0.5, 0.5]范围内) dh_frac = np.where(dh_frac > 0.5, dh_frac - 1, dh_frac) dh_frac = np.where(dh_frac < -0.5, dh_frac + 1, dh_frac) ah_frac = np.where(ah_frac > 0.5, ah_frac - 1, ah_frac) ah_frac = np.where(ah_frac < -0.5, ah_frac + 1, ah_frac) # 转换为笛卡尔向量 (H->D 和 H->A) vec_hd = np.dot(dh_frac, lattice.matrix) # H->D向量 vec_ha = np.dot(ah_frac, lattice.matrix) # H->A向量 # 计算向量点积 dot_product = np.dot(vec_hd, vec_ha) # 计算向量模长 norm_hd = np.linalg.norm(vec_hd) norm_ha = np.linalg.norm(vec_ha) # 避免除以零 if norm_hd < 1e-6 or norm_ha < 1e-6: return None # 计算余弦值 cos_theta = dot_product / (norm_hd * norm_ha) # 处理数值误差 cos_theta = np.clip(cos_theta, -1.0, 1.0) # 计算角度 (弧度转角度) angle_rad = np.arccos(cos_theta) angle_deg = np.degrees(angle_rad) return angle_deg def calculate_hbond_lengths_frame(struct, atom_types, hbond_config, bond_threshold=1.3): “”“计算单帧结构中氢键键长(H···A距离)”“” # 构建全局KDTree用于快速搜索 all_coords = np.array([site.coords for site in struct]) lattice_abc = struct.lattice.abc kdtree = cKDTree(all_coords, boxsize=lattice_abc) # 结果字典: {氢键类型: [键长列表]} length_results = {hbond["name"]: [] for hbond in hbond_config} # 处理每一类氢键 for hbond in hbond_config: # 获取供体原子列表 if hbond["donor_type"] == "P-OH": donors = atom_types["phosphate_oxygens"]["P-OH"] else: donors = atom_types[hbond["donor_type"]] # 获取受体原子列表 if hbond["acceptor_type"] == "P-O/P=O": acceptors = atom_types["phosphate_oxygens"]["P-O/P=O"] elif hbond["acceptor_type"] == "P-OH": acceptors = atom_types["phosphate_oxygens"]["P-OH"] else: acceptors = atom_types[hbond["acceptor_type"]] # 获取氢原子列表 hydrogens = atom_types[hbond["h_type"]] # 如果没有氢原子或受体,跳过 if len(hydrogens) == 0 or len(acceptors) == 0: continue # 为受体构建KDTree(使用全局坐标) acceptor_coords = all_coords[acceptors] acceptor_kdtree = cKDTree(acceptor_coords, boxsize=lattice_abc) # 遍历所有氢原子 for h_idx in hydrogens: h_coords = all_coords[h_idx] # 查找与H成键的供体 (距离 < bond_threshold) donor_neighbors = kdtree.query_ball_point(h_coords, r=bond_threshold) donor_candidates = [idx for idx in donor_neighbors if idx in donors] # 如果没有找到供体,跳过 if not donor_candidates: continue # 选择最近的供体 min_dist = float('inf') donor_idx = None for d_idx in donor_candidates: dist = struct.get_distance(h_idx, d_idx) if dist < min_dist: min_dist = dist donor_idx = d_idx # 查找在距离阈值内的受体 acceptor_indices = acceptor_kdtree.query_ball_point(h_coords, r=hbond["distance_threshold"]) for a_idx_offset in acceptor_indices: a_idx = acceptors[a_idx_offset] # 排除供体自身 if a_idx == donor_idx: continue # 计算键长 (H···A距离) ha_distance = struct.get_distance(h_idx, a_idx) # 计算键角 angle = calculate_angle(struct, donor_idx, h_idx, a_idx) # 检查角度阈值 if angle is not None and angle >= hbond["angle_threshold"]: length_results[hbond["name"]].append(ha_distance) return length_results def calculate_hbond_lengths_frame_wrapper(args): “”“包装函数用于多进程处理”“” struct, hbond_config = args atom_types = identify_atom_types(struct) return calculate_hbond_lengths_frame(struct, atom_types, hbond_config) def calculate_hbond_lengths_parallel(structures, workers=1, step_interval=10): “”“并行计算氢键键长,每step_interval帧计算一次”“” hbond_config = get_hbond_config() all_results = [] # 只选择每step_interval帧的结构 selected_structures = structures[::step_interval] frame_indices = list(range(0, len(structures), step_interval)) # 准备参数列表 args_list = [(struct, hbond_config) for struct in selected_structures] # 如果没有可用的worker,则顺序执行 if workers == 1: results = [] for args in tqdm(args_list, desc="Calculating HBond Lengths"): results.append(calculate_hbond_lengths_frame_wrapper(args)) else: with multiprocessing.Pool(processes=workers) as pool: results = list(tqdm( pool.imap(calculate_hbond_lengths_frame_wrapper, args_list), total=len(selected_structures), desc="Calculating HBond Lengths" )) # 将结果与帧索引组合 for frame_idx, frame_result in zip(frame_indices, results): all_results.append({ "frame_idx": frame_idx, "results": frame_result }) return all_results def plot_hbond_length_time_series(all_results, system_name): “”“绘制氢键键长随时间变化的曲线并保存原始数据”“” # 创建输出目录 os.makedirs(“HBond_Length_Time_Series”, exist_ok=True) os.makedirs(“HBond_Length_Data”, exist_ok=True) hbond_config = get_hbond_config() # 创建统计信息汇总文件 - 使用'A'代替Å避免编码问题 summary_path = os.path.join("HBond_Length_Data", f"{system_name}_summary.csv") with open(summary_path, 'w', newline='', encoding='utf-8') as summary_file: summary_writer = csv.writer(summary_file) summary_writer.writerow(["HBond Type", "Mean Length (A)", "Std Dev (A)", "Median (A)", "Min (A)", "Max (A)"]) # 处理每种氢键类型 for hbond in hbond_config: hbond_name = hbond["name"] safe_name = hbond_name.replace("/", "").replace("=", "").replace("···", "_").replace(" ", "_") # 提取该氢键类型的所有帧的键长 frame_indices = [] all_lengths = [] for frame_data in all_results: frame_idx = frame_data["frame_idx"] lengths = frame_data["results"].get(hbond_name, []) if lengths: # 只记录有数据的帧 frame_indices.append(frame_idx) all_lengths.append(lengths) if not frame_indices: print(f"No hydrogen bonds found for {hbond_name} in {system_name}") continue # 计算每帧的统计量 mean_lengths = [np.mean(lengths) for lengths in all_lengths] median_lengths = [np.median(lengths) for lengths in all_lengths] std_lengths = [np.std(lengths) for lengths in all_lengths] # 计算全局统计量 all_flat_lengths = [length for sublist in all_lengths for length in sublist] global_mean = np.mean(all_flat_lengths) global_std = np.std(all_flat_lengths) global_median = np.median(all_flat_lengths) global_min = np.min(all_flat_lengths) global_max = np.max(all_flat_lengths) # 保存全局统计信息到汇总文件 with open(summary_path, 'a', newline='', encoding='utf-8') as summary_file: summary_writer = csv.writer(summary_file) summary_writer.writerow([ hbond_name, f"{global_mean:.4f}", f"{global_std:.4f}", f"{global_median:.4f}", f"{global_min:.4f}", f"{global_max:.4f}" ]) # ========== 保存原始时间序列数据 ========== data_path = os.path.join("HBond_Length_Data", f"{system_name}_{safe_name}_time_series.csv") with open(data_path, 'w', newline='', encoding='utf-8') as data_file: data_writer = csv.writer(data_file) data_writer.writerow(["Frame Index", "Mean Length (A)", "Median Length (A)", "Std Dev (A)"]) for i, frame_idx in enumerate(frame_indices): data_writer.writerow([ frame_idx, f"{mean_lengths[i]:.4f}", f"{median_lengths[i]:.4f}", f"{std_lengths[i]:.4f}" ]) # ========== 绘制时间序列图 ========== plt.figure(figsize=(8, 6)) # 符合期刊要求的尺寸 # 绘制平均键长曲线 plt.plot(frame_indices, mean_lengths, color=hbond["color"], label="Mean Length", linewidth=1.5) # 绘制中位数键长曲线 plt.plot(frame_indices, median_lengths, color=hbond["color"], linestyle="--", label="Median Length", linewidth=1.0) # 添加标准差误差带 plt.fill_between( frame_indices, np.array(mean_lengths) - np.array(std_lengths), np.array(mean_lengths) + np.array(std_lengths), color=hbond["color"], alpha=0.2, label=1 Std Dev" ) # 添加全局统计信息(放在右上角) stats_text = (f"Global Mean: {global_mean:.3f} $\mathrm{{\\AA}}$\n" f"Global Std: {global_std:.3f} $\mathrm{{\\AA}}$\n" f"Global Median: {global_median:.3f} $\mathrm{{\\AA}}$\n" f"Count: {len(all_flat_lengths)}") plt.text(0.95, 0.95, stats_text, transform=plt.gca().transAxes, verticalalignment='top', horizontalalignment='right', fontsize=10, bbox=dict(boxstyle='round', facecolor='white', alpha=0.8)) # 设置标题和标签 plt.title(f"{system_name}: {hbond_name} Bond Length", fontsize=14) # 精简标题 plt.xlabel("Time(fs)", fontsize=12) plt.ylabel(r"Bond Length ($\mathrm{\AA}$)", fontsize=12) # 使用LaTeX格式的Å符号 # 统一设置Y轴范围(1.0-2.4 Å) plt.ylim(1.0, 2.4) plt.xlim(0,4000) plt.grid(True, linestyle='--', alpha=0.5) # 将图例放在左上角 plt.legend(loc='upper left', fontsize=10, frameon=True, framealpha=0.8) # 调整布局 plt.tight_layout() # 保存图像(使用TIFF格式,期刊推荐) image_path = os.path.join("HBond_Length_Time_Series", f"{system_name}_{safe_name}_time_series.tiff") plt.savefig(image_path, dpi=600, format='tiff', bbox_inches='tight') plt.close() print(f"Saved HBond length time series data: {data_path}") print(f"Saved HBond length time series plot: {image_path}") print(f"Saved summary statistics: {summary_path}") def main(vasprun_files, workers=1, step_interval=10): “”“主处理函数”“” for system_name, vasprun_file in vasprun_files.items(): print(f"\n{‘=’*50}“) print(f"Processing {system_name}: {vasprun_file} with {workers} workers”) print(f"Step interval: {step_interval} frames") print(f"{‘=’*50}") start_time = time.time() try: # 加载VASP结果 vr = Vasprun(vasprun_file, ionic_step_skip=5) structures = vr.structures print(f"Loaded {len(structures)} frames") print(f"Lattice parameters: {structures[0].lattice.abc}") print(f"Periodic boundary handling: Fractional coordinates + PBC correction") # 计算氢键键长时间序列 print("Calculating hydrogen bond lengths over time...") hbond_lengths = calculate_hbond_lengths_parallel(structures, workers=workers, step_interval=step_interval) # 绘制并保存结果 plot_hbond_length_time_series(hbond_lengths, system_name) elapsed = time.time() - start_time print(f"\nCompleted processing for {system_name} in {elapsed:.2f} seconds") except Exception as e: print(f"Error processing {system_name}: {str(e)}", file=sys.stderr) import traceback traceback.print_exc() print("\nAll HBond length time series analysis completed successfully!") if name == “main”: # 设置命令行参数 parser = argparse.ArgumentParser(description=‘Calculate hydrogen bond lengths from VASP simulations’) parser.add_argument(‘–workers’, type=int, default=multiprocessing.cpu_count(), help=f’Number of parallel workers (default: {multiprocessing.cpu_count()})‘) parser.add_argument(’–step_interval’, type=int, default=10, help=‘Frame interval for analysis (default: 10)’) args = parser.parse_args() # 自动设置vasprun文件和系统名称 vasprun_files = { "System1": "vasprun1.xml", "System2": "vasprun2.xml", "System3": "vasprun3.xml", "System4": "vasprun4.xml" } # 检查文件是否存在 missing_files = [name for name, path in vasprun_files.items() if not os.path.exists(path)] if missing_files: raise FileNotFoundError(f"Missing vasprun files: {', '.join(missing_files)}") print(f"Starting HBond length analysis with {args.workers} workers...") main(vasprun_files, workers=args.workers, step_interval=args.step_interval)以上代码是从RDF到氢键角度的计算完整过程,其中的逻辑判断,首先是供体-H之间的化学键判定,然后再是H受体之间的氢键距离判定,最后是氢键角度的判定,由于目前氢键角度的判定暂定为120°,在这里我们需要延用一样的逻辑计算不同的内容,即计算氢键寿命,采用存活相关函数SCF计算,弛豫时间采用积分法,计算对象为LAMMPS数据,并只计算一个体系,导入的文件为lammpstrij和data文件,计算的步长为0.1fs,按照合适的要求,最好符合The Journal of Chemical Physics期刊要求,轨迹输出频率,相关函数计算窗口,以及时间原点间隔,由于LAMMPS体系较大,尽可能的提高CPU和内存利用,以计算速率为主,可以增大CPU和内存的占用率。其中对原子进行映射,具体主要为1: "H", 2: "O", 3: "F", 7: "P",H3PO4-23pure.data为data文件名,nvt-P2O5-353K-23.lammpstrj为lammpstrij的文件名,然后请输出完整代码,然后提供一个测试代码,对第一帧进行氢键的识别,确保无误之后再进行氢键寿命计算的调试,在计算过程中只需要计算氢键寿命,保留键长随时间变化的识别逻辑,不需要再算键长随时间的变化。data文件为 序号 原子类型 x y z类型,例如 Atoms # charge 1 7 17.470000000000 55.085000000000 14.227000000000 2 2 16.654000000000 54.249000000000 15.498000000000 3 2 16.582000000000 55.222000000000 12.750000000000 4 2 17.569000000000 56.681000000000 14.791000000000 5 1 16.331000000000 53.417000000000 15.086000000000 优化原子ID识别问题,最好能分步进行,命令中能够输入只算第一帧氢键识别,如果无误再接着后续默认参数计算氢键寿命,然后文件的地址都设为默认参数而不用在命令栏中输入文件名
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值