语法分析器源代码

时间:2025-07-07

西安邮电大学编译原理实验代码

#include<stdio.h> #include<stdlib.h> #include<string.h>

#define HIGHER 1 #define LOWER -1 #define EQUAL 0 #define TRUE 1 #define FALSE 0 #define OPER_NUM 50 #define VN_NUM 50 #define MAX_BUFFER 128 度

#define MAX_GRA_NUM 20 #define EMPTY -2 表示这对算符没有优先关系 #define STACK_SIZE 64

typedef struct { char c; int firstvt[OPER_NUM]; 在oper_list中的下标 int fir_n,last_n; int lastvt[OPER_NUM]; }vn_t;

int prior_table[OPER_NUM][OPER_NUM]; char oper_list[OPER_NUM]; int oper_num = 0;

vn_t vn_list[VN_NUM]; int vn_num = 0;

char *grammar[MAX_GRA_NUM][2]; int gra_num = 0; char start_vn;

char stack[STACK_SIZE]; int top = 0;

void get_input(char *buf); int buf_deal(char* buf); void get_FIRVT_LASTVT(int vn_n);

//默认算符数目 //默认非终结符数目 //每行输入行最大长 //最大文法数目 //算符优先表初始值,

//非终极符符号

//firstvt集,保存算符

西安邮电大学编译原理实验代码

int create_table(); void init_table();

int analyze(char *sentence); void display_table(); void display_fir_lastvt();

int get_oper(char c); //得到算符c的数组下标 没有返回-1

int get_vn(char c); //得到非终结符c的数组下标,没有返回-1

int is_vn(char a) { return (('A'<=a&&a<='Z')?1:0); }

int get_oper(char c) { int i; for(i = 0; i < oper_num; ++i) if(oper_list[i] == c) return i; return -1; }

int get_vn(char c) { int i; for(i = 0; i < vn_num; ++i) if(vn_list[i].c == c) return i; return -1; }

char reduce(int start, int end, int size) //规约 { char *tar, *g, t1, t2, left; int i, j =0, gi, ti, cn = 0; int same; tar = (char *)malloc(sizeof(char)*MAX_BUFFER);

西安邮电大学编译原理实验代码

if(!tar) { printf("Allocation fails.\n"); exit(-1); }

for(i = start; i <= end; ++i) //将此最左素短语的终结符存入tar字符串 { if(!is_vn(stack[i])) tar[j++] = stack[i]; }

tar[j++] = '\0';

for(i = 0; i < gra_num; ++i) { g = grammar[i][1]; gi = 0; ti = 0; same = FALSE; t1 = g[gi]; t2 = tar[ti]; while (t1 != '\0') { if(t2 == '\0' && !is_vn(t1)) { same = FALSE; break; } if(!is_vn(t1)) { if(t1 != t2){ same = FALSE; break; } t2 = tar[++ti]; same = TRUE; } t1= g[++gi]; } if(same && t2 == '\0') break; }

if(i == gra_num) { printf("无法找到相应文法!\n"); return FALSE;

西安邮电大学编译原理实验代码

} left = grammar[i][0][0]; return vn_list[get_vn(left)].c; }

int analyze(char *sentence) { char r, c,new_vn; int i = 0, k = 0, j, pi, printi = 1, cou = 1; //i是sentence[]和stack[]的索引 int r_index, s_index, pre_index; printf("\n\n规约过程如下表所示:\n"); printf("--------------------------------------\n"); stack[top++] = '#'; printf("序号\t符号栈\t最左素短语\t规约\t\n"); do { r = sentence[i++]; if((r_index = get_oper(r)) == -1) { printf("Error : 您输入的字符不在文法定义中!\n"); flushall();c = getchar();flushall(); return FALSE; } if(!is_vn(stack[k])) { j = k; s_index = get_oper(stack[j]); } else { j = k - 1; s_index = get_oper(stack[j]); } while(prior_table[s_index][r_index] == HIGHER) { do { pre_index = s_index; if(!is_vn(stack[j-1])) { j--; s_index = get_oper(stack[j]); } else

西安邮电大学编译原理实验代码

{ j -= 2; s_index = get_oper(stack[j]); } }while(prior_table[s_index][pre_index] != LOWER); printf(" %d\t", cou++); for(pi = 0; pi < top; ++pi) { printf("%c",stack[pi]); } printf(" \t"); for(pi = j + 1; pi <= k; ++pi) { if (pi == j+1) printf(" %c",stack[pi]); else printf("%c",stack[pi]); } if((new_vn = reduce(j + 1, k, k - j)) == 0) { return FALSE; } printf("\t\t %c\n",new_vn); k = j + 1; //规约最左素短语 stack[k] = new_vn; top = k + 1; } if(prior_table[s_index][r_index] == LOWER || EQUAL) { stack[++k] = r; top++; } else { printf("Error : 您输入的句型有错误!\n"); return FALSE; } }while(r != '#'); printf("--------------------------------------\n"); return TRUE; }

prior_table[s_index][r_index] ==

西安邮电大学编译原理实验代码

int buf_deal(char *buf) { int i = 2,count = 0; char pre = buf[0], now; char *left_g, *right_g, *new_buf; left_g = (char *)malloc(sizeof(char)*2); right_g = (char *)malloc(sizeof(char)*(MAX_BUFFER-2)); if(!left_g || !right_g) { printf("Allocation fails.\n"); exit(-2); } if(is_vn(pre)) { if(get_vn(pre) == -1) { vn_list[vn_num].c = pre; vn_list[vn_num].fir_n = 0; vn_list[vn_num].last_n = 0; vn_num++; } left_g[count] = pre; count++; left_g[count] = '\0'; } else return FALSE; if(buf[1] != '-' || buf[2] != '>') return FALSE; pre = buf[2]; count = 0; while((now = buf[++i]) != '\0') { if(now != '|') { right_g[count] = now; count++; if(is_vn(now) && is_vn(pre)) return FALSE;

西安邮电大学编译原理实验代码

{ if(get_vn(now) == -1) { vn_list[vn_num].c = now; vn_list[vn_num].fir_n = 0; vn_list[vn_num].last_n = 0; vn_num++; } else continue; } else { if(get_oper(now) == -1) { oper_list[oper_num] = now; oper_num++; } else continue; } pre = now; …… 此处隐藏:9481字,全部文档内容请下载后查看。喜欢就下载吧 ……

语法分析器源代码.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219