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

📄 ifelse.cpp

📁 该程序能够对if-else进行语法指导的翻译
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype> 
using namespace std;
//------------------------------创建全局成员,声明函数-----------------------------------------------------
struct Node{
	int   type;//保存单词种类
	char  data[20]; //保存单词
	Node* next;    //指向下一单词
};
char words[10];   //保存变量 数字
static int k=0;
Node* head;
char str[256];
ifstream fin("example.txt",ios::in);
int bound(char ch);

int bSym(char ch);
char* copy(Node* p,Node* top);
int count(Node* p,Node* q);
void print_evalue(Node* p,Node* q);
Node* findE(Node* s);
Node* findE_1(Node* s);
char *findG(char *str);
char getC();
int grammar(Node* s);
void init();
void init_gene();
void initMatrix();
int location(Node* p);
int mSym(char ch);
void nSym(char ch);
int oper(char ch);
int operSym(char ch);
char Precede (char op1,char op2);
void print_link(Node *head);
void printE(Node* p,Node* q);
void print_result(Node* s);
int WordAnalysis();
char wordtypeE(Node* s) ;
//----------------------------------------主函数-----------------------------------------------------
int main()
{
	int i;
	init();
	//cout<<"词法分析结果如下"<<endl;
	i=WordAnalysis();
	if(i==1)
	{
		i=grammar(head->next);
	    if(i==1)
		{
		    cout<<"四元式输出:"<<endl;
		    print_result(head->next);
		}
	    else return 0;
	}
	else return 0;
	return 0;
}
//----------------------------------------------函数部分----------------------------------------------


//------------------------------------词法分析--------------------------------------------------
void init()   //初始化文件
{
	int i=0;
	char ch;
	fin.get(ch);
	do
	{
		if((ch!=' ')&&(ch!='\n')){
			str[i++]=ch;
		}
		else 
		{   	fin.get(ch);
			continue;
		}
			fin.get(ch);
	}while(!fin.eof());

	/*while(!fin.eof()){
		fin.get(ch);
		if((ch!=' ')&&(ch!='\n')){
			str[i++]=ch;
		}
		else continue;
	}*/
	str[i]='#';
	cout<<"输入的语句如下:"<<endl;
	cout<<str<<endl;
}
char getC(){          
	char ch=str[k+1];   //取下一字符
	k++;
	return ch;
}

int mSym(char ch){     //标识符
	int i=0;
	while(isalpha((int)ch)||isdigit((int)ch)||ch=='_')
	{
			words[i]=ch;
			words[i+1]='\0';
			if(!strcmp(words,"if"))
			{
				k++;
			    return 3;
			}
		    if(!strcmp(words,"else"))
			{
				k++;
			    return 3;
			}
            if(!strcmp(words,"then"))
			{
				k++;
			   return 3;
			}
			ch=getC();
			i++;
	}
	return 1;
}
void nSym(char ch){   //数字
	int i=0;
	while(isdigit((int)ch)){
			words[i]=ch;
			words[i+1]='\0';
			ch=getC();
			i++;
		}
}
int oper(char ch){
	if(ch=='+'||ch=='-'||ch=='*'||ch=='/')  return 1;
	else if(ch=='<'||ch=='>'||ch=='='||ch=='!') return 2;
	else if(ch=='&'||ch=='|')  return 3;
	return 0;
}
int operSym(char ch){
	if(oper(ch)==1){
		words[0]=ch;
		words[1]='\0';
		ch=getC();
        if(words[0]=='+'){
		    if(ch=='+'){
				words[1]=ch;
			    words[2]='\0';   //cout<<words<<endl;
			    k++;
				return 12;
			}
		}
        if(words[0]=='-'){
			//ch=getC();
			if(ch=='-'){
				words[1]=ch;
			    words[2]='\0';   //cout<<words<<endl;
			    k++;
				return 12;
			}
		}
		return 11;
	}
	if(oper(ch)==2){
		words[0]=ch;
		words[1]='\0';
		ch=getC();
		if(words[0]=='<'){
			//ch=getC();
			if(ch=='='){
				words[1]=ch;
			    words[2]='\0';
				k++;
			    return 22;
			}
		}
        if(words[0]=='>'){
			//ch=getC();
			if(ch=='='){
				words[1]=ch;
			    words[2]='\0';
				k++;
			    return 22;
			}
		}
		if(words[0]=='!'){
			//fin.get(ch);
			if(ch=='='){
				words[1]=ch;
			    words[2]='\0';
				k++;
			    return 22;
			}
			if(ch!='=') return 31;
		} 
		if(words[0]=='='){
			//fin.get(ch);
			if(ch=='='){
				words[1]=ch;
			    words[2]='\0';
				k++;
			    return 22;
			}
			if(ch!='=') return 4;
		}  
		return 21;
	}
    if(oper(ch)==3){
		words[0]=ch;
		words[1]='\0';
		ch=getC();
		if(words[0]=='&'){
			//fin.get(ch);
			if(ch=='&'){
				words[1]=ch;
			    words[2]='\0';
				k++;
				return 32;
			}
		}
	    if(words[0]=='|'){
			//fin.get(ch);
			if(ch=='|'){
				words[1]=ch;
			    words[2]='\0';
				k++;
				return 32;
			}
		}
	    return 0;
	}
	//fin.get(ch);
	//return 0;	
}
int bound(char ch){
	if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch==';'||ch=='#')
		return 1;
	return 0;
}
int bSym(char ch){
	int i=0;
	words[i]=ch;
	words[i+1]='\0';
	k++;
	if(words[0]=='('||words[0]==')') return 51;
	if(words[0]=='{'||words[0]=='}') return 52;
	if(words[0]=='#') return 0;
	return 53;
}
int WordAnalysis()
{
	char ch;
	int i,j;
	//	cout<<"单词种别:#----0,  标识符----1,常数----2,关键字----3,运算符----4,界符----5"<<endl;
	//	cout<<"        算术运算符----41, 关系运算符----42, 逻辑运算符----43, 赋值运算符----44"<<endl;
	//Node *head;
	head=new Node;//可以改到主函数中
	head->next=NULL;
	Node *p,*q;
	p=head;
	  //fin.get(ch);   //cout<<ch<<endl;
	do{
		for(j=0;j<10;j++)
			words[j]='\0';
  		ch=str[k];   //cout<<ch<<endl;
		if(isalpha((int)ch)||ch=='_'){
			i=mSym(ch);
			q=new Node;
			q->type=i;
			strcpy(q->data,words);   //cout<<q->data<<endl;
			q->next=NULL;
			if(head->next==NULL){
				head->next=q;
				p=q;
			}
			else{
				p->next=q;
				p=q;
			}
		}
		else if(isdigit((int)ch)){
			nSym(ch);
			q=new Node;
			q->type=2;
			strcpy(q->data,words);  //cout<<q->data<<endl;
			q->next=NULL;
			if(head->next==NULL){
				head->next=q;
				p=q;
			}
			else{
				p->next=q;
				p=q;
			}
		}
		else if(oper(ch)){
			//operSym();
         	q=new Node;
			i=operSym(ch);
			if(i==11)  q->type=411;
			if(i==12)  q->type=412;
            if(i==21)  q->type=421;
			if(i==22)  q->type=422;
			if(i==31)  q->type=431;
			if(i==32)  q->type=432;
            if(i==4)  q->type=44;
			if(i==0)
			{
				cout<<"词法分析错误!"<<endl;
				return 0;
			}
			strcpy(q->data,words);   //cout<<q->data<<endl;
			q->next=NULL;
			if(head->next==NULL){
				head->next=q;
				p=q;
			}
			else{
				p->next=q;
				p=q;
			}
		}
		else if(bound(ch)){
			q=new Node;
			i=bSym(ch);
			if(i==51)  q->type=51; 
			if(i==52)  q->type=52;
			if(i==53)  q->type=53;
			if(i==0)  q->type=0;
			strcpy(q->data,words);   //cout<<q->data<<endl;
			q->next=NULL;
			if(head->next==NULL){
				head->next=q;
				p=q;
			}
			else{
				p->next=q;
				p=q;
			}
		}
		else
		{
			cout<<"词法分析错误"<<endl;
				return 0;
        }
	}while(ch!='#');
	//print_link(head);
	return 1;
}
void print_link(Node *head){
	Node *r;
	r=head->next;
	while(r->next){
		cout<<setiosflags(ios::left)<<setw(6)<<r->type<<"  "<<r->data<<endl;
		r=r->next;
	}
	cout<<setiosflags(ios::left)<<setw(6)<<r->type<<"  "<<r->data<<endl;
}
//--------------------------------词法分析结束-------------------------------------------------------




//----------------------------------语法分析-------------------------------------------------------
struct Ge
{//表示产生式
	char left[2];//左部
	char right[14];//右部
};
Ge gene[16];
void init_gene(){
	strcpy(gene[0].left,"S");
	strcpy(gene[0].right,"ifBthenAelseA");

    strcpy(gene[1].left,"B");
	strcpy(gene[1].right,"i<E");
	strcpy(gene[2].left,"B");
	strcpy(gene[2].right,"i<=E");
	strcpy(gene[3].left,"B");
	strcpy(gene[3].right,"i==E");
	strcpy(gene[4].left,"B");
	strcpy(gene[4].right,"i!=E");
	strcpy(gene[5].left,"B");
	strcpy(gene[5].right,"i>=E");
	strcpy(gene[6].left,"B");
	strcpy(gene[6].right,"i>E");
	strcpy(gene[7].left,"A");
	strcpy(gene[7].right,"i=E");
    strcpy(gene[8].left,"E");
	strcpy(gene[8].right,"T+E");
	strcpy(gene[9].left,"E");
	strcpy(gene[9].right,"T-E");
	strcpy(gene[10].left,"E");
	strcpy(gene[10].right,"T");
	strcpy(gene[11].left,"T");
	strcpy(gene[11].right,"T*F");
	strcpy(gene[12].left,"T");
	strcpy(gene[12].right,"T/F");
	strcpy(gene[13].left,"T");
	strcpy(gene[13].right,"F");
	strcpy(gene[14].left,"F");
	strcpy(gene[14].right,"i");
	strcpy(gene[15].left,"F");
	strcpy(gene[15].right,"n");
}
char Matrix[23][23];
void initMatrix()                    //--------简单优先关系矩阵---------------
{
	int i,j;
	for(i=0;i<23;i++)
	{
		for(j=0;j<23;j++)
		{Matrix[i][j]=' ';}
	}
	Matrix[0][22]='>'; 
	Matrix[1][10]='=';
	Matrix[2][9]='=';
	Matrix[3][9]='>';   Matrix[3][10]='>';  Matrix[3][22]='>';
	Matrix[4][9]='>';	Matrix[4][10]='>';	Matrix[4][11]='=';	Matrix[4][12]='=';  Matrix[4][13]='=';	
						Matrix[4][14]='=';  Matrix[4][22]='>';
	Matrix[5][9]='>';	Matrix[5][10]='>';	Matrix[5][11]='>';	Matrix[5][12]='>';	Matrix[5][13]='>';
						Matrix[5][14]='>';  Matrix[5][22]='>';
	Matrix[6][9]='>';	Matrix[6][10]='>';	Matrix[6][11]='>';	Matrix[6][12]='>';  Matrix[6][13]='>';
						Matrix[6][14]='>';  Matrix[6][15]='=';	Matrix[6][16]='=';  Matrix[6][17]='=';
						Matrix[6][18]='=';  Matrix[6][19]='=';	Matrix[6][20]='=';  Matrix[6][21]='=';   Matrix[6][22]='>'; 
	Matrix[7][9]='>';	Matrix[7][10]='>';	Matrix[7][11]='>';	Matrix[7][12]='>';  Matrix[7][13]='>';   
						Matrix[7][14]='>';  Matrix[7][22]='>'; 
	Matrix[8][2]='=';	Matrix[8][6]='<';	
	Matrix[9][1]='>';	Matrix[9][6]='<';
	Matrix[10][1]='>';	Matrix[10][6]='<';
	Matrix[11][3]='=';	Matrix[11][4]='<';	Matrix[11][5]='<';	Matrix[11][6]='<';	Matrix[11][7]='<';
	Matrix[12][3]='=';	Matrix[12][4]='<';	Matrix[12][5]='<';	Matrix[12][6]='<';	Matrix[12][7]='<';
	Matrix[13][5]='=';	Matrix[13][6]='<';	Matrix[13][7]='<';
	Matrix[14][5]='=';	Matrix[14][6]='<';	Matrix[14][7]='<';
	Matrix[15][3]='=';	Matrix[15][4]='<';	Matrix[15][5]='<';	Matrix[15][6]='<';	Matrix[15][7]='<';
	Matrix[16][3]='=';	Matrix[16][4]='<';	Matrix[16][5]='<';	Matrix[16][6]='<';	Matrix[16][7]='<';
	Matrix[17][3]='=';	Matrix[17][4]='<';	Matrix[17][5]='<';	Matrix[17][6]='<';	Matrix[17][7]='<';
	Matrix[18][3]='=';	Matrix[18][4]='<';	Matrix[18][5]='<';	Matrix[18][6]='<';	Matrix[18][7]='<';
	Matrix[19][3]='=';	Matrix[19][4]='<';	Matrix[19][5]='<';	Matrix[19][6]='<';	Matrix[19][7]='<';
	Matrix[20][3]='=';	Matrix[20][4]='<';	Matrix[20][5]='<';	Matrix[20][6]='<';	Matrix[20][7]='<';
	Matrix[21][3]='=';	Matrix[21][4]='<';	Matrix[21][5]='<';	Matrix[21][6]='<';	Matrix[21][7]='<';
	Matrix[22][0]='<';	Matrix[22][3]='<';	Matrix[22][4]='<';	Matrix[22][5]='<';	Matrix[22][6]='<';
						Matrix[22][7]='<';	Matrix[22][8]='<';  Matrix[22][22]='=';	
	for(i=0;i<23;i++)
	{
		for(j=0;j<23;j++)
		{//cout<<Matrix[i][j]<<" ";
		}
		//cout<<endl;
	}
}
char *mark[23]={"S","A","B","E","T","F","i","n","if","then","else","+","-","*","/","<","<=","==","!=",">=",">","=","#"};
int location(Node* p)                   //在矩阵中找行列
{
	int i=0;
	char *s,*t;
	s=p->data;
	if(p->type==1) return 6;

⌨️ 快捷键说明

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