编译原理实验:ll(1)文法分析

一、实验目的

运用LL(1)语法分析的基本原理实现对于句子的语法分析

二、实验要求

1、文法由用户输入(注意:ε符号由@代替,文法中的“定义为”符号位->)
2、数据结构的定义
(1)产生式的数据类型定义如下:
typedef struct producion
{
char left;
char right[MAXSIZE];
} production;
(2)非终结符的first集合的数据类型定义如下:
typedef struct first
{
char ch;
char firstSet[MAXSIZE];
}first;
(3)非终结符的follow集合的数据类型定义如下:
typedef struct follow
{
char ch;
char followSet[MAXSIZE];
}follow;
(4)终结符号集合
字符数组ts;
(5)非终结符号集合
字符数组nts;
(6)文法
production数组 gram;//注意:设产生式为6条
(7)select集合
二维字符数组Select;
(8)LL(1)分析表
二维整型数组 lllist; //元素的值为0~5,即产生式在garm数组的下标;

三、实验内容

1、任意输入一个文法,判断它是否为LL(1)文法
2、如果是一个LL(1)文法,请构造该文法对应的LL(1)分析表; 如果不是,请输出“该文法不是LL(1)文法”。
3、输入一个字符串,请用LL(1)分析算法判断它是否为该文法的一个句子。

四、数据结构设计

主要是通过map数据结构存储分析表、first和follow集。
在这里插入图片描述

关键函数设计:
1.创建分析表:

在这里插入图片描述

五、算法原理和流程
算法原理步骤:
1.先将输入的文法消除左递归
2.然后提取公因式
3.输出非终结符的first和follow集,再计算各个产生式的select集,判断是否是ll(1)文法
4.如果是ll(1)文法则构建分析表
5.根据分析表来判断一个输入的串是否是该文法的一个句子。

六、实验结果
首先输入文法,终结符和非终结符的信息,便会输出分析表。
然后输入一个串便会输出分析该串的过程。

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

七、实验代码

#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;
}

  • 4
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值