自己编程实现对C语言的词法分析程序,加深对词法分析原理的理解。 二、 实验要求 1、 C语言分为关键字、标识符、数字、运算符、界符五类。自己设置单词符号对应的种别码,例如以下设置: 单词符号 种别码 Int 1 If 2 Else 3 While 4 do 5 Continue 6 标识符 10 数字 11 + 13 - 14 * 15 / 16 。。。。。。 。。。。。。 2、要求能够识别以下代码: int main() { char c=56; int a=1; int bb_b1=7; if(a>bb_b1)c=9; else c=90; while(1){;} return 67; }的实验代码,并且如何使用Devc++进行编译

时间: 2023-12-10 16:41:07 浏览: 28
以下是一个简单的C语言词法分析程序,可以识别上述要求的代码,并输出每个单词的种别码和属性值: ```c #include <stdio.h> #include <ctype.h> #include <string.h> #define MAX_LEN 100 // 定义单词符号对应的种别码 #define KW_INT 1 #define KW_IF 2 #define KW_ELSE 3 #define KW_WHILE 4 #define KW_DO 5 #define KW_CONTINUE 6 #define IDENTIFIER 10 #define NUMBER 11 #define ADD 13 #define SUB 14 #define MUL 15 #define DIV 16 #define SEMICOLON 17 #define LEFT_BRACKET 18 #define RIGHT_BRACKET 19 #define LEFT_BRACE 20 #define RIGHT_BRACE 21 #define GREATER_THAN 22 #define LESS_THAN 23 #define EQUAL 24 // 定义词法分析函数 void lexicalAnalysis(char *code); int main() { char code[MAX_LEN] = "int main() { char c=56; int a=1; int bb_b1=7; if(a>bb_b1)c=9; else c=90; while(1){;} return 67; }"; lexicalAnalysis(code); return 0; } void lexicalAnalysis(char *code) { int i = 0; int len = strlen(code); while (i < len) { // 跳过空格和换行符 while (isspace(code[i])) { i++; } // 判断关键字 if (strncmp(code + i, "int", 3) == 0) { printf("%d\t%s\n", KW_INT, "int"); i += 3; } else if (strncmp(code + i, "if", 2) == 0) { printf("%d\t%s\n", KW_IF, "if"); i += 2; } else if (strncmp(code + i, "else", 4) == 0) { printf("%d\t%s\n", KW_ELSE, "else"); i += 4; } else if (strncmp(code + i, "while", 5) == 0) { printf("%d\t%s\n", KW_WHILE, "while"); i += 5; } else if (strncmp(code + i, "do", 2) == 0) { printf("%d\t%s\n", KW_DO, "do"); i += 2; } else if (strncmp(code + i, "continue", 8) == 0) { printf("%d\t%s\n", KW_CONTINUE, "continue"); i += 8; } // 判断标识符 else if (isalpha(code[i])) { int j = i; while (isalnum(code[j])) { j++; } printf("%d\t%.*s\n", IDENTIFIER, j - i, code + i); i = j; } // 判断数字 else if (isdigit(code[i])) { int j = i; while (isdigit(code[j])) { j++; } printf("%d\t%.*s\n", NUMBER, j - i, code + i); i = j; } // 判断运算符和界符 else if (code[i] == '+') { printf("%d\t%c\n", ADD, code[i]); i++; } else if (code[i] == '-') { printf("%d\t%c\n", SUB, code[i]); i++; } else if (code[i] == '*') { printf("%d\t%c\n", MUL, code[i]); i++; } else if (code[i] == '/') { printf("%d\t%c\n", DIV, code[i]); i++; } else if (code[i] == ';') { printf("%d\t%c\n", SEMICOLON, code[i]); i++; } else if (code[i] == '(') { printf("%d\t%c\n", LEFT_BRACKET, code[i]); i++; } else if (code[i] == ')') { printf("%d\t%c\n", RIGHT_BRACKET, code[i]); i++; } else if (code[i] == '{') { printf("%d\t%c\n", LEFT_BRACE, code[i]); i++; } else if (code[i] == '}') { printf("%d\t%c\n", RIGHT_BRACE, code[i]); i++; } else if (code[i] == '>') { printf("%d\t%c\n", GREATER_THAN, code[i]); i++; } else if (code[i] == '<') { printf("%d\t%c\n", LESS_THAN, code[i]); i++; } else if (code[i] == '=') { if (code[i+1] == '=') { printf("%d\t%s\n", EQUAL, "=="); i += 2; } else { printf("%d\t%c\n", EQUAL, code[i]); i++; } } // 其他情况 else { printf("Unknown token %c\n", code[i]); i++; } } } ``` 使用Dev-C++进行编译的步骤如下: 1. 打开Dev-C++,点击菜单栏的“文件”-“新建”-“源文件”; 2. 将上述代码复制到新建的源文件中; 3. 点击菜单栏的“运行”-“编译”; 4. 如果编译成功,会在输出窗口中显示“编译成功”; 5. 点击菜单栏的“运行”-“运行”,可在控制台中查看程序输出结果。

相关推荐

最新推荐

recommend-type

编译原理实验一——C 语言词法分析器设计与实现

通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
recommend-type

词法分析示例程序(C语言编写,针对PL/0语言)

词法分析阶段是编译过程的第一个阶段,是编译的基础。...词法分析程序实现这个任务。词法分析程序可以使用Lex等工具自动生成。 PL/0语言是Pascal语言的子集,它具备一般高级程序设计语言的典型特点。
recommend-type

编译原理实验二词法分析

构造tiny语言的词法分析器(扫描器),利用第三方的lex工具进行构造。 构造出的扫描器,能够读入教材样例中给出的tiny语言的示例代码,分解成token输出。 掌握使用lex工具 掌握构造Tiny语言词法分析器 掌握LEX的...
recommend-type

编译原理词法分析C语言实现实验报告(含源码)

编译原理词法分析C语言实现实验报告(含源码) 适合学生报告作参考 简单易懂
recommend-type

词法分析器 编译原理 C语言(内含设计报告和源代码)

用C语言编写的词法分析器,内部含有完成的程序源代码,拷贝出来即可使用。还有报告设计文档,供大家参考一下。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。