【编译原理】《编译原理第二版》LR0例题代码

以下是本人关于《编译原理第二版》里LR0例题的理解以及代码实现:

/*
		广西师范大学 计算机科学与工程学院 
		GuangXi Normal University 
		College of Computer Science and Engineering  
		Student STZ 
*/ 
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int State[] = {0,1,2,3,4,5,6,7,8,9,10,11};
string ACTION[20][6] = {
	{"S2","S3",  "",   "",   ""},
	{  "",  "",  "",   "","acc"},
	{  "",  "","S4","S10",   ""},
	{  "",  "","S5","S11",   ""},
	{  "",  "","S4","S10",   ""},
	{  "",  "","S5","S11",   ""},
	{"r1","r1","r1", "r1", "r1"},
	{"r2","r2","r2", "r2", "r2"},
	{"r3","r3","r3", "r3", "r3"},
	{"r5","r5","r5", "r5", "r5"},
	{"r4","r4","r4", "r4", "r4"},
	{"r6","r6","r6", "r6", "r6"},
};
int GOTO[20][3] ={
	{1,0,0},
	{0,0,0},
	{0,6,0},
	{0,0,7},
	{0,8,0},
	{0,0,9},
	{0,0,0},
	{0,0,0},
	{0,0,0},
	{0,0,0},
	{0,0,0},
	{0,0,0},
};
char characterstack[20];//符号栈 
int  charlength;//符号栈长度 
int  statestack[20];//状态栈 
int  statelength;//状态栈长度 
char input[] = "bccd#";//输入串 
int  inputlength = 5;//输入串长度 
string ac = "";//记录每次匹配到的ACTION
int gt;//记录每次匹配到的ACTION
void initialize(){//初始化状态栈 
	statestack[0] = 0;
	statelength++;
	characterstack[0] = '#';
	charlength++;
}
int locateAC(char c){ //确定ACTION的列 
	if(c=='a'){
		return 0;
	}else if(c=='b'){
		return 1;
	}else if(c=='c'){
		return 2;
	}else if(c=='d'){
		return 3;
	}else if(c=='#'){
		return 4;
	}
}
int locateGT(char c){ //确定GOTO的列 
	if(c=='E'){
		return 0;
	}else if(c=='A'){
		return 1;
	}else if(c=='B'){
		return 2;
	}
}
void characterpush(){//符号栈入栈
	characterstack[charlength] = input[0];
	charlength+=1;
}
void stateACpush(int r,int c){//状态栈入栈 
//	cout<<ACTION[r][c]<<endl;
	if(ACTION[r][c].compare("S2")==0){//匹配ac 
		statestack[statelength]=2;
		statelength++;
	}else if(ACTION[r][c].compare("S3")==0){
		statestack[statelength]=3;
		statelength++;
	}else if(ACTION[r][c].compare("S4")==0){
		statestack[statelength]=4;
		statelength++;
	}else if(ACTION[r][c].compare("S5")==0){
		statestack[statelength]=5;
		statelength++;
	}else if(ACTION[r][c].compare("S10")==0){
		statestack[statelength]=10;
		statelength++;
	}else if(ACTION[r][c].compare("S11")==0){
		statestack[statelength]=11;
		statelength++;
	}else{
		cout<<"PUSH_ERROR!"<<endl;
	} 
}
void stateGTpush(int r,int c){//GOTO表入状态栈 
	statestack[statelength] = GOTO[r][c];
	gt = GOTO[r][c];
	statelength++;
}
void statepop(int lenght){//状态栈出栈 
	if(lenght==1){
		statestack[statelength-1]='\0';
		statelength--;
	}else if(lenght==2){
		statestack[statelength-1]='\0';
		statestack[statelength-2]='\0';
		statelength-=2;
	}
}
void characterpop(){//符号栈出栈或转换 
//    cout<<charlength<<endl;
	if(ac.compare("r2")==0){//bB
		statepop(2);
		characterstack[charlength-2]='E';
		charlength-=1;
		stateGTpush(statestack[statelength-1],locateGT(characterstack[charlength-1]));
	}else if(ac.compare("r5")==0){//cB
		statepop(2);
		characterstack[charlength-2]='B';
		charlength-=1;
		stateGTpush(statestack[statelength-1],locateGT(characterstack[charlength-1]));
	}else if(ac.compare("r6")==0){//d	
		statepop(1);
		characterstack[charlength-1]='B';
		stateGTpush(statestack[statelength-1],locateGT(characterstack[charlength-1]));
	}
	
}
void inputpop(){//符号串匹配成功,输入队列出列 
	for(int i=0;i<inputlength-1;i++){
		input[i] = input[i+1];
	}
	inputlength--;
	input[inputlength] = '\0';
} 
int seleteSt(int r,int c){//根据ACTION表匹配Sr选择策略 
//	cout<<r<<c<<endl;
//	cout<<ACTION[r][c]<<endl;
	if(ACTION[r][c].compare("S2")==0){
		ac = "S2";
		return 1;
	}else if(ACTION[r][c].compare("S3")==0){
		ac = "S3";
		return 1;
	}else if(ACTION[r][c].compare("S4")==0){
		ac = "S4";
		return 1;
	}else if(ACTION[r][c].compare("S5")==0){
		ac = "S5";
		return 1;
	}else if(ACTION[r][c].compare("S10")==0){
		ac = "S10";
		return 1;
	}else if(ACTION[r][c].compare("S11")==0){
		ac = "S11";
		return 1;
	}else if(ACTION[r][c].compare("r1")==0){
		ac = "r1";
		return 2;
	}else if(ACTION[r][c].compare("r2")==0){
		ac = "r2";
		return 2;
	}else if(ACTION[r][c].compare("r3")==0){
		ac = "r3";
		return 2;
	}else if(ACTION[r][c].compare("r4")==0){
		ac = "r4";
		return 2;
	}else if(ACTION[r][c].compare("r5")==0){
		ac = "r5";
		return 2;
	}else if(ACTION[r][c].compare("r6")==0){
		ac = "r6";
		return 2;
	}else if(ACTION[r][c].compare("acc")==0){
		ac = "acc";
		cout<<"LR0分析完毕!"<<endl;
		return 3;
	}else{
		cout<<"ERROR!"<<endl;
		system("pause");
		return 0;
	}
}
void dostrategy(int strategy){//执行具体策略 
	if(strategy==1){
		stateACpush(statestack[statelength-1],locateAC(input[0]));//状态栈入栈
		characterpush();//符号栈入栈
		inputpop();//输入栈出栈 
	}else if(strategy==2){
		characterpop();
	}else if(strategy==3){
		return ;
	}else{
		cout<<"ERROR!"<<endl;
		system("pause");
	}
	
} 
void march(int si){//方法进入入口 
		dostrategy(seleteSt(statestack[si],locateAC(input[0])));//选择策略:1状态入栈,2状态出栈,3结束,0出错 
} 
void division(){//输出模块 
	cout<<"********************************"<<endl<<endl;
//	cout<<charlength<<endl;
	cout<<"statestack:";
	for(int i=0;i<statelength;i++){
		cout<<statestack[i];
	}
	cout<<endl;
	cout<<"characterstack:";
	for(int i=0;i<charlength;i++){
		cout<<characterstack[i];
	}
	cout<<endl;
	cout<<"input:";
	for(int i=0;i<inputlength;i++){
		cout<<input[i];
	}
	cout<<endl;
	cout<<"ACTION:"<<ac<<endl;
	cout<<"GOTO:"<<gt<<endl;
	cout<<endl<<"********************************"<<endl;
}
int main(){
	initialize();
	while(ac.compare("acc")!=0){
		division();
		march(statelength-1);	
	}
	return 0;
}

运行截图:
在这里插入图片描述

在这里插入图片描述
敬请批评指正。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芣苢的成长之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值