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

📄 编译原理.cpp

📁 华中科技大学2007编译原理课程设计报告+源代码.rar
💻 CPP
字号:
// 编译原理.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


/****************************************/
/*    算符优先分析程序 */
/****************************************/ 

struct Lchar
{
  char char_ch;
  struct Lchar *next;
}LLchar,*p,*h,*temp,*top,*base;
int table[7][7]={{0,1,1,1,0,1,1},
    {-1,1,0,-1,-1,1,1},
    {-1,-1,0,-1,-1,0,1},
    {-1,1,0,1,-1,1,1},
    {-1,-1,0,-1,-1,0,0}, 
    {0,1,0,1,0,1,1},
	{-1,0,-1,0,0,0,-1}};
//存储算符优先关系表,大于为1,小于或等于为-1,其它为0表示出错
char curchar;
char curcmp;
int right;  /*设置开关项,当出错时为0*/ 
int i,j; 
int k;      /*比较字符在栈的位置*/
void push(char pchar)  /*入栈函数*/ 
{
 temp=(Lchar*)malloc(sizeof(LLchar)); 
 temp->char_ch=pchar; 
 temp->next=top; 
 top=temp;
} 
char pop(void)  /*出栈函数*/ 
{
 char c;
 c=top->char_ch;
 if(top->char_ch!='#') {
	top=top->next; 
 }
 return c;

} 
int changchartoint(char ch)  /*将字符转为数字,以得到算符优先值*/ 
{ 
 int t; 
 switch(ch)
 { 
  case 'i':t=0;break; 
  case '+':t=1;break; 
  case '=':t=2;break;
  case '*':t=3;break; 
  case '(':t=4;break; 
  case ')':t=5; break;
  case '#': t=6;
 } 
 return t; 
}
void guiyue(){
	char tempc;
	char tempc1;
	char tempc2;
	char tempc3;
	char popc;
	tempc =	pop();
	tempc1=pop();
	if(tempc=='i')
	{
		
		if(tempc1=='#')
		{
			push('V');
			k=1;
		}
		else {
				
				switch(tempc1){
				case '+':popc='T';break;
				case '*':popc='F';break;
				case '=':popc='E';
				}
				push(tempc1);
				push(popc);
				k=1;
			}

	}
	else
	{ 
		if(tempc=='F'){
			tempc2=pop();
				if(tempc1=='*'&&tempc2=='T'){
					push('T');
				}
				else
					right=0;
			}
		else if(tempc=='T'){
			tempc2=pop();
			if(tempc1=='+'&&tempc2=='E'){
					push('E');
				}
			else
			right=0;
		}
        else if(tempc=='E'){
			tempc2=pop();
			if(tempc1=='='&&tempc2=='V'){
					push('A');
				}
			else
			right=0;
		}
		else if(tempc=='A'){
			tempc2=pop();
			if(tempc2!='#'){
				right=0;
			}
		}
		else if(tempc=='='){
			tempc2=pop();
			if(tempc1=='V'&&tempc2=='E'){
				push('A');
				
			}
			else right=0;
		}
		
    }			
}
void dosome(void) 
{ 
 k=1;
 for(;;) 
 { 
  curchar=h->char_ch; 
  temp=top;
  if(temp->char_ch=='A'){
	break;
  }
  else {
  for(;;) 
  { 
   if(temp->char_ch=='V'||temp->char_ch=='E'||temp->char_ch=='T'||temp->char_ch=='F'||temp->char_ch=='A'||temp->char_ch=='S') 
   {
    temp=temp->next; 
    k++; 
   } 
   else
   { 
    curcmp=temp->char_ch; 
    break; 
   }
  } 
  printf("\n%d\t%d\t",table[i][j],k); 
  temp=top; 
  for(;;)    /*打印栈*/
  { 
   printf("%c",temp->char_ch); 
   if(temp->char_ch=='#') 
    break;
   else 
    temp=temp->next; 
  } 
  printf("\t");
  temp=h; 
  for(;;)    /*打印待比较的字符*/ 
  { 
   printf("%c",temp->char_ch);
    
   if(temp->char_ch=='#') 
    break; 
   else
    temp=temp->next; 
  } 
  i=changchartoint(curcmp); 
  j=changchartoint(curchar);
  if(table[i][j]==0)  /*算符优先值为空*/ 
  { 
   printf("\n%d\t%d\t%c\t%c\terror1",table[i][j],k,curcmp,curchar); 
   right=0;
   break; 
  } 
  else                 /*算符优先值不为空*/ 
  {
   if(table[i][j]<0)   /*算符优先值为-1,移进*/ 
   { 
    if(curchar=='#')   /*待比较字符为空*/ 
    {
     if(k==2)          /*当前比较字符在栈的位置为两个元素*/ 
      break; 
     else 
     {
      printf("\n%d\t%d\t%c\t%c\terror2",table[i][j],k,curcmp,curchar); 
      right=0; 
      break; 
     }
    } 
    push(curchar); 
    k=1; 
    curcmp=curchar;
    h=h->next; 
   } 
   else              /*算符优先值为1,归约*/ 
   {
    guiyue();
   } 
  }
  }
 } 
} 
void main(void) 
{
 char ch; 
 right=1; 
 base=(Lchar*)malloc(sizeof(LLchar)); 
 base->next=NULL;
 base->char_ch='#'; 
 top=base; 
 h=(Lchar*)malloc(sizeof(LLchar)); 
 h->next=NULL;
 p=h; 
 do{                 /*输入待比较字符串,以'#'结束*/ 
  ch=getchar(); 
  putchar(ch);
   if(ch=='i'||ch=='+'||ch=='='||ch=='*'||ch=='('||ch==')'||ch=='#') 
  { 
   temp=(Lchar*)malloc(sizeof(LLchar)); 
   temp->next=NULL;
   temp->char_ch=ch; 
   h->next=temp; 
   h=h->next; 
  }
  else 
  { 
   temp=p->next; 
   printf("\nInput a wrong char!Input again:\n");
   for(;;) 
   {     
    if (temp!=NULL) 
     printf("%c",temp->char_ch);
    else 
     break; 
    temp=temp->next; 
   }
  } 
 }while(ch!='#');      /*输入待比较字符串,以'#'结束*/ 
 p=p->next; 
 h=p;
 dosome();             /*开始识别*/ 
 if(right) 
  printf("\nOK!\n"); 
 else
  printf("\nError!\n"); 
 getchar(); 
 getchar();
} 


⌨️ 快捷键说明

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