{"id":21503572,"url":"https://github.com/LIU42/SyntaxAnalyzer","last_synced_at":"2025-10-05T05:31:08.875Z","repository":{"id":250541955,"uuid":"833527598","full_name":"LIU42/SyntaxParser","owner":"LIU42","description":"《编译原理》课程设计，基于 LR (1) 分析的类 C 语言语法分析器","archived":false,"fork":false,"pushed_at":"2024-09-06T08:36:40.000Z","size":63,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-23T18:29:39.855Z","etag":null,"topics":["compiler","course-project","grammar-rules","homework-project","lr-parser","lr1-parser","syntax-analysis","syntax-analyzer","syntax-parser"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LIU42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-25T08:33:29.000Z","updated_at":"2024-09-06T08:36:43.000Z","dependencies_parsed_at":"2024-07-28T10:25:50.231Z","dependency_job_id":"68edbb77-3f15-4c59-aa69-00e84e38891f","html_url":"https://github.com/LIU42/SyntaxParser","commit_stats":null,"previous_names":["liu42/syntaxparser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FSyntaxParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FSyntaxParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FSyntaxParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FSyntaxParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LIU42","download_url":"https://codeload.github.com/LIU42/SyntaxParser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235365799,"owners_count":18978402,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","course-project","grammar-rules","homework-project","lr-parser","lr1-parser","syntax-analysis","syntax-analyzer","syntax-parser"],"created_at":"2024-11-23T18:24:13.847Z","updated_at":"2025-10-05T05:31:08.865Z","avatar_url":"https://github.com/LIU42.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SyntaxAnalyzer 语法分析器\n\n*\u003cu\u003ev1.1.0 新变化：优化项目结构，同步上传语法分析器以及配套的 [词法分析器](https://github.com/LIU42/LexicalAnalyzer) 。\u003c/u\u003e*\n\n## 项目简介\n\n本项目为《编译原理》课程设计，基于 LR (1) 的类 C 语言语法分析器，可以实现针对一种类似 C 语言程序的 Token 序列（由 [词法分析器](https://github.com/LIU42/LexicalAnalyzer) 生成）进行语法分析，给出合法判断、出错位置及大致原因。\n\n本项目提供的默认文法支持除了：\n\n- 复杂的指针类型（如函数指针，行指针等）\n\n- 关键字 typedef 及相关的类型定义\n\n- 编译预处理指令\n\n以外大部分的 C 语言语法规则。此外还有一些额外的关键字，详见 [词法分析器](https://github.com/LIU42/LexicalParser) 中的介绍。\n\n## 实现方案\n\n### 工作流程\n\n整个项目的工作流程如下：\n\n1. 首先读入文法配置文件（configs/grammar.json）中描述语法规则的带有拓广产生式的 2 型文法，并根据该文法利用项目集规范族法构造识别活前缀的有穷自动机（状态转换表）。\n\n2. 其次根据状态转换表，判断项目类型，逐一添加 ACTION 表项和 GOTO 表项，分别构造 ACTION 表和 GOTO 表。\n\n3. 最后根据 ACTION 表和 GOTO 表，以及错误信息配置文件（configs/message.json）利用 LR (1) 分析流程，对输入的 Token 序列进行语法分析和错误处理。\n\n当检测到语法错误时，采用恐慌模式（Panic）错误恢复策略，即不断丢弃下一个 Token，直到找到一个能够进行正常分析的 Token 继续分析。\n\n### 伪代码描述\n\n伪代码仅表示程序的处理逻辑，与真正的实现不完全一致。\n\n```c\n创建状态栈和符号栈;\n\nwhile (指针未到达末尾 \u0026\u0026 分析结束标志为假) {\n    根据指针位置取出待分析的 Token;\n    查询 ACTION 表获取该 Token 对应的操作;\n\n    if (无对应操作) {\n        ACTION 出错处理程序;\n    } else if (为接受操作) {\n        分析结束标志设为真;\n    } else if (为移进操作) {\n        当前 Token 压入符号栈;\n        对应 ACTION 表值压入状态栈;\n        指针后移一位;\n    } else if (为归约操作) {\n        从符号栈中弹出指定数量的符号进行归约，将归约后的符号重新压入栈中;\n        从状态栈中弹出相同数量的符号，之后用栈顶状态和归约后的符号查询 GOTO 表;\n\n        if (GOTO 表查询结果为空) {\n            GOTO 出错处理程序;\n        }\n        GOTO 表查询结果压入状态栈中;\n    }\n}\n```\n\n## 使用说明\n\nconfigs 目录下的 grammar.json 文件为本项目的文法配置文件，本项目提供一种默认的文法，也可根据需要调整其中内容，其结构如下：\n\n```json5\n{\n  \"formulas\": [\n    /*\n     * 语法规则文法产生式列表，要求 II 型文法\n     * 非终结符以[]包裹，名称可自定义\n     * 终结符按照 Token 的表示规则：\u003c所属类型,内容\u003e\n     * 不同的符号间以空格分隔\n     */\n  ]\n}\n```\n\nconfigs 目录下的 message.json 文件为本项目的错误信息配置文件，用于根据出错的符号确定错误原因，本项目提供一组默认的配置，也可根据需要调整其中内容，其结构如下：\n\n```json5\n{\n  \"messages\": [\n    {\n      \"token\": /* 出错的 Token，表示规则为 \u003c所属类型,内容\u003e */,\n      \"message\": /* 对应的出错原因描述 */\n    }\n    // 类似地可添加多个规则\n  ],\n  \"defaults\": // 默认的出错原因描述，在没有成功匹配时调用\n}\n```\n\n由于文法产生式数量较多，构建 ACTION 表和 GOTO 表为一项耗时操作。运行 tables.py 文件即可根据配置的文法生成 ACTION 表和 GOTO 表并存储。\n\n```shell-session\npython tables.py\n```\n\n生成的 ACTION 表和 GOTO 表位于 builds 目录下，执行生成任务前若该目录不存在请先创建，其中 actions.txt 文件为 ACTION 表内容，gotos.txt 文件为 GOTO 表内容。生成报告同样位于 builds 目录下，其中 items.txt 文件为生成的项目集，conflicts.txt 文件为生成过程中的表项冲突。\n\n运行主程序 main.py 即可进行语法分析，其命令行参数的含义如下，其中输入图像文件路图像文件路径数量保持一致。\n\n| 参数名       | 简化参数名 | 参数描述                     |\n| --------- | ----- | ------------------------ |\n| --sources | -s    | 输入分析文件路径序列，不同的文件路径以空格分隔。 |\n| --outputs | -o    | 输出结果文件路径序列，不同的文件路径以空格分隔。 |\n\n下面是一个运行示例。\n\n```shell-session\npython main.py --sources \"s1.txt\" \"s2.txt\" --outputs \"o1.txt\" \"o2.txt\"\n```\n\n本项目提供了一些测试用例，也可根据需要调整输入和输出文件路径。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLIU42%2FSyntaxAnalyzer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLIU42%2FSyntaxAnalyzer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLIU42%2FSyntaxAnalyzer/lists"}