其他分享
首页 > 其他分享> > 实验二、语法设计——基于LL(1)文法的预测分析表法

实验二、语法设计——基于LL(1)文法的预测分析表法

作者:互联网


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

 // 预测分析表大小 
 #define A 255
 #define a 255
 
 // 终结符个数
 #define Vtn 5 
 
 
 //终结符集
 char Vt[5] = {'i', '+', '*', '(', ')'}; 
 // 输入字符串 
 char w[255];
 // 预测分析表
 const char *M[A][a];
 // 输入指针 
 char *current;
 
 // 预测分析栈
 char stack[50];
 // 栈顶指针 
 int top = 1;
 //栈顶元素
 char topChar; 
 
 // 初始化预测分析表
 void initM();
 //预测分析法 
 void LL1(char w[255], const char *M[A][a]);
 // 入栈操作
 void Push(const char *M[A][a]); 
 // 出栈操作
 void Pop();
 // 判断是否位终结符
 bool isVt(char c); 
 //遍历栈
 void showStack(); 
  
 int main()
 {
 	//初始化表 
 	initM();
 	
 	// 初始化栈
 	stack[0] = '#';
 	stack[1] = 'E';
 	
 	printf("请输入待检验的字符串:");
 	while(scanf("%s",w) != EOF)
	{
		int length=0;
		while(w[length++] != '\0');
		w[length-1] = '#';
		LL1(w, M);
		printf("请输入待检验的字符串:");
	}
 	printf("%s",M['F']['(']);
 }
 
 void initM()
 {
 	// 预测分析表
 	// L相当于E',K相当于T'
 	for(int i=0;i<A;i++)
		for(int j=0;j<a;j++)
			M[i][j]= "-1";
 	M['E']['i'] = "TL";
 	M['E']['('] = "TL";
 	M['L']['+'] = "+TL";
 	M['L'][')'] = "ε";
 	M['L']['#'] = "ε";
 	M['T']['i'] = "FK";
 	M['T']['('] = "FK";
 	M['K']['+'] = "ε";
 	M['K']['*'] = "*FK";
 	M['K'][')'] = "ε";
 	M['K']['#'] = "ε";
 	M['F']['i'] = "i";
 	M['F']['('] = "(E)";
 }
 
 void LL1(char w[255], const char *M[A][a])
 {
 	current = w;
 	topChar = stack[top];
 	printf("+-----------+-----------+-----------+\n");
 	printf("  分析栈        剩余串      产生式\n");
 	printf("+-----------+-----------+-----------+\n\n");
 	while(topChar != '#')
 	{
 		if(topChar == *current)
 		{
 			showStack();
 			printf("\t\t%8s",current);
 			printf("%5c匹配\n",topChar);
 			Pop();
 			current++;
		}
		else if(isVt(topChar))
		{
			printf("+-----------+-----------+-----------+\n\n");
			printf("字符 %c 与字符 %c 不匹配!\n\n",topChar, *current);
			return;
		}
		else if(strcmp(M[topChar][*current], "-1") == 0)
		{
			printf("+-----------+-----------+-----------+\n\n");
			printf("无可用产生式,此串不是此文法的句子!\n\n");
			return;
		}
		else
		{
			showStack();
			printf("\t\t%8s",current);
			printf("%5c->%s\n", topChar, M[topChar][*current]);
			Pop();
			Push(M);
		}
		topChar = stack[top];
	}
	showStack();
	printf("\t\t%8s",current);
	printf("    接受\n");
	printf("+-----------+-----------+-----------+\n\n");
 }
 
 void Push(const char *M[A][a])
 {
 	if(strcmp(M[topChar][*current], "ε") == 0) {
 		return;
	 }
 	//分析表项长度 
 	int length;
 	length = strlen(M[topChar][*current]);
 	top += length;
	// 逆序进栈 
 	for(int i = 0; i <length; i++)
 	{
 		stack[top - i] = M[topChar][*current][i];
	}
 }
 
 void Pop()
 {
 	stack[top] = '\0';
 	top--;
 }
 
 bool isVt(char c)
 {
 	bool flag = false;
 	for(int i = 0; i < Vtn; i++)
 	{
 		if(Vt[i] == c)
 		{
 			flag = true;
 			break;
		}
	}
	return flag;
 }
 
 void showStack()
 {
 	printf(" ");
 	for(int i = 0; i <= top; i++)
 	{
 		printf("%c",stack[i]);
	}
 }

标签:文法,int,LL,表法,char,length,topChar,printf,void
来源: https://www.cnblogs.com/zenglearn/p/16256179.html