lr(0)分析程序c语言,C语言编写源程序建立LR(1)分析器
时间: 2023-11-28 18:50:47 浏览: 221
以下是一个简单的 LR(0) 分析程序的 C 语言代码示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STATE_NUM 100
#define MAX_SYMBOL_NUM 100
typedef struct {
int next_state[MAX_SYMBOL_NUM];
int reduce_prod; // 产生式编号,-1 表示不能规约
} state_t;
state_t states[MAX_STATE_NUM];
int state_num = 0;
int symbol_num;
char symbols[MAX_SYMBOL_NUM]; // 终结符和非终结符的集合
int start_symbol; // 开始符号编号
int prod_num;
char prods[MAX_SYMBOL_NUM][100]; // 产生式
int parse_table[MAX_STATE_NUM][MAX_SYMBOL_NUM];
// 添加一个新状态
int add_state() {
int i;
state_t *new_state = &states[state_num++];
for (i = 0; i < symbol_num; i++) {
new_state->next_state[i] = -1;
}
new_state->reduce_prod = -1;
return state_num - 1;
}
// 判断一个状态是否已经存在
int find_state(int next_state[MAX_SYMBOL_NUM], int reduce_prod) {
int i, j;
for (i = 0; i < state_num; i++) {
state_t *s = &states[i];
if (s->reduce_prod != reduce_prod) {
continue;
}
for (j = 0; j < symbol_num; j++) {
if (s->next_state[j] != next_state[j]) {
break;
}
}
if (j == symbol_num) {
return i;
}
}
return -1;
}
// 计算闭包
void closure(int state[MAX_SYMBOL_NUM], int size, int result[MAX_SYMBOL_NUM], int *result_size) {
int i, j;
for (i = 0; i < size; i++) {
result[i] = state[i];
}
*result_size = size;
for (i = 0; i < prod_num; i++) {
char *prod = prods[i];
for (j = 0; j < *result_size; j++) {
if (symbols[result[j]] == prod[0]) {
result[*result_size] = i;
(*result_size)++;
}
}
}
}
// 计算移进后的状态
void shift(int state[MAX_SYMBOL_NUM], int size, int symbol, int result[MAX_SYMBOL_NUM], int *result_size) {
int i, j;
for (i = 0; i < size; i++) {
result[i] = states[state[i]].next_state[symbol];
}
*result_size = size;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++) {
if (result[i] == result[j]) {
result[j] = -1;
}
}
}
for (i = 0; i < size; i++) {
if (result[i] == -1) {
for (j = i; j < size - 1; j++) {
result[j] = result[j + 1];
}
(*result_size)--;
}
}
}
// 计算一个状态的 LR(0) 闭包
void lr0_closure(int state[MAX_SYMBOL_NUM], int size, int result[MAX_SYMBOL_NUM], int *result_size) {
int i, j;
closure(state, size, result, result_size);
for (i = 0; i < *result_size; i++) {
int prod = result[i];
char *p = prods[prod];
for (j = 1; p[j] != '\0'; j++) {
if (p[j] == symbols[start_symbol]) {
break;
}
if (p[j] == symbols[p[0]]) {
int k;
for (k = 0; k < *result_size; k++) {
if (result[k] == prod) {
break;
}
}
shift(result, *result_size, p[0], result, result_size);
if (k < *result_size) {
k = find_state(result, -1);
if (k == -1) {
k = add_state();
memcpy(states[k].next_state, result, symbol_num * sizeof(int));
}
states[k].reduce_prod = prod;
}
break;
}
}
}
}
// 构造 LR(0) 自动机
void build_lr0_parser() {
int i, j;
int state[MAX_SYMBOL_NUM];
int state_size;
state[0] = 0;
state_size = 1;
lr0_closure(state, state_size, state, &state_size);
while (1) {
for (i = 0; i < symbol_num; i++) {
if (states[0].next_state[i] != -1 && i == symbols[start_symbol]) {
parse_table[0][i] = 1;
} else if (states[0].next_state[i] != -1) {
shift(state, state_size, i, state, &state_size);
int k = find_state(state, -1);
if (k == -1) {
k = add_state();
memcpy(states[k].next_state, state, symbol_num * sizeof(int));
}
parse_table[0][i] = k;
}
}
if (state_size == 0) {
break;
}
lr0_closure(state, state_size, state, &state_size);
}
}
// 构造 LR(1) 自动机
void build_lr1_parser() {
// TODO: 实现 LR(1) 分析器的构造
}
int main() {
// TODO: 读入文法,初始化 symbols、start_symbol、prod_num、prods 等变量
build_lr0_parser();
build_lr1_parser();
// TODO: 使用分析器进行语法分析
return 0;
}
```
关于如何构造 LR(1) 分析器,可以参考类似《编译原理》这样的教材,这里就不再赘述。
阅读全文