这是一个基于 Python 开发的 GUI 工具,旨在对 C 语言代码进行结构化分析。该系统能够将 C 源代码解析为树状结构(Abstract Syntax Tree 的简化版),支持代码预处理、语法检测(括号匹配)、可视化展示以及两份代码间的重复率(相似度)计算。
- 代码树构建:解析
main函数体,识别if、for、while、switch等控制流语句并构建层级树结构。 - 重复率计算:通过比较两棵代码树的结构,计算代码间的相似度,适用于简单的代码查重。
- 代码预处理:自动移除注释(单行及多行),并将非关键字标识符统一替换为
var以进行结构化对比。 - 语法检测:实时检测代码中的小括号
()和中括号[]是否匹配,并精确定位错误行号。 - 可视化展示:集成 Graphviz 库,将复杂的代码逻辑以直观的图形化树状结构展示。
- 自动备份:程序启动时会自动将当前的 Python 脚本备份至
code_backups文件夹。
| 文件名 | 描述 |
|---|---|
main.py |
程序入口,负责启动备份逻辑及 GUI 应用。 |
CodeTree.py |
核心逻辑类,包含代码预处理、树构建算法、表达式解析及括号检测。 |
CodeTreeApp.py |
基于 tkinter 的图形界面实现,包含操作面板、输出日志及图像显示。 |
util.py |
工具模块,提供带时间戳的脚本自动备份功能。 |
test.py |
测试脚本,用于验证代码解析逻辑的正确性。 |
-
Python 版本: 3.x
-
第三方库:
-
graphviz: 用于生成代码树图像。 -
anytree: 用于管理和操作树形数据结构。 -
外部依赖: 需在系统中安装 Graphviz 软件 并配置环境变量。
- 安装依赖库:
pip install -r requirements.txt- 运行主程序:
python main.py- 在操作面板中选择“输入代码”或使用“示例代码演示”查看效果。
This is a Python-based GUI tool designed for the structural analysis of C language code. The system parses C source code into a tree-like structure (a simplified AST), supporting code preprocessing, syntax detection (bracket matching), visual representation, and similarity (repetition rate) calculation between two code samples.
- Code Tree Construction: Parses the
mainfunction body to identify control flow statements likeif,for,while, andswitchto build a hierarchical tree. - Similarity Calculation: Compares the structures of two code trees to calculate similarity, useful for basic code plagiarism detection.
- Code Preprocessing: Automatically removes single-line and multi-line comments and normalizes identifiers (non-keywords) to
varfor structural comparison. - Syntax Detection: Real-time detection of mismatched parentheses
()and square brackets[], providing precise error line numbers. - Visualization: Integrates with Graphviz to display complex code logic as an intuitive graphical tree.
- Automatic Backup: Automatically backs up current Python scripts to a
code_backupsfolder upon program execution.
| File | Description |
|---|---|
main.py |
Entry point of the application; handles backup logic and starts the GUI. |
CodeTree.py |
Core logic class containing code preprocessing, tree-building algorithms, and bracket detection. |
CodeTreeApp.py |
GUI implementation using tkinter, featuring an operation panel, logs, and image displays. |
util.py |
Utility module providing timestamped script backup functionality. |
test.py |
Test script for validating code parsing logic. |
-
Python Version: 3.x
-
Libraries:
-
graphviz: For generating code tree images. -
anytree: For managing tree data structures. -
External Dependency: Must have Graphviz software installed and configured in your system's PATH.
- Install dependencies:
pip install -r requirements.txt- Run the application:
python main.py- Use the "Input Code" buttons or "Demo Example" in the panel to start analyzing code.