⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 课程设计.cpp

📁 编译原理
💻 CPP
字号:
#include <iostream>
#include<fstream>
#include <string>
#include <stack>
#include <vector>
#include <deque>
#include "word.h"
#include "change.h"
//#include "cifa.h"
using namespace std;
struct Link
{
	int 	place;
	Link * next;
};//链结点;
stack <string> sign;//符号表;
struct InterLanguage
{
	string operor ;
	string operndL;
string operndR;
string redult;
Link * link;
}; //中间代码结点;
deque <InterLanguage*>InterLanguagequeue;
stack <char>signstack;
stack <string>operant;//操作符;
stack <string>operantor;//操作数;
stack <InterLanguage*>interLanguagestack;//中间代码结点栈;
int lcount=0;
string a[8][10]={
{"CtBi","","","","","","","","",""},
{"", "HI","Hc","","","","","","",""},
{"","","","Dr","","","","","",""},
{"","E=I","","","","","","","",""},
{"","GI","Gc","","","","","","",""},
{"","","",""," ","EF","EF","EF","EF"," "},
{"","","","","","+","-","*","/",""},
{"","I","c","","","","","","",""}
};  
char b[20]={'i','I','c','r','e','+','-','*','/',';','t','=','A','B','H','C','E','G','F','D'};
int getid(char c,char*a,int size){
for(int i=0;i<size;i++)
{if(c==b[i]){
	if(i>11)
	return i-12;
 return i;
}
}
return -1;
}

int analysis(char z){
	      	char cl;
			string cn;
	int m;
	int n;
	const int l=20;
	InterLanguage* node;

             if(z==signstack.top()){
			   if(z==';'){
                   cout<<"接受"<<endl;
				   if(!operant.empty()){
                   node=new InterLanguage;
				   lcount++;
				   node->operor=operant.top();
				   operant.pop();
				   node->operndR=operantor.top();
				   operantor.pop();
				   node->operndL=operantor.top();
				   operantor.pop();
                   node->link=NULL;
				   string temp="t";
				   operantor.push(temp);
				   node->redult="t";
				   InterLanguagequeue.push_back(node);
				   InterLanguage *tnode=interLanguagestack.top();
                   tnode->operndL=operantor.top();
				   operantor.pop();
				    lcount++;
				
					
					
					InterLanguagequeue.push_back(tnode);
				   }
				   cout<<"语法分析成功";
				   return 3;}
		   
		   char cc=signstack.top();
		   signstack.pop();
		   
		   
          if (cc=='i'){
		   
		  cout<<"if匹配"<<endl;

		   }
		   else if (cc=='t'){
			   cout<<"then匹配"<<endl;
			   node=new InterLanguage;
			   lcount++;
			   node->link=new Link;
			   if(!operantor.empty()){
			     node->operndR=operantor.top();
				 operantor.pop();
				 node->operndL=operantor.top();
				 operantor.pop();
				 
			   }
			   if(!operant.empty()){
			     node->operor=operant.top();
				 operant.pop();
			   }
			   node->link->place=lcount+2;
			   InterLanguagequeue.push_back(node);
			   node=new InterLanguage;
			   lcount++;
			   node->link=new Link;
			   node->operor="jump";
			   node->operndL="";
			   node->operndR="";
			    InterLanguagequeue.push_back(node);
		   }
		if(cc=='e'){//*****
			   cout<<"else匹配"<<endl;
			   if(!operant.empty()){
			       node=new InterLanguage;
				   lcount++;
				   node->operor=operant.top();
				   operant.pop();
				   node->operndR=operantor.top();
				   operantor.pop();
				   node->operndL=operantor.top();
				   operantor.pop();
                   node->link=NULL;
				   string temp="t";
				   operantor.push(temp);
				   node->redult="t";
				   InterLanguagequeue.push_back(node);
				   InterLanguage *tnode=interLanguagestack.top();
                   tnode->operndL=operantor.top();
				   operantor.pop();
				    lcount++;
					InterLanguagequeue.push_back(tnode);
					  
				
			   
                
				node=new InterLanguage;
			   lcount++;
			   node->link=new Link;
			   node->operor="jump";
			   node->operndL="";
			   node->operndR="";
			   node->link->place=lcount+2;
			   InterLanguagequeue.push_back(node);

                     deque<InterLanguage*>::iterator iter=InterLanguagequeue.begin();
					 (*(iter+1))->link->place=lcount;




			  }
		 }
		  
		   if(cc=='I'||cc=='c'){
             cout<<sign.top()<<"匹配"<<endl;
		     operantor.push(sign.top());
			 sign.pop();
		   }
		   if(cc=='r'){
			 cout<<sign.top()<<"匹配"<<endl;
		     operant.push(sign.top());
			 sign.pop();
		   }
		   if(cc=='='){
             cout<<"=匹配"<<endl;
		     node=new InterLanguage;
			 lcount++;
			 node->link=NULL;
			 node->operor="=";
			 node->redult=operantor.top();
			 operantor.pop();

             interLanguagestack.push(node);
		   }
		   if(cc=='+'||cc=='-'||cc=='*'||cc=='/'){
			   cout<<sign.top()<<"匹配"<<endl;
		      operant.push(sign.top());
			 sign.pop();
		   }

return 1;
		   }    
		 
		   cl=signstack.top(); 
		    m=getid(z,b,l);
			n=getid(cl,b,l);              
		   cn=a[n][m];
		   cout<<"所用到的产生式:"<<cn<<endl;
           if (cn.length()==0)
			return -1;
		   if(cn==" "){
		   signstack.pop();
		   return analysis(z);
		   }  
		   signstack.pop();
           const char *ck=cn.c_str();
		   for(int i=0;i<cn.length();i++)
		   signstack.push(ck[i]);   
		   return analysis(z);
}
void readlineAndanalysis(ifstream& in,ofstream& out){
	string ch,cn;
	char cl;
	int m;
	int n;
	while(in>>ch){
	     //ch=in.get();
		 if (ch=="if"){
		   analysis('i');
		 }
	 if(ch=="1"){
		 in>>ch;
		 in>>ch;
		 sign.push(ch);
		 analysis('I');
		 }
		 if(ch=="2"||ch=="3"){
			  in>>ch;
		 in>>ch;
		  sign.push(ch);
		 analysis('c');
		 }
		 if(ch=="23"||ch=="24"||ch=="25"||ch=="26"||ch=="27"||ch=="29"){
         in>>ch;
		 in>>ch;
		  sign.push(ch);
		  analysis('r');
		 }
		 if(ch=="then"){
			  
	      analysis('t');
		 }
		 if(ch=="XX"){
			 
		   analysis('e');
		 }
		 if(ch==";"){
			 
		   analysis(';');
		 }
		 if(ch=="+"){
			  sign.push(ch);
		   analysis('+');
		 }
		 if(ch=="-"){
			  sign.push(ch);
		  analysis('-');
		 }
		 if(ch=="*"){
			  sign.push(ch);
		   analysis('*');
		 }
		 if(ch=="/"){
			  sign.push(ch);
		   analysis('/');
		 }
          if(ch=="="){
			   sign.push(ch);
		   analysis('=');
		 }
		 
	
	
	}
}
void main()
{
	//for(int i=0;i<=19;i++){
	//	sign[i]="i";}
	signstack.push(';');
    signstack.push('A');
	main_lexical_analysis();
	ifstream in("LEXoutput.txt");
	ofstream out("output.txt");
    readlineAndanalysis(in,out);
    /*while (!signstack.empty()){
	cout<<signstack.top();signstack.pop();}*/
	cout<<endl<<endl;

	deque<InterLanguage*>::iterator iter=InterLanguagequeue.begin();
	int i=1;
	while (iter!=InterLanguagequeue.end()) {
		if((*iter)->link!=NULL){
		 cout<<i<<" ("<<(*iter)->operor<<","<<(*iter)->operndL<<","<<(*iter)->operndR<<","<<(*iter)->link->place<<")"<<endl;
		}else{
		cout<<i<<" ("<<(*iter)->operor<<","<<(*iter)->operndL<<","<<(*iter)->operndR<<","<<(*iter)->redult<<")"<<endl;
		}
		i++;

	  iter++;
	}
/*signstack.push('a');
cout<<signstack.top();
string bu("dgalkjsgdasdjg");
const char *no=bu.c_str();
for(int i=0;i<bu.length();i++)
signstack.push(no[i]);
while (!signstack.empty()){
cout<<signstack.top();signstack.pop();*/


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -