用c语言编写一个正规表达式编辑器,要求分别用LL(1)分析实现、算符优先分析实现、 LR分析实现

时间: 2024-03-19 15:46:00 浏览: 15
以下是三种不同的语法分析方法的C语言实现代码,分别使用LL(1)分析、算符优先分析和LR分析方法实现正则表达式的解析。 1. LL(1)分析实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char input[100]; int pos = 0; char lookahead; void error(char *msg) { printf("ERROR: %s\n", msg); exit(1); } void match(char c) { if (lookahead == c) { lookahead = input[++pos]; } else { char msg[100]; sprintf(msg, "expect %c but got %c", c, lookahead); error(msg); } } int is_terminal(char c) { return c == 'a' || c == 'b' || c == '(' || c == ')' || c == '*' || c == '\0'; } void S(); void E(); void T(); void F(); void S() { E(); if (lookahead != '\0') { error("expect end of input"); } } void E() { T(); while (lookahead == '|') { match('|'); T(); } } void T() { F(); while (!is_terminal(lookahead)) { F(); } } void F() { switch (lookahead) { case 'a': case 'b': match(lookahead); break; case '(': match('('); E(); match(')'); break; default: error("expect a, b or ("); break; } if (lookahead == '*') { match('*'); } } int main() { printf("请输入正则表达式:"); scanf("%s", input); lookahead = input[pos]; S(); printf("输入的正则表达式符合要求。\n"); return 0; } ``` 2. 算符优先分析实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char input[100]; char stack[100]; int top = -1; char optr[7] = {'(', '|', ')', '*', 'a', 'b', '\0'}; int opnd[7][7] = { /* ( | ) * a b */ /* ( */ {'>', '<', '>', '<', '<', '<'}, /* | */ {'<', '>', '>', '<', '<', '<'}, /* ) */ {'<', '<', '=', '<', '=', '='}, /* * */ {'>', '>', '>', '=', '=', '='}, /* a */ {'>', '>', '>', '>', '=', '='}, /* b */ {'>', '>', '>', '>', '=', '='}, /* \0*/ {'<', '<', '=', '<', '=', '='} }; void error(char *msg) { printf("ERROR: %s\n", msg); exit(1); } int get_index(char c) { switch (c) { case '(': return 0; case '|': return 1; case ')': return 2; case '*': return 3; case 'a': return 4; case 'b': return 5; case '\0': return 6; default: error("invalid character"); } } char get_optr() { if (top < 0) { return '\0'; } else { return stack[top]; } } void push(char c) { if (top >= 99) { error("stack overflow"); } else { stack[++top] = c; } } char pop() { if (top < 0) { error("stack underflow"); return '\0'; } else { return stack[top--]; } } void reduce() { char c, op; do { c = pop(); } while (c != '('); op = pop(); push(op); } void shift() { char optr = get_optr(); int i = get_index(optr); int j = get_index(input[0]); if (opnd[i][j] == '<' || opnd[i][j] == '=') { push(input[0]); input[0] = input[1]; input[1] = '\0'; } else { error("invalid operator priority"); } } void init_stack() { push('\0'); push('('); } void check_stack() { char optr = get_optr(); int i = get_index(optr); int j = get_index(input[0]); if (opnd[i][j] == '<') { shift(); } else if (opnd[i][j] == '>') { reduce(); } else { error("invalid operator priority"); } } void check_input() { if (get_optr() == '(' && input[0] == ')') { error("empty parentheses"); } } void check_end() { if (get_optr() != '\0' || input[0] != '\0') { error("expect end of input"); } } void parse() { init_stack(); while (get_optr() != '\0' || input[0] != '\0') { check_stack(); check_input(); } check_end(); } int main() { printf("请输入正则表达式:"); scanf("%s", input); strcat(input, "\0"); parse(); printf("输入的正则表达式符合要求。\n"); return 0; } ``` 3. LR分析实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char input[100]; int pos = 0; char lookahead = '\0'; int action[6][5] = { /* ( ) a b $ */ /* 0 */ { 5, 0, 4, 4, 0}, /* 1 */ { 0, 0, 0, 0, 2}, /* 2 */ { 0, 0, 0, 0, 3}, /* 3 */ { 0, 0, 0, 0, 0}, /* 4 */ { 5, 0, 4, 4, 0}, /* 5 */ { 0, 0, 0, 0, 0} }; int go_to[6][2] = { /* E T */ /* 0 */ { 1, 4}, /* 1 */ { 0, 0}, /* 2 */ { 0, 0}, /* 3 */ { 0, 0}, /* 4 */ { 5, 4}, /* 5 */ { 0, 0} }; char stack[100]; int top = -1; int state = 0; void error(char *msg) { printf("ERROR: %s\n", msg); exit(1); } void shift(int s, char c) { push(c); state = s; lookahead = input[++pos]; } void reduce(char *s, int len) { for (int i = 0; i < len; i++) { pop(); } push(s[0]); int t = top; char op = stack[t - 1]; int i = op - '0'; int j = get_index(s[0]); push(go_to[i][j] + '0'); state = go_to[i][j]; } void push(char c) { if (top >= 99) { error("stack overflow"); } else { stack[++top] = c; } } char pop() { if (top < 0) { error("stack underflow"); return '\0'; } else { return stack[top--]; } } int get_index(char c) { switch (c) { case '(': return 0; case ')': return 1; case 'a': return 2; case 'b': return 3; case '$': return 4; default: error("invalid character"); } } void parse() { push('$'); push('0'); while (1) { int s = state; int i = get_index(lookahead); int a = action[s][i]; if (a == 0) { error("syntax error"); } else if (a == 1) { printf("输入的正则表达式符合要求。\n"); break; } else if (a > 0) { shift(a, lookahead); } else if (a < 0) { int len; switch (-a) { case 1: len = 1; break; case 2: len = 3; break; case 3: len = 3; break; case 4: len = 3; break; case 5: len = 1; break; case 6: len = 3; break; } char s[4]; for (int j = 0; j < len; j++) { s[j] = pop(); } s[len] = '\0'; reduce(s, len); } } } int main() { printf("请输入正则表达式:"); scanf("%s", input); lookahead = input[pos]; parse(); return 0; } ``` 以上是三种不同的语法分析方法的C语言实现代码,分别使用LL(1)分析、算符优先分析和LR分析方法实现正则表达式的解析。需要注意的是,以上代码仅供参考,可能存在不足之处,具体实现还需要根据实际需求进行调整和优化。

相关推荐

最新推荐

recommend-type

编译原理实验二——算符优先分析法设计与实现

用算符优先分析方法设计一个分析解释程序,对输入的赋值语句、输出语句、清除语句进行词法分析、语法分析、表达式求值并存储于指定变量中;若存在错误,提示错误相关信息。
recommend-type

基于算符优先分析方法的表达式语法分析器

基于算符优先分析方法的表达式语法分析器基于算符优先分析方法的表达式语法分析器基于算符优先分析方法的表达式语法分析器基于算符优先分析方法的表达式语法分析器
recommend-type

编译原理实验报告(词法语法分析 算符优先分析 有限自动机 LL(1)文法分析法等)

编程 词法分析编程实现 语法分析的编程实现 (C语言,C-free下编译) 算法 算符优先分析 确定的有限自动机 中间代码——a 建立链可用函数b 回填函数 LL(1)文法分析法
recommend-type

语法分析(算符优先).doc

(1)根据给定文法,先求出FirstVt和LastVt集合,构造算符优先关系表(要求算符优先关系表 输出到显示器或者输出到文件); (2)根据算法和优先关系表分析给定表达式是否是该文法识别的正确的算术表达式(要求输出...
recommend-type

采用算符优先分析法对表达式进行分析

1、 实验目的:采用算符优先分析法对表达式进行分析,掌握算符优先分析法的基本原理和实现过程。 2、 实验要求: 文法:无二义性的算术表达式的文法 (1)把词法分析作为语法分析的子程序实现(5分) (2)独立的...
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

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

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