#include#includeusing namespace std;
//保留字表
static char reserve[32][20] = ;
//界運算子表
static char operatorlimit[36][10] = ", "\\", ".", "\?", ":", "!"
};char idenoperator[10000][50] = ;
/********查詢保留字*****************/
int searchrev(char reserveword[20], char s)
}return -1;//否則返回-1,代表查詢不成功,即為識別符號
}/*編譯預處理*/
void filter(char r,int source)
} if(r[i] =='/'&&r[i+1] == '*')
}i+=2;
} if(r[i] != '\n'&&r[i] !='\t'&&r[i]!='\v'&&r[i]!='\r')
cmptstring[count++] = r[i];
} cmptstring[count] = '\0';
strcpy(r,cmptstring);
}//掃瞄乙個單詞或者符號
void scan(int &lab, char r, char getstring, int &source)
for(int i = 0 ; i < 50 ; i++)
if(isalpha(r[source]))
getstring[count] = '\0';
lab = searchrev(reserve,getstring);
if(lab == -1)
return;
} else if(isdigit(r[source]))
getstring[count] = '\0';
lab = 99;
} else if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == ';' || ch == '(' || ch == ')' || ch == '^'
|| ch == ',' || ch == '\"' || ch == '\'' || ch == '~' || ch == '#' || ch == '%' || ch == '['
|| ch == ']' || ch == '' || ch == '\\' || ch == '.' || ch == '\?' || ch == ':')
} source++;
return;
} else if(r[source] =='<')
else if(r[source] == '<')
else
source++;
return;
} else if(r[source] =='>')
else if(r[source] == '>')
else
source++;
return;
} else if(r[source] == '=')
else
source++;
return;
} else if(r[source] =='!')
else
source++;
return;
} else if(r[source] == '&')
else
source++;
return;
}else if (r[source] == '|')
else
source++;
return;
}else if (r[source] == '$')
else
}int main()
; int lab = -1,i;//初始化
int source = 0;//源程式指標
file *fp,*fp1,*fp2;
if ((fp = fopen("c:\\in.txt", "r")) == null)
resourcestring[source] = fgetc(fp);
while(resourcestring[source] != '$')
fclose(fp);
resourcestring[++source] = '\0';
//過濾
filter(resourcestring,source);
cout << endl << "過濾之後的程式:" << endl;
cout << resourcestring << endl;
if((fp1 = fopen("c:","w+")) == null)
source = 0;
while(lab!=0)
if (strcmp(idenoperator[i], "") == 0)
}printf("(%3d ,%s)\n",lab, getstring);
fprintf(fp1, "(%3d ,%s)\n",lab, getstring);
}else if(lab >= 1 && lab <= 32)
else if (lab == 99)
else if (lab >= 33 && lab <= 68)
} fclose(fp1);
if ((fp2 = fopen("c:\\out2.txt", "w+")) == null)
int num = 0;
for(int i = 0 ; i < 100 ; i++)
for (i = 0; ifclose(fp2);
return 0;
}
C 實現詞法分析器
written by zzg date 11,25,2017 include include include using namespace std string keyword 15 char separater 8 分隔符 char operator 8 運算子 char filter 4 過濾...
C 詞法分析器
編譯原理實驗 一 實驗題目 設計 編制 除錯乙個識別一簡單語言單詞的詞法分析程式。程式能夠識別基本字 識別符號 無符號整數 浮點數 運算子和界符 單詞符號及種別表如下 單詞符號 種別編碼 begin1if 2then 3while4do 5end 6l l d 10dd 11 13 14 15 16...
詞法分析器
這是我自己的第一篇部落格,就分享一下最近才做完的編譯原理實驗,詞法分析器。本次實驗中我用mysql資料庫儲存自動機狀態表,這樣做的目的只是為了在後續的課設中可以繼續使用現在的 這一段 並不是太完善,發出來只是為了太完善。裡面還有很多問題,比如對字元和字串的識別,不知道為什麼資料庫無法將 和 轉換到我...