?? slr0.cpp
字號(hào):
#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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -