{"id":21503573,"url":"https://github.com/LIU42/LexicalAnalyzer","last_synced_at":"2025-10-05T05:31:07.396Z","repository":{"id":242456355,"uuid":"809595761","full_name":"LIU42/LexicalParser","owner":"LIU42","description":"《编译原理》课程设计，基于有穷自动机的类 C 语言词法分析器","archived":false,"fork":false,"pushed_at":"2024-09-06T08:28:46.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-23T18:29:40.243Z","etag":null,"topics":["automata","automation","compiler","course-project","grammar-rules","homework-project","lexical-analysis","lexical-analyzer","lexical-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-06-03T04:36:11.000Z","updated_at":"2024-09-06T08:28:49.000Z","dependencies_parsed_at":"2024-06-03T05:49:35.882Z","dependency_job_id":"44bbfe1c-e759-4a00-89f4-8a3ff3ad4d90","html_url":"https://github.com/LIU42/LexicalParser","commit_stats":null,"previous_names":["liu42/lexicalparser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FLexicalParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FLexicalParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FLexicalParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIU42%2FLexicalParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LIU42","download_url":"https://codeload.github.com/LIU42/LexicalParser/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":["automata","automation","compiler","course-project","grammar-rules","homework-project","lexical-analysis","lexical-analyzer","lexical-parser"],"created_at":"2024-11-23T18:24:14.190Z","updated_at":"2025-10-05T05:31:07.390Z","avatar_url":"https://github.com/LIU42.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LexicalAnalyzer 词法分析器\n\n*\u003cu\u003ev1.1.0 新变化：优化项目结构，同步上传词法分析器以及配套的 [语法分析器](https://github.com/LIU42/SyntaxParser) 。\u003c/u\u003e*\n\n## 项目简介\n\n本项目为《编译原理》课程设计，基于有穷自动机的类 C 语言词法分析器，可以将一种类似 C 语言的源程序（不包含编译预处理指令）解析为以下五类 Token 构成的序列：\n\n- 标识符（identifiers）\n- 常量（constants）\n- 关键字（keywords）\n- 运算符（operators）\n- 界符（bounds）\n\n其中每一个 Token 为一个四元组，其格式为 *\u003c所在行号，所在列号，所属类别，内容\u003e*。\n\n### 文法规则\n\n本项目提供一个默认的文法规则，也可通过修改默认的文法配置文件（configs/grammar.json）实现自定义规则，本项目提供的文法规则如下：\n\n- 标识符仅能由字母、数字和下划线组成，且不能以数字开头。\n\n- 常量包括整数（例如 123）、浮点数（例如 123.456）、字符（例如 'c'）、字符串（例如 \"string\"）、科学计数法表示的整数或浮点数（例如 2E5 和 1.23e-2）、复数（例如 1.5+3.8i）、布尔类型（true 和 false）。\n\n- 仅支持十进制表示的数字。\n\n- 关键字在 C 语言 32 个关键字的基础上添加 bool（表示布尔类型）和 complex（表示复数类型） 关键字。\n\n- 运算符和界符与 C 语言保持一致。\n\n## 实现方案\n\n### 识别方案\n\n针对标识符和常量的识别，采用有穷自动机实现。详细步骤如下：\n\n1. 读取文法配置文件中定义的标识符和常量的正规文法（3 型文法）构造不确定的有穷自动机（NFA）。\n\n2. 将不确定的有穷自动机采用子集法确定化为确定的有穷自动机（DFA）。\n\n3. 对标识符和常量分别利用对应确定的有穷自动机进行识别。\n\n针对运算符和界符，采用枚举的方法进行识别。识别过程中采用**贪心**的策略，即尽可能地向前搜索，在正确的前提下尽可能将更多的字符组合识别为单个运算符，也就是优先匹配更长的运算符。\n\n针对关键字的识别，由于关键字可以视为特殊的标识符，因此关键字的识别在确定为标识符后对所有的关键字进行枚举比对实现。\n\n### 分析处理流程\n\n在读入字符的过程中，分析程序的处理流程如下：\n\n1. 若当前没有正在运行的自动机且当前读入的字符可以作为某个自动机的初态，则分配该自动机进行识别。\n\n2. 若仍然没有分配到自动机，说明当前读入的字符应为运算符、界符或是空白符号，则调用可枚举 Token 处理程序进行识别。\n\n3. 若已经分配到自动机，则调用自动机处理程序，尝试进行状态转换：\n   \n   1. 若状态转换成功，则继续读入下一个字符。\n   \n   2. 转换不成功时，若位于终态则识别成功，转入进一步的判断，否则进入出错处理程序。\n\n#### 流程伪代码描述\n\n```c\nwhile (指针未到达末尾) {\n    从指针位置读入一个字符;\n    if (当前没有正在运行的自动机) {\n        根据读入的字符尝试分配自动机;\n    }\n    if (仍然没有分配到自动机) {\n        调用可枚举 Token 处理程序进行识别;\n        根据识别结果调整指针位置\n    } else {\n        尝试进行状态转换;\n        if (状态转换成功) {\n            进行进一步的判断确定其类型;\n            指针向后移动一个字符;\n        } else {\n            调用出错处理程序;\n        }\n    }\n}\n```\n\n## 使用说明\n\nconfigs 目录下的 grammar.json 文件为本项目的文法配置文件，本项目提供一种默认的文法，也可根据需要调整其中内容，其结构如下：\n\n```json5\n{\n  \"alias\": {\n    /*\n     * 这里包含文法中一组终结符的别名，其结构为别名名称对应终结符列表\n     * 使用别名可以减少文法的编写工作量\n     */\n  },\n  \"keywords\": [\n    // 所有的关键字列表\n  ],\n  \"constants\": {\n    \"specials\": [\n      // 特殊常量列表\n    ],\n    \"formulas\": [\n      /*\n       * 描述常量的正规文法产生式列表\n       * 产生式编写规则形如：\"[非终结符] -\u003e 终结符|`别名` [非终结符]|ε\"\n       * 非终结符以[]包裹，名称可自定义\n       * 终结符集合别名以``包裹\n       * 单个终结符直接编写即可\n       */\n    ],\n    \"start\": /* 文法起始符号 */, \n    \"final\": /* 文法结束符号 */\n  },\n  \"identifiers\": {\n    \"formulas\": [\n      // 描述标识符的正规文法产生式列表，编写规则同上\n    ],\n    \"start\": /* 文法起始符号 */, \n    \"final\": /* 文法结束符号 */\n  },\n  \"operators\": [\n    // 所有的运算符列表，排列的顺序即为匹配的优先顺序\n  ],\n  \"bounds\": [\n    // 所有的界符列表\n  ],\n  \"spaces\": [\n    // 所有的空白符号列表，包括空格、制表符以及换行符等\n  ]\n}\n```\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%2FLexicalAnalyzer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLIU42%2FLexicalAnalyzer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLIU42%2FLexicalAnalyzer/lists"}