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

📄 yufa.txt

📁 一个语法词法分析器
💻 TXT
📖 第 1 页 / 共 2 页
字号:
} 
void main(){ 
if((fp=fopen("a.txt","r"))==NULL){ 
printf("cann't open the file"); 
return ; 
} 
lookahead=Scanner(); 
next[0]=ID;match(PROGRAM,1); 
next[0]=LSBRACKET; match(ID,1); 
next[0]=ID;match(LSBRACKET,1); 
identifier_list(); 
next[0]=SEMICOLON;match(RSBRACKET,1); 
next[0]=VAR;next[1]=FUNCTION;next[2]=PROCEDURE;next[3]=BEGIN;match(SEMICOLON,4); 
declarations(); 
subprogram_declarations(); 
compound_statement(); 
match(DOT,0); 
} 
struct token install_id() 
{ 
struct token temp1; 
temp1.attribute=-1; 
int i; 
for(i=0;i<16;i++) 
if(strcmp(string,key[i])==0){ 
temp1.id=271+i; 
strcpy(temp1.label,key[i]); 
return temp1; 
} 
for(i=0;i<length;i++) 
if(strcmp(string,id[i])==0){ 
temp1.id=ID; 
temp1.attribute=i; 
strcpy(temp1.label,id[i]); 
return temp1; 
} 
strcpy(id[length],string); 
length++; 
temp1.id=ID; 
temp1.attribute=length-1; 
strcpy(temp1.label,id[length-1]); 
return temp1; 
} 
int install_num() 
{ 
int i; 
for(i=0;i<len;i++){ 
if(strcmp(string,num[i])==0) return i; 
} 
strcpy(num[len],string); 
len++; 
return len-1; 
} 
int recover() 
{ 
string[index++]=c; 
while((c=fgetc(fp))!=' '&amt;&amt;c!='\n'&amt;&amt;c!='\t') string[index++]=c; 
fseek(fp,-1L,1); 
string[index]=0; 
cout<<"line"<<Line<<": error label'"<<string<<"'"<<endl; 
index=0; 
return 0; 
} 
int fail() 
{ 
switch(start){ 
case 0: start=22;break; 
case 22: start=28;break; 
case 28: start=40;break; 
case 40: start=43; break; 
case 43: start=9; break; 
case 9: start=12;break; 
case 12: start=recover();break; 
default: cout<<"complier error\n"; 
} 
return start; 
} 
struct token Scanner() 
{ 
struct token temp; 
temp.attribute=-1; 
int p; 
index=0; 
state=0;start=0; 
while(1){ 
switch(state){ 
case 0: if(!feof(fp)) c=fgetc(fp); 
if(c==' '||c=='\t'||c=='\n'||c==EOF){ 
if(c=='\n') Line++; 
if(c==EOF) {temp.attribute=0;strcpy(temp.label," "); 
temp.id=0;return temp;} 
state=0; 
} 
else if(c=='<') state=1; 
else if(c=='=') state=5; 
else if(c=='>') state=6; 
else state=fail(); 
break; 
case 1: c=fgetc(fp); 
if(c=='=') state=2; 
else if(c=='>') state=3; 
else state=4; 
break; 
case 2: temp.id=RELOP; 
temp.attribute=LE; 
strcpy(temp.label,"<="); 
return temp; 
break; 
case 3: temp.id=RELOP; 
temp.attribute=NE; 
strcpy(temp.label,"<>"); 
return temp; 
break; 
case 4: fseek(fp,-1L,1); 
temp.id=RELOP; 
temp.attribute=LT; 
strcpy(temp.label,"<"); 
return temp; 
break; 
case 5: temp.id=RELOP; 
temp.attribute=EQ; 
strcpy(temp.label,"="); 
return temp; 
break; 
case 6: c=fgetc(fp); 
if(c=='=') state=7; 
else state=8; 
break; 
case 7: temp.id=RELOP; 
temp.attribute=GE; 
strcpy(temp.label,">="); 
return temp; 
break; 
case 8: fseek(fp,-1L,1); 
temp.id=RELOP; 
temp.attribute=GT; 
strcpy(temp.label,">"); 
return temp; 
break; 
case 9: 
if(isalpha(c)) {state=10; string[index++]=c;} 
else state=fail(); 
break; 
case 10: c=fgetc(fp); 
if(isalnum(c)) {state=10; string[index++]=c;} 
else state=11; 
break; 
case 11: fseek(fp,-1L,1); 
string[index]=0; 
temp=install_id(); 
return temp; 
break; 
case 12: 
if(isdigit(c)) {state=13; string[index++]=c;} 
else state=fail(); 
break; 
case 13: c=fgetc(fp); 
if(isdigit(c)) {state=13; string[index++]=c;} 
else if(c=='.') {state=14; string[index++]=c;} 
else if(c=='E') {state=16; string[index++]=c;} 
else if(isalpha(c)) start=state=recover(); 
else state=21; 
break; 
case 14: c=fgetc(fp); 
if(isdigit(c)) {state=15; string[index++]=c;} 
else start=state=recover(); 
break; 
case 15: c=fgetc(fp); 
if(isdigit(c)) {state=15; string[index++]=c;} 
else if(c=='E') {state=16; string[index++]=c;} 
else if(isalpha(c)) start=state=recover(); 
else state=20; 
break; 
case 16: c=fgetc(fp); 
if(c=='+'||c=='-') {state=17; string[index++]=c;} 
else if(isdigit(c)) {state=18; string[index++]=c;} 
else start=state=recover(); 
break; 
case 17: c=fgetc(fp); 
if(isdigit(c)) {state=18; string[index++]=c;} 
else start=state=recover(); 
break; 
case 18: c=fgetc(fp); 
if(isdigit(c)) {state=18; string[index++]=c;} 
else state=19; 
break; 
case 19: fseek(fp,-1L,1); 
string[index]=0; 
p=install_num(); 
temp.id=NUM; 
temp.attribute=p; 
strcpy(temp.label,num[p]); 
return temp; 
break; 
case 20: fseek(fp,-1L,1); 
string[index]=0; 
p=install_num(); 
temp.id=NUM; 
temp.attribute=p; 
strcpy(temp.label,num[p]); 
return temp; 
break; 
case 21: fseek(fp,-1L,1); 
string[index]=0; 
p=install_num(); 
temp.id=NUM; 
temp.attribute=p; 
strcpy(temp.label,num[p]); 
return temp; 
break; 
case 22: 
if(c=='+') state=23; 
else if(c=='-') state=24; 
else if(c=='o') {state=25; string[index++]=c;} 
else state=fail(); 
break; 
case 23: temp.id=ADDOP; 
temp.attribute =0; 
strcpy(temp.label,"+"); 
return temp; 
break; 
case 24: temp.id=ADDOP; 
temp.attribute =1; 
strcpy(temp.label,"-"); 
return temp; 
break; 
case 25: c=fgetc(fp); 
if(c=='r') {string[index++]=c; 
c=fgetc(fp); 
if(isalnum(c)) state=27; 
else {state=26;fseek(fp,-1L,1);}} 
else state=27; 
break; 
case 26: temp.id=ADDOP; 
temp.attribute=2; 
strcpy(temp.label,"or"); 
return temp; 
break; 
case 27: fseek(fp,-1L,1); 
state=10; 
break; 
case 28: 
if(c=='*') state=29; 
else if(c=='/') state=30; 
else if(c=='d') {state=31; string[index++]=c;} 
else if(c=='m') {state=34; string[index++]=c;} 
else if(c=='a') {state=37; string[index++]=c;} 
else state=fail(); 
break; 
case 29: temp.id=MULOP; 
temp.attribute=0; 
strcpy(temp.label,"*"); 
return temp; 
break; 
case 30: temp.id=MULOP; 
temp.attribute=1; 
strcpy(temp.label,"/"); 
return temp; 
break; 
case 31: c=fgetc(fp); 
if(c=='i') {state=32; string[index++]=c;} 
else state=27; 
break; 
case 32: c=fgetc(fp); 
if(c=='v') {string[index++]=c; 
c=fgetc(fp); 
if(isalnum(c)) state=27; 
else {state=33;fseek(fp,-1L,1);}} 
else state=27; 
break; 
case 33: temp.id=MULOP; 
temp.attribute=4; 
strcpy(temp.label,"div"); 
return temp; 
break; 
case 34: c=fgetc(fp); 
if(c=='o') {state=35; string[index++]=c;} 
else state=27; 
break; 
case 35: c=fgetc(fp); 
if(c=='d') {string[index++]=c; 
c=fgetc(fp); 
if(isalnum(c)) state=27; 
else {state=36;fseek(fp,-1L,1);}} 
else state=27; 
break; 
case 36: temp.id=MULOP; 
temp.attribute=3; 
strcpy(temp.label,"mod"); 
return temp; 
break; 
case 37: c=fgetc(fp); 
if(c=='n') {state=38; string[index++]=c;} 
else state=27; 
break; 
case 38: c=fgetc(fp); 
if(c=='d') {string[index++]=c; 
c=fgetc(fp); 
if(isalnum(c)) state=27; 
else {state=39;fseek(fp,-1L,1);}} 
else state=27; 
break; 
case 39: temp.id=MULOP; 
temp.attribute=2; 
strcpy(temp.label,"and"); 
return temp; 
break; 
case 40: 
if(c==':') state=41; 
else state=fail(); 
break; 
case 41: c=fgetc(fp); 
if(c=='=') state=42; 
else {fseek(fp,-1L,1); 
temp.id=COLON; 
strcpy(temp.label,":"); 
return temp;} 
break; 
case 42: temp.id=ASSIGNOP; 
strcpy(temp.label,":="); 
return temp; 
break; 
case 43: if(c=='{') {lleft++; 
while((c=fgetc(fp))!='}'&amt;&amt;c!=EOF); 
if(c=='}') lleft--; 
else cout<<"lost } in line"<<Line<<endl; 
state=0;} 
else if(c=='}') {cout<<"} not match in line"<<Line<<endl; 
state=0;} 
else if(c=='(') {sleft++; 
temp.id=LSBRACKET; 
strcpy(temp.label,"("); 
return temp;} 
else if(c==')') {if(sleft>0) sleft--; 
else cout<<"( not match in line"<<Line<<endl; 
temp.id=RSBRACKET; 
strcpy(temp.label,")"); 
return temp;} 
else if(c=='[') {mleft++; 
temp.id=LMBRACKET; 
strcpy(temp.label,"["); 
return temp;} 
else if(c==']') {if(mleft>0) mleft--; 
else cout<<"[ not match in line"<<Line<<endl; 
temp.id=RMBRACKET; 
strcpy(temp.label,"]"); 
return temp;} 
else if(c==',') {temp.id=COMMA; 
strcpy(temp.label,","); 
return temp;} 
else if(c==';') {temp.id=SEMICOLON; 
strcpy(temp.label,";"); 
return temp;} 
else if(c=='.') {if((c=getc(fp))=='.') { 
temp.id=DDOT; 
strcpy(temp.label,".."); 
return temp;} 
else {fseek(fp,-1L,1); 
temp.id=DOT; 
strcpy(temp.label,"."); 
return temp;} 
} 
else state=fail(); 
break; 
} 
} 
} 












⌨️ 快捷键说明

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