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

📄 slr0.cpp

📁 在纯DOS下运行的TurboC3_ucos2_ucgui bug改进版本
💻 CPP
字号:
#include "conio.h"
#include "stdlib.h"
#include "dos.h"
#include "slr0.h"
int i=0;
char InputString[30] ; int IStop=0;
SttsStack StatusStack ; int stas_top=0;
SmblStack SymbolStack ; int syms_top=0;
SymbolType getachar();
void action();
void SLR0_Analyze();
void print()
 { int tmpsta=0,tmpsym=0,tmpis=IStop;
   printf("%d",i++); printf("    ");
   while(StatusStack[++tmpsta]!=0)
   { printf("%d",StatusStack[tmpsta]);
   }
   printf("    ");
   printf("%s",SymbolStack); printf("     ");
   while(InputString[tmpis]!='\0')
   { printf("%c",InputString[tmpis++]);
   }
   printf("\n");
  }
void main()
{ printf("please input a experssion to be analyze(end with '#'):\n");
  gets(InputString);
  SLR0_Analyze();

}
SymbolType getachar()
 { //char ch=*IStop;
   switch (InputString[IStop])
    { case 'i': return element;
      case '+': return add;
      case '*': return times;
      case '(': return lpn;
      case ')': return rpn;
      case '#': return end;
      default :return error;
     }
 }

void action()
 { SymbolType ISnum=getachar();
   char ISchar=InputString[IStop++];
   if (ISnum==error)
    {  printf("the experssion is not a SLR(0) experssion.\n");
       getch();
       exit(1);
    }
   ElementType aa=ActionTable[StatusStack[stas_top]][ISnum];
   switch (aa.act)
    { case 0 :
	  { printf("error the experssion is not a SLR(0) experssion.\n");
	     getch();
	     exit(1);
	  }
     case 1 :
	 { printf("the experssion is a SLR(0) experssion.\n");
	   getch();
	   exit(0);
	  }
	  break;

      case 3 :
	   { StatusStack[++stas_top]=aa.num;
	     SymbolStack[syms_top++]=ISchar;

	   }
	   break;
      case 2 :
	  {  IStop--;
	     switch (aa.num)
	     {
	       case 1 :
		   { StatusStack[stas_top--];
		     StatusStack[stas_top--];
		     StatusStack[stas_top--];
		     SymbolStack[syms_top--];
		     SymbolStack[syms_top--];
		     SymbolStack[syms_top--];

		     StatusStack[++stas_top]=1;
		     SymbolStack[syms_top++]='E';
		    }
		    break;
		case 2 :
		   {
		     StatusStack[stas_top--];
		     SymbolStack[syms_top--];

		     StatusStack[++stas_top]=1;
		     SymbolStack[syms_top++]='E';
		   }
		   break;
		case 3 :
		   { StatusStack[stas_top--];
		     StatusStack[stas_top--];
		     StatusStack[stas_top--];
		     SymbolStack[syms_top--];
		     SymbolStack[syms_top--];
		     SymbolStack[syms_top--];

		     StatusStack[++stas_top]=9;
		     SymbolStack[syms_top++]='T';
		   }
		   break;
		case 4 :
		   { StatusStack[stas_top--];
		     SymbolStack[syms_top--];
		     StatusStack[++stas_top]=2;
		     SymbolStack[syms_top++]='T';
		   }
		   break;
	       case 5 :
		   { StatusStack[stas_top--];
		     StatusStack[stas_top--];
		     StatusStack[stas_top--];
		     SymbolStack[syms_top--];
		     SymbolStack[syms_top--];
		     SymbolStack[syms_top--];

		     StatusStack[++stas_top]=3; // ?   3->R4,10->R3
		     SymbolStack[syms_top++]='F';
		    }
		    break;
		case 6 :
		   {
		     StatusStack[stas_top--];
		     SymbolStack[syms_top--];
		     StatusStack[++stas_top]=3;
		     SymbolStack[syms_top++]='F';  // ?
		   }
		break;
	   default :
	       break;
		}
    }
  }
}
void  SLR0_Analyze()
  { int TorF=ture;
    StatusStack[stas_top]=0;
    while(TorF==ture)
    {
     action();
     print();    //      sym=getachar(IStop);
    }
 }

⌨️ 快捷键说明

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