資源簡介
杭電編譯原理語法分析器 內含杭電編譯原理語法分析器源代碼 每行基本上有注釋 容易懂

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
#define?Max?655 ?//最大代碼長度
#define?WordMaxNum?256 ?//變量最大個數
#define?DigitNum?256 //常量最大個數
#define?MaxKeyWord 32 //關鍵字數量
#define?MaxOptANum?8 //運算符最大個數
#define?MaxOptBNum?4 //運算符最大個數
#define?MaxEndNum?11 //界符最大個數
typedef?struct?DisplayTable
{
int?Index;??//標識符所在表的下標
int?type; //標識符的類型
int?line; //標識符所在表的行數
char?symbol[20]; //標識符所在表的名稱
}Table;
int?TableNum?=?0;??//display表的表項總數
int?WordNum?=?0;???//變量的個數
int?DigNum?=?0; ?//常量的個數
bool?errorFlag?=?0;?//錯誤標志
int?TableIndex?=?-1;??//display?表的下標索引
int?beginCount?=?0;//遇到begin加1,遇到end減1
int?ifCount?=?0;?//遇到if加1
Table?*table?=?new?Table[Max];
//關鍵字
const?char*?const?KeyWord[MaxKeyWord]?=?{“and““array“?“begin““case““char““constant““do““else““end““false““for““if““input““integer““not““of““or““output“
“packed““procedure““program““read““real““repeat““set“?“then“?“to“?“type“?“until“?“var““while“?“with“};????
//?單目運算
const?char?OptA[]?=?{‘+‘‘-‘‘*‘‘/‘‘=‘‘#‘‘<‘‘>‘};
//雙目運算符
const?char?*OptB[]?=?{“<=““>=““:=““<>“};
//?界符
const?char?End[]?=?{‘(‘?‘)‘??‘‘??‘;‘??‘.‘??‘[‘??‘]‘??‘:‘??‘{‘??‘}‘??‘“‘};?
void?error(char?str[20]int?nLine?int?errorType)
{
?errorFlag?=?1;//將錯誤標志置1,代表程序出現過錯誤
?cout?<<“?\nError?:????“;
?switch(errorType)
?{
?case?1:
cout?<“第“?< ???break;
??case?2:
cout?<“第“?< ??break;
case?3:
cout?<“第“?< break;
}//switch
}//error
void?Scanner(char?ch[]int?chLenint?nLine)
{
int?chIndex?=?0;?
while(chIndex? {
/****************處理空格和tab***************************/
//忽略空格和tab
while(ch[chIndex]?==?‘?‘?||?ch[chIndex]?==?9?) ??
{?
chIndex?++;?
}
/*************************處理換行符*********************/
??//遇到換行符,行數加1
while(ch[chIndex]?==?10)
{
nLine++;
chIndex?++;
}
/***************************標識符**********************/
if(?isalpha(ch[chIndex]))?//以字母、下劃線開頭
{
?char?str[256];
?int?strLen?=?0;
//是字母、下劃線
?while(isalpha(ch[chIndex])?||?ch[chIndex]?==?‘_‘?)
?{
?str[strLen?++]?=?ch[chIndex];
?chIndex?++;
while(isdigit(ch[chIndex]))//不是第一位,可以為數字
{
str[strLen?++]?=?ch[chIndex];
chIndex?++;
}
}
str[strLen]?=?0;?//字符串結束符
//開始匹配
if(strlen(str)?>?20)?//標識符超過規定長度,報錯處理
{
error(strnLine1);
}
else
{
int?i;
for(i?=?0;i? {
if(strcmp(str?KeyWord[i])?==?0)
{?
strcpy(table[TableNum].symbolstr);
table[TableNum].type?=?1;??//關鍵字
table[TableNum].line?=?nLine;
table[TableNum].Index?=?i;
TableNum?++;
break;
}
}
if(i?>=?MaxKeyWord)?//不是關鍵字
{
table[Tabl
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????15768??2012-05-20?16:01??code?w\code.cpp
?????文件???????3381??2012-05-19?16:18??code?w\code.dsp
?????文件????????533??2012-05-19?16:18??code?w\code.dsw
?????文件??????41984??2012-05-20?16:02??code?w\code.ncb
?????文件??????49664??2012-05-20?16:02??code?w\code.opt
?????文件????????242??2012-05-20?16:02??code?w\code.plg
?????文件????????309??2012-05-20?16:02??code?w\test.txt
?????文件?????287744??2012-05-20?16:02??code?w\Debug\code.bsc
?????文件?????565307??2012-05-20?16:02??code?w\Debug\code.exe
?????文件?????807280??2012-05-20?16:02??code?w\Debug\code.ilk
?????文件?????284920??2012-05-20?16:02??code?w\Debug\code.obj
?????文件????2267748??2012-05-20?15:50??code?w\Debug\code.pch
?????文件????1106944??2012-05-20?16:02??code?w\Debug\code.pdb
?????文件??????????0??2012-05-20?16:02??code?w\Debug\code.sbr
?????文件??????74752??2012-05-20?16:02??code?w\Debug\vc60.idb
?????文件?????110592??2012-05-20?16:02??code?w\Debug\vc60.pdb
?????目錄??????????0??2012-05-20?16:02??code?w\Debug
?????目錄??????????0??2012-05-20?16:03??code?w
-----------?---------??----------?-----??----
??????????????5617168????????????????????18
- 上一篇:DW網頁設計100例
- 下一篇:Agilent 33250A使用手冊
評論
共有 條評論