📄 main.c
字号:
#include "global.h"
#include <string.h>
int state=0,start=0,i=-1,j=0,x=0,y=0,z,choice1=1,choice2,change=0,elseif[5],b=0,d,e=0,f=0;
char a[100],lexbuf[100],c,BSIZE[15],S[100],bb[5],aa[100];
char nextchar();int fail();int nexttoken();char retract();int install_id();
void Getstring(int i,int j);
void stmt();
void expr();void expr1();
void term();void term1();
void list();void list1();
void factor();void ERROR();
void main()
{
//strcat(bb,"ELSE");
init();
/* printf("normal expression:\n");
printf("List -> List ; stmt | stmt\n");
printf("Stmt -> if expr then stmt\n| if expr then stmt else stmt\n| while expr do stmt\n| begin list end\n| id := expr\n");
printf("expr -> expr + term |term\nterm -> term * factor\nfactor -> (expr) | id | num\n");
printf("example:\n");
printf("begin if(a+b*c) then a:=c else while a+b do c:=124 end\n");
printf("please input:\n");*/
gets(a);
while(a[i+1]!='\0')
{
state=0;start=0;
memset(lexbuf,'\0',100);
j=0;
nexttoken();
}
list();
printf("\n\nTrue!\n");
getch();
}
void stmt()
{
Getstring(y,y+2);
if(change==1)
{
}
else
{
if(strcmp("IF",BSIZE)==0)
{
z=0;
if(choice1==1)
{
elseif[0]=y;
}
if(choice1==2)
{
elseif[0]=y-elseif[0];
elseif[1]=y;
}
if(choice1==3)
{
elseif[1]=y-elseif[1];
elseif[2]=y;
}
if(choice1==4)
{
elseif[2]=y-elseif[2];
elseif[3]=y;
}
if(choice1==5)
{
elseif[3]=y-elseif[3];
elseif[4]=y;
}
if(choice1==5)
{
elseif[4]=y-elseif[4];
}
choice1++;
printf("\nStmt->if expr then stmt ");
printf("\n\n%s\n",BSIZE);
y=y+2;
z=z+2;
ERROR();
//printf(" %d",y);
expr();
Getstring(y,y+4);
printf("\n\n%s\n",BSIZE);
if(strcmp("THEN",BSIZE)==0)
{
y=y+4;
z=z+4;
ERROR();
//printf(" %d",y);
stmt();
Getstring(y,y+4);
if(strcmp("ELSE",BSIZE)==0)
{
choice2++;
change=1;
stmt();
}
}
else
{
printf("\nError!\n");
exit(0);
}
}
}
if(change==1)
{
change=2;
if(choice2==1)
{
y=y-z;
}
if(choice2==3)
{
y=y-z-elseif[0];
}
if(choice2==6)
{
y=y-z-elseif[0]-elseif[1];
}
if(choice2==10)
{
y=y-z-elseif[0]-elseif[1]-elseif[2];
}
if(choice2==15)
{
y=y-z-elseif[0]-elseif[1]-elseif[2]-elseif[3];
}
if(choice2==21)
{
y=y-z-elseif[0]-elseif[1]-elseif[2]-elseif[3]-elseif[4];
}
choice2++;
z=0;
Getstring(y,y+2);
if(strcmp("IF",BSIZE)==0)
{
printf("\nStmt->if expr then stmt else stmt");
y=y+2;
z=z+2;
ERROR();
//printf(" %d",y);
expr();
Getstring(y,y+4);
if(strcmp("THEN",BSIZE)==0)
{
if(choice2==4||choice2==7||choice2==8||choice2==11||choice2==12||choice2==13||choice2==16||choice2==17||choice2==18
||choice2==19||choice2==22||choice2==23||choice2==24||choice2==25||choice2==26)
{
change=1;
}
y=y+4;
z=z+4;
ERROR();
//printf(" %d",y);
stmt();
if(e==1)
{
change=3;
f=1;
}
Getstring(y,y+4);
if(strcmp("ELSE",BSIZE)==0)
{
if(f==0)
{
for(d=y+4;d<strlen(S);d++,b=0)
{
while(d<strlen(S) && b<strlen(bb))
{
if(S[d]==bb[b]){++d;++b;}
else{d=d-b+1;b=0;}
}
if(b >= strlen(bb))
{
e++;
}
else
{
change=3;
}
}
}
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+4;
z=z+4;
ERROR();
//printf(" %d",y);
stmt();
if(e==1)
{
change=3;
f=1;
}
if(f==0)
{
e=0;
}
}
}
else
{
printf("\nError!\n");
exit(0);
}
}
else
{
printf("\nError!\n");
exit(0);
}
}
else if(strcmp("WH",BSIZE)==0)
{
Getstring(y,y+5);
if(strcmp("WHILE",BSIZE)==0)
{
printf("\nStmt->while expr do stmt");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+5;
z=z+5;
ERROR();
//printf(" %d",y);
expr();
Getstring(y,y+2);
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
if(strcmp("DO",BSIZE)==0)
{
y=y+2;
z=z+2;
ERROR();
//printf(" %d",y);
stmt();
}
}
}
if(strcmp("BE",BSIZE)==0)
{
Getstring(y,y+5);
if(strcmp("BEGIN",BSIZE)==0)
{
printf("\n\n%s\n",BSIZE);
y=y+5;
z=z+5;
ERROR();
//printf(" %d",y);
printf("\nStmt->begin list end");
list();
Getstring(y,y+3);
if(strcmp("END",BSIZE)==0)
{
printf("\n\n%s\n",BSIZE);
y=y+3;
z=z+3;
//printf(" %d",y);
}
else
{
printf("\nError!\n");
exit(0);
}
}
}
else if(strcmp("ID",BSIZE)==0)
{
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+2;
z=z+2;
ERROR();
//printf(" %d",y);
printf("\nStmt->id:=expr");
Getstring(y,y+1);
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
if(strcmp(":",BSIZE)==0)
{
y=y+1;
z=z+1;
//printf(" %d",y);
ERROR();
Getstring(y,y+1);
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
if(strcmp("=",BSIZE)==0)
{
y=y+1;
z=z+1;
//printf(" %d",y);
ERROR();
expr();
}
else
{
printf("\nError!\n");
exit(0);
}
}
else
{
printf("\nError!\n");
exit(0);
}
}
else if(strcmp("NU",BSIZE)==0)
{
printf("\nError!\n");
exit(0);
}
else if(strcmp("RE",BSIZE)==0)
{
printf("\nError!\n");
exit(0);
}
}
void expr()
{
printf("\nexpr->term expr1");
term();expr1();
}
void expr1()
{
Getstring(y,y+1);
if(strcmp("+",BSIZE)==0)
{
printf("\nexpr1->+term expr1");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+1;
z=z+1;
ERROR();
//printf(" %d",y);
term();
expr1();
}
else
{
printf("\nexpr1->E");
}
}
void term()
{
printf("\nterm->factor term1");
factor();term1();
}
void term1()
{
Getstring(y,y+1);
if(strcmp("*",BSIZE)==0)
{
printf("\nterm1->*factor term1");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+1;
z=z+1;
ERROR();
//printf(" %d",y);
factor();
term1();
}
else
{
printf("\nterm1->E");
}
}
void factor()
{
Getstring(y,y+1);
if(strcmp("(",BSIZE)==0)
{
printf("\nfactor->(expr)");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+1;
z=z+1;
ERROR();
//printf(" %d",y);
expr();
Getstring(y,y+1);
if(strcmp(")",BSIZE)==0)
{
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+1;
z=z+1;
//printf(" %d",y);
}
else
{
printf("\nError!\n");
exit(0);
}
}
else if(strcmp("I",BSIZE)==0)
{
Getstring(y,y+2);
if(strcmp("ID",BSIZE)==0)
{
printf("\nfactor->id");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+2;
z=z+2;
//printf(" %d",y);
}
}
else if(strcmp("N",BSIZE)==0)
{
Getstring(y,y+3);
if(strcmp("NUM",BSIZE)==0)
{
printf("\nfactor->num");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+3;
z=z+3;
//printf(" %d",y);
}
}
}
void list()
{
printf("\nList->stmt list1");
stmt();list1();
}
void list1()
{
Getstring(y,y+1);
if(strcmp(";",BSIZE)==0)
{
printf("\nList1->;stmt list1");
if(change==2)
{
}
else
{
printf("\n\n%s\n",BSIZE);
}
y=y+1;
z=z+1;
ERROR();
//printf(" %d",y);
stmt();list1();
}
else
{
printf("\nList1->E");
}
}
void Getstring(int i,int j)
{
int z=0;
memset(BSIZE,'\0',15);
for(i;i<j;i++)
{
BSIZE[z]=S[i];
z++;
}
}
void ERROR()
{
if(S[y+1]=='\0')
{
printf("\nError!\n");
exit(0);
}
}
void gettoken(int z)
{
switch(z)
{
case 1: case 2:
strcat(S,"IF");
printf(" IF");break;
//printf("IF\n\n");break;
case 3: case 4:
strcat(S,"THEN");
printf(" THEN");break;
//printf("THEN\n\n");break;
case 5: case 6:
strcat(S,"ELSE");
printf(" ELSE");break;
//printf("ELSE\n\n");break;
case 7: case 8:
strcat(S,"WHILE");
printf(" WHILE");break;
//printf("WHILE\n\n");break;
case 9: case 10:
strcat(S,"DO");
printf(" DO");break;
//printf("DO\n\n");break;
case 11: case 12:
strcat(S,"BEGIN");
printf(" BEGIN");break;
//printf("BEGIN\n\n");break;
case 13: case 14:
strcat(S,"END");
printf(" END");break;
//printf("END\n\n");break;
case 15:
printf(" AVA");break;
default:
strcat(S,"ID");
printf(" ID");break;
//printf("ID\n\n");
}
}
char nextchar()
{
i++;
return a[i];
}
char retract()
{
i--;
return a[i];
}
int fail()
{
i--;
switch(start){
case 0 : start = 9 ;break;
case 9 : start = 16;break;
case 16: start = 19;break;
case 19: start = 27;break;
case 27: start = 32;break;
case 32: start = 35;break;
default: break;//printf("error!");
//编译错误
}
return start;
}
int nexttoken()
{ while(1){
switch(state){
case 0: c = nextchar();
// c是超前扫描字符
if(c==' ' || c=='\t' || c=='\n'){
state = 0;
//词素开始指针的前移
}
else if (c=='<') state = 1;
else if (c=='=') state = 5;
else if (c=='>') state = 6;
else state = fail();
break;
case 1: c = nextchar();
if (c=='=') state = 2;
else if (c=='>') state = 3;
else state = 4;
break;
case 2:
strcat(S,"RELOP");
printf(" RELOP");
//printf("<=\nLE\n\n");
return RELOP;
case 3:
strcat(S,"RELOP");
printf(" RELOP");
//printf("<>\nNE\n\n");
return RELOP;
case 4:
retract();
strcat(S,"RELOP");
printf(" RELOP");
//printf("LT<\n\n");
return RELOP;
case 5:
strcat(S,"=");
printf(" = =");
//printf("=\nEQ\n\n");
return RELOP;
case 6: c = nextchar();
if ( c=='=') state = 7;
else state = 8;
break;
case 7:
strcat(S,"RELOP");
printf(" RELOP");
//printf(">=\nGE\n\n");
return RELOP;
case 8:
retract();
strcat(S,"RELOP");
printf(" RELOP");
//printf(">\nGT\n\n");
return RELOP;
case 9: c=nextchar();
if(c=='+') state =10;
else if (c=='*') state =11;
else if (c=='(') state =12;
else if (c==')') state =13;
else if (c==';') state =14;
else if (c==':') state =15;
else state = fail();
break;
case 10:
strcat(S,"+");
printf(" + +");
return ADD;
case 11:
strcat(S,"*");
printf(" * *");
return MULTIPLY;
case 12:
strcat(S,"(");
printf(" ( (");
return LEFTBRACKET;
case 13:
strcat(S,")");
printf(" ) )");
return RIGHTBRACKET;
case 14:
strcat(S,";");
printf(" ; ;");
return SEMICOLON;
case 15:
strcat(S,":");
printf(" : :");
return COLON;
case 16: c=nextchar();
if (isalpha(c))
{
lexbuf[j]=c;
j++;
state = 17;
}
else
{
state = fail();
}
break;
case 17: c=nextchar();
if (isalpha(c))
{
lexbuf[j]=c;
j++;
state = 17;
}
else if (isdigit(c))
{
lexbuf[j]=c;
j++;
state = 17;
}
else
{
state = 18;
}
break;
case 18:
retract();
printf(" %s",lexbuf);
lookup(lexbuf);
return (gettoken(lookup(lexbuf)));
case 19: c = nextchar();
if (isdigit(c))
{
memset(lexbuf,'\0',100);
j=0;
lexbuf[j]=c;
j++;
x=0;
x++;
state = 20;
}
else
{
i=i-x;
state = fail();
}
break;
case 20: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
x++;
state = 20;
}
else if (c=='.')
{
lexbuf[j]=c;
j++;
x++;
state =21;
}
else if (c=='E')
{
lexbuf[j]=c;
j++;
x++;
state = 23;
}
else
{
i=i-x;
state = fail();
}
break;
case 21: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
x++;
state = 22;
}
else
{
i=i-x;
state = fail();
}
break;
case 22: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
x++;
state = 22;
}
else if (c=='E')
{
lexbuf[j]=c;
j++;
state = 23;
}
else
{
i=i-x;
state = fail();
}
break;
case 23: c = nextchar();
if (c=='+' || c=='-')
{
lexbuf[j]=c;
j++;
state = 24;
}
else if (isdigit(c))
{
lexbuf[j]=c;
j++;
state = 25;
}
else
{
state = fail();
}
break;
case 24: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
state = 25;
}
else
{
state = fail();
}
break;
case 25: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
state = 25;
}
else
{
state = 26;
}
break;
case 26:
retract();
x=0;
strcat(S,"NUM");
printf(" NUM");
printf(" %s",lexbuf);
return NUM;
case 27: c = nextchar();
if (isdigit(c))
{
memset(lexbuf,'\0',100);
j=0;
lexbuf[j]=c;
j++;
x=0;
x++;
state = 28;
}
else
{
i=i-x;
state = fail();
}
break;
case 28: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
x++;
state = 28;
}
else if (c=='.')
{
lexbuf[j]=c;
j++;
x++;
state = 29;
}
else
{
i=i-x;
state = fail();
}
break;
case 29: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
x++;
state = 30;
}
else
{
state = fail();
}
break;
case 30: c = nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
x++;
state = 30;
}
else
{
state = 31;
}
break;
case 31:
retract();
x=0;
strcat(S,"NUM");
printf(" %s",lexbuf);
return NUM;
case 32: c=nextchar();
if (isdigit(c))
{
memset(lexbuf,'\0',100);
j=0;
lexbuf[j]=c;
j++;
state = 33;
}
else
{
state = fail();
}
break;
case 33: c=nextchar();
if (isdigit(c))
{
lexbuf[j]=c;
j++;
state = 33;
}
else
{
state = 34;
}
break;
case 34:
retract();
strcat(S,"NUM");
printf(" %s",lexbuf);
return NUM;
case 35: c = nextchar();
printf(" %c\nError!\n",c);
exit(0);
return 0;
default:
printf("error");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -