?? cifa.cpp
字號:
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
int start=0; //當前正在檢查的狀態圖的開始狀態標號
int state=0; //當前所在狀態的標號
int token_beginning;//一個token在buf中的開始位置
char c; //用于存儲從buf中提取的一個字符
int forward=0; //當前正在檢查的字符在buf中的位置
#define BSIZE 128 //buf的大小
char buf[BSIZE]; //緩沖區
int bufend=0;
//輸入某一程序段所占buf的大小
#define keyword 1
#define id 2 //基本保留字、標識符
#define num 3 //常數
#define relation 4 //關系運算符
#define operation 4 //算術運算符
#define separation 5 //分隔符
int KEY_lenth=17;
char * keywords[]= { //關鍵字
"int","char","float","double","bool","void",
"procedure","call","begin","end",
"if","then","while","do","var","odd","const"
};
char nextchar() //取下一個字符;
{
return buf[forward++];
}
void recover(){ //錯誤恢復,向前指針前移
forward++;
}
void retract(int i){ //向前指針回退
forward-=i;
}
void install_id(){}; //將id寫入符號表,還未定義
void install_num(){}; //將num寫入符號表,還未定義
int fail() //將start置為下一狀態圖的開始狀態,用下一狀態圖識別此token。
{
forward=token_beginning;
switch(start){
case 0:
start=9;
break;
case 9:
start=12;
break;
case 12:
start=20;
break;
case 20:
start=25;
break;
case 25:
start=28;
break;
case 28:
start=30;
break;
case 30:
recover();
start=-1;
break;
default:
;
}
return start;
}
int nexttoken() //識別下一個詞素,根據狀態轉換圖編寫。
{
start=state=0;
token_beginning=forward;
while(1){
switch(state){
case 0: //0~8,識別關系運算符。
c=nextchar();
if(c==' '||c=='\t'||c=='\n'){
state=0;
token_beginning++;
}
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:
return(4);
case 3:
return(4);
case 4:
retract(1);
return(4);
case 5:
return(4);
case 6:
c=nextchar();
if(c=='=')
state=7;
else
state=8;
break;
case 7:
return(4);
case 8:
retract(1);
return(4);
case 9:
c=nextchar();
if(isalpha(c))
state=10;
else
state=fail();
break;
case 10:
c=nextchar();
if(isalpha(c))
state=10;
else if(isdigit(c))
state=10;
else state=11;
break;
case 11:
retract(1);
//install_id();
return(2);
case 12: //12~19,識別形如1.2E+123的數。
c=nextchar();
if(isdigit(c))
state=13;
else
state=fail();
break;
case 13:
c=nextchar();
if(isdigit(c))
state=13;
else if(c=='.')
state=14;
else if(c=='E')
state=16;
else
state=fail();
break;
case 14:
c=nextchar();
if(isdigit(c))
state=15;
else
state=fail();
break;
case 15:
c=nextchar();
if(isdigit(c))
state=15;
else if(c=='E')
state=16;
else
state=fail();
break;
case 16:
c=nextchar();
if(c=='+'||c=='-')
state=17;
else if(isdigit(c))
state=18;
else
state=fail();
break;
case 17:
c=nextchar();
if(isdigit(c))
state=18;
else
state=fail();
break;
case 18:
c=nextchar();
if(isdigit(c))
state=18;
else
state=19;
break;
case 19:
retract(1);
///install_num();
return(3);
case 20: //20~24,識別形如1.234的數。
c=nextchar();
if(isdigit(c))
state=21;
else
state=fail();
break;
case 21:
c=nextchar();
if(isdigit(c))
state=21;
else if(c=='.')
state=22;
else
state=fail();
break;
case 22:
c=nextchar();
if(isdigit(c))
state=23;
else
state=fail();
break;
case 23:
c=nextchar();
if(isdigit(c))
state=23;
else
state=24;
break;
case 24:
retract(1);
//install_num();
return(3);
case 25: //25~27,識別形如123的數。
c=nextchar();
if(isdigit(c))
state=26;
else
state=fail();
break;
case 26:
c=nextchar();
if(isdigit(c))
state=26;
else
state=27;
break;
case 27:
retract(1);
return(3);
case 28: //28~29,識別運算符。
c=nextchar();
if(c=='+'||c=='-'||c=='*'||c=='/'||
c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}')
state=29;
else
state=fail();
break;
case 29:
return(5);
case 30: //30~31,識別分隔符。
c=nextchar();
if(c==','||c==';')
state=31;
else
state=fail();
break;
case 31:
return(5);
default:
return 0;
}
}
}
void judge_print(int type) //根據type,選擇輸出。
{
int i,j=0;
char str[30]; //將詞素保存在str數組中。
for(i=token_beginning;i<forward;i++)
str[j++]=buf[i];
str[j]=0;
if(type==id){
for(int i=0;i<KEY_lenth;i++) //判斷id是否是關鍵字。
if(strcmp(str,keywords[i])==0){
cout<<"[1,"<<str<<']'<<endl;
fout<<"[1,"<<str<<']'<<endl;
break;
}
if(i==KEY_lenth){ //沒有關鍵字與id匹配
cout<<"[2,"<<str<<']'<<endl;
fout<<"[2,"<<str<<']'<<endl;
}
}
else if(type==num){
cout<<"[3,"<<str<<']'<<endl;
fout<<"[3,"<<str<<']'<<endl;
}
else if(type==relation||type==operation){
cout<<"[4,"<<str<<']'<<endl;
fout<<"[4,"<<str<<']'<<endl;
}
else if(type==separation){
cout<<"[5,"<<str<<']'<<endl;
fout<<"[5,"<<str<<']'<<endl;
}
}
int main()
{
string c;
int i;
while(fin>>c){ //將輸入保存在buf數組中。
for(i=0;c[i];i++)
buf[bufend++]=c[i];
buf[bufend++]=' ';
}
int type;
while(forward<bufend){ //識別、打印每個token,直到buf結尾
type=nexttoken();
judge_print(type);
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -