编译原理LL(1)语法分析实验代码

#include<iostream>
#include<map>
#include<set>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
int n1,n2,n3;  //分别是文法的长度,非终结符的长度,终结符的长度 
const int N=100;  //输入规模为小于等于100的文法行
string  input_grammer[N];  //输入文法 
map<string,vector<string> > cutting_input;  //整理完的文法
string is_terminal[N];  //终结符 
string not_terminal[N];  //非终结符 
bool Is_terminal(string s);
bool is_empty(string s);
bool Not_terminal(string s);
map<pair<string,string> , string> table;   //分析表的输出
//table[make_pair("s","T")]="t->s";  输出格式 
//set<string> first_set,fellow_set;  
map<string,set<string> > first_set,fellow_set;//first集  fellow集
bool isempty(string s){    //判断文法是否为空(以@为空)
	if(s=="@")	return true; 
	vector<string> tmp =cutting_input[s];
	for(int i=0;i<tmp.size();i++){
		if(tmp[i]=="@")	return true;
	}
	return false;
}
void dfs_one(string t,string s){          //查找单个的终结符 
	if(Is_terminal(s)){
		 first_set[t].insert(s);
		 //cout<<s<<endl;
		 return ;
	}
	else if(Not_terminal(s))  {
		vector<string> tmp =cutting_input[s];
		for(int i=0;i<tmp.size();i++){
			for(int j=0;j<tmp[i].size();j++){
				string txt="";
				txt+=tmp[i][j];
				dfs_one(s,txt);
				//set_union(first_set[t].begin(), first_set[t].end(), first_set[s].begin(), first_set[s].end(), inserter(first_set[t], first_set[t].begin()));
				if(!isempty(txt)) break;	
			}
		}
		set_union(first_set[t].begin(), first_set[t].end(), first_set[s].begin(), first_set[s].end(), inserter(first_set[t], first_set[t].begin()));
	}
}
void dfs_all(){
	for(int i=0;i<n2;i++){
		vector<string> tmp = cutting_input[not_terminal[i]];
		for(int j=0;j<tmp.size();j++){
			int flag=1;
			for(int k=0;k<tmp[j].size();k++){
				string txt="";
				txt+=tmp[j][k];
				dfs_one(not_terminal[i],txt);
				if(!isempty(txt)){
					flag=0;
					break;
				}
			}
			//if(!flag) break;
		}
	}
}
set<string> cal_large(string s){
	//cout<<s<<endl;
	set<string> tmp;
	bool flag=1;
	for(int i=0;i<s.size();i++){
		string t ="";
		t+=s[i];
		if(Is_terminal(t)){
			tmp.insert(t);
			break;
		}
		else{
			set_union(tmp.begin(), tmp.end(), first_set[t].begin(), first_set[t].end(), inserter(tmp, tmp.begin()));
			if(!isempty(t)) break;
		}
	}
	return tmp;
}
void Fellow(){
	int cir=5;
	while(cir--){
		for(int i=0;i<n2;i++){
			vector<string> tmp = cutting_input[not_terminal[i]];
			for(int j=0;j<tmp.size();j++){
				//cout<<tmp[j]<<endl;
				for(int k=0;k<tmp[j].size()-1;k++){
					string tt="";
					tt+=tmp[j][k];
					string tt_f="";
					tt_f+=tmp[j][tmp[j].size()-1];
					//cout<<tmp[j][k]<<endl;
					//cout<<tt<<endl;
					if(Not_terminal(tt)){
						set<string> ttmp = cal_large(tmp[j].substr(k+1));
						set_union(fellow_set[tt].begin(), fellow_set[tt].end(), ttmp.begin(), ttmp.end(), inserter(fellow_set[tt], fellow_set[tt].begin()));
						bool flag=0;
						for(set<string>::iterator it=ttmp.begin();it!=ttmp.end();it++){
							if((*it)=="@")	 flag=1;
							//cout<<"here"<<endl;
						}
						if(flag){
						//	cout<<tt<<' '<<not_terminal[i]<<endl;
							set_union(fellow_set[tt].begin(), fellow_set[tt].end(), fellow_set[not_terminal[i]].begin(), fellow_set[not_terminal[i]].end(), inserter(fellow_set[tt], fellow_set[tt].begin()));
							set_union(fellow_set[tt_f].begin(), fellow_set[tt_f].end(), fellow_set[not_terminal[i]].begin(), fellow_set[not_terminal[i]].end(), inserter(fellow_set[tt_f], fellow_set[tt_f].begin()));
						}
					}
				}
			}
		}
	}
}

bool Is_terminal(string s){    //判断是否为终结符
	bool flag=0; 
	for(int i=0;i<n3;i++) 
		if(s==is_terminal[i])
			flag=1;
	return flag; 	
} 
bool Not_terminal(string s){    //判断是否是非终结符 
	bool flag=0; 
	for(int i=0;i<n3;i++) 
		if(s==not_terminal[i])
			flag=1;
	return flag; 
}
void input(){
	cout<<"请输入有几个文法行:";
	cin>>n1;
	cout<<"请接着输入上述的文法行:"<<endl;
	for(int i=0;i<n1;i++) cin>>input_grammer[i];
	//cout<<input_grammer[0][0];
	for(int i=0;i<n1;i++){
		string terminal=input_grammer[i];
		string tmp="";
		tmp+=terminal[0];
		//cutting_input[tmp]=input_grammer[i].substr(3);
		cutting_input[tmp].push_back(input_grammer[i].substr(3));
	}
	cout<<"请输入非终结符的个数:";
	cin>>n2;
	cout<<"请输入上述的非终结符:"; 
	for(int i=0;i<n2;i++)	cin>>not_terminal[i];
	cout<<"请输入终结符的个数:";
	cin>>n3;
	cout<<"请输入上述的终结符(最后一个为空):";
	for(int i=0;i<n3;i++)	cin>>is_terminal[i];
	for(int i=0;i<n2;i++){
		fellow_set[not_terminal[i]].insert("#");
	}
}
void output(){
	cout<<"\t\t\t分析表"<<endl;
	for(int i=0;i<n3;i++){
		if(!i)	cout<<"\t"<<is_terminal[i]<<"\t";
		else{
			if(is_terminal[i]=="@")
				cout<<"#\t";
			else	
				cout<<is_terminal[i]<<"\t";
		}
	} 
	cout<<endl;
	for(int i=0;i<n2;i++){
		cout<<not_terminal[i]<<"\t";
		for(int j=0;j<n3;j++){
			if(table[make_pair(not_terminal[i],is_terminal[j])]!=""){
				cout<<table[make_pair(not_terminal[i],is_terminal[j])];
			}
			cout<<"\t";
		}
		cout<<endl;
	}
	//最后输出栈
	 stack<char> input_s;  //输入栈
	 stack<char> match_s; //匹配栈
	 string I;
	 cout<<"请输入要预测分析的字符串:";
	 cin>>I;
	 input_s.push('#');
	 for(int i=0;i<I.size();i++){
	 	input_s.push(I[I.size()-i-1]);
	 } 
	 match_s.push('#');
	 match_s.push('E');
	 cout<<"\t栈\t输入\t动作\t"<<endl;
	 while(1){
	 	stack<char> tmp_i = input_s;
	 	stack<char> tmp_m = match_s;
	 	string s_i="";
	 	string s_m="";
	 	while(!tmp_i.empty()){
	 		s_i+=tmp_i.top();
	 		tmp_i.pop();
		 }
		 while(!tmp_m.empty()){
	 		s_m+=tmp_m.top();
	 		tmp_m.pop();
		 }
		 //cout<<"\t"+s_i+"\t";
		 cout<<"\t";
		 for(int i=0;i<s_i.size();i++)
		 	cout<<s_i[s_i.size()-i-1];
		cout<<"\t";
		 cout<<"\t"+s_m+"\t";
		 string m_top = "";
		 m_top+=match_s.top();
		// match_s.pop();
		 if(input_s.top()==match_s.top()){
		 	cout<<"匹配"<<input_s.top()<<"\t"; 
		 	match_s.pop();
		 	input_s.pop(); 
		 }
		 else{
		 	match_s.pop();
		 	string i_top="";
		 	i_top+=input_s.top();
		 	string match = table[make_pair(m_top,i_top)].substr(3);
		 	if(match!="@"){
		 		for(int i=0;i<match.size();i++){
		 			match_s.push(match[match.size()-i-1]);
				 }
				 //cout<<"输出"<<table[make_pair(m_top,i_top)]<<'\t';
			 }
			cout<<"输出"<<table[make_pair(m_top,i_top)]<<'\t';
		 }
		 cout<<endl;
		 if(match_s.top()==input_s.top()&&input_s.top()=='#')	break;
	 }
	 
}
void create_Table(){
	for(int i=0;i<n2;i++){
		vector<string> tmp = cutting_input[not_terminal[i]];
		//first table
		for(int j=0;j<tmp.size();j++){
			set<string> tmp_set = cal_large(tmp[j]);
			for(set<string>::iterator it=tmp_set.begin();it!=tmp_set.end();it++){
				table[make_pair(not_terminal[i],(*it))]=not_terminal[i]+"->"+tmp[j];
			}
		}	
		//fellow table
		set<string> tmp_set = first_set[not_terminal[i]];
		bool flag=0;
		for(set<string>::iterator it=tmp_set.begin();it!=tmp_set.end();it++){
			if((*it)=="@"){
				flag=1;
				break;	
			}
		}	 
		if(flag){
			set<string> fellow_tmp = fellow_set[not_terminal[i]];
			for(set<string>::iterator it=fellow_tmp.begin();it!=fellow_tmp.end();it++){
				string t = not_terminal[i];
				t+="->@";
				table[make_pair(not_terminal[i],(*it))]=t;
			}
		}
	}
}
int main(){
	input();
	//for(int i=0;i<n2;i++)	cout<<not_terminal[i];
	//cout<<endl;	
	dfs_all();
	Fellow();
	create_Table(); 
	output();
	//cout<<table[make_pair("G","#")]<<endl;
	return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验二--LL(1)分析法实验报告全文共11页,当前为第1页。实验二--LL(1)分析法实验报告全文共11页,当前为第1页。实验LL(1)分析法 实验二--LL(1)分析法实验报告全文共11页,当前为第1页。 实验二--LL(1)分析法实验报告全文共11页,当前为第1页。 实验目的 通过完成预测分析法的语法分析程序,了解预测分析法和递归子程序法的区别和联系。使学生了解语法分析的功能,掌握语法分析程序设计的原理和构造方法,训练学生掌握开发应用程序的基本方法。有利于提高学生的专业素质,为培养适应社会多方面需要的能力。 二、实验内容及设计原理 所谓LL(1)分析法,就是指从左到右扫描输入串(源程序),同时采用最左推导,且对每次直接推导只需向前看一个输入符号,便可确定当前所应当选择的规则。实现LL(1)分析的程序又称为LL(1)分析程序或LL1(1)分析器。 我们知道一个文法要能进行LL(1)分析,那么这个文法应该满足:无二义性,无左递归,无左公因子。当文法满足条件后,再分别构造文法每个非终结符的FIRST和FOLLOW集合,然后根据FIRST和FOLLOW集合构造LL(1)分析表,最后利用分析表,根据LL(1)语法分析构造一个分析器。LL(1)的语法分析程序包含了三个部分,总控程序,预测分析表函数,先进先出的语法分析栈,本程序也是采用了同样的方法进行语法分析,该程序是采用了C++语言来编写,其逻辑结构图如下: LL(1)预测分析程序的总控程序在任何时候都是按STACK栈顶符号X和当前的输入符号a做哪种过程的。对于任何(X,a),总控程序每次都执行下述三种可能的动作之一: (1)若X = a ='#',则宣布分析成功,停止分析过程。 (2)若X = a '#',则把X从STACK栈顶弹出,让a指向下一个输入符号。 (3)若X是一个非终结符,则查看预测分析表M。若M[A,a]中存放着关于X的一个产生式,那么,首先把X弹出STACK栈顶,然后,把产生式的右部符号串按反序一一弹出STACK栈(若右部符号为ε,则不推什么东西进STACK栈)。若M[A,a]中存放着"出错标志",则调用出错诊断程序ERROR。 三、程序结构描述 实验二--LL(1)分析法实验报告全文共11页,当前为第2页。实验二--LL(1)分析法实验报告全文共11页,当前为第2页。 实验二--LL(1)分析法实验报告全文共11页,当前为第2页。 实验二--LL(1)分析法实验报告全文共11页,当前为第2页。 1、定义的变量 初始化预测分析表: LL E[8]={"TG","TG","error","error","error","error","error","error"}; LL G[8]={"error","error","null","+TG","-TG","error","error","null"}; LL T[8]={"FS","FS","error","error","error","error","error","error"}; LL S[8]={"error","error","null","null","null","*FS","/FS","null"}; LL F[8]={"i","(i)","error","error","error","error","error","error"}; const int MaxLen=10; 初始化栈的长度 const int Length=10; 初始化数组长度 char Vn[5]={'E','G','T','S','F'}; 非终结符数组 char Vt[8]={'i','(',')','+','-','*','/','#'}; 终结符数组 char ch,X; /全局变量,ch用于读当前字符,X用于获取栈顶元素 char strToken[Length]; 存储规约表达式 2、定义的函数 class stack 栈的构造及初始化 int length(char *c) 输出字符数组的长度 void print(int i,char*c) 剩余输入串的输出 void run() 分析程序 3、LL(1)预测分析程序流程图 实验二--LL(1)分析法实验报告全文共11页,当前为第3页。实验二--LL(1)分析法实验报告全文共11页,当前为第3页。 实验二--LL(1)分析法实验报告全文共11页,当前为第3页。 实验二--LL(1)分析法实验报告全文共11页,当前为第3页。 四、程序源代码及运行结果 #include<iostream> using namespace std; const int MaxLen=10; //初始化栈的长度 const int Length=10;//初始化数组长度 ch

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值