資源簡介
本程序能實現C語言子集的編譯,能實現的主要功能包括:
(1)、實現編譯的詞法分析
(2)、語法分析
(3)、語義分析功能
(4)、錯誤處理能力,并給出總的出錯報告
(5)、編譯最終形成四元式的中間代碼形式

代碼片段和文件信息
#include?
#include?
#include?
#define?E?-1
#define??FLAG?100
#define?ACC?1000
//文法規則結構
struct?rule
{
char?left[2];
char?right[10];
};
//鏈表結點
struct?word
{
char?w[10];
struct?word?*next;
};
//狀態棧結點結構
struct?state
{
int?s;
struct?state?*next;
};
struct?word?*tem_name_list;???//臨時變量存放列表
struct?formula?*four_item;
int?_i;??//四元式編號
int?beg[100];???//存放要回填的四元式的編號
int?beg_i;
int?i_i;??//生成四元式的標號
//添加結點到鏈表,其實下面的鏈表就是一棧
void?add_list(struct?word?**headchar?*p);
//結點出n次棧
void?pop(struct?word?**headint?n);
//添加結點到狀態到棧
void?state_add(struct?state?**headint?m);
//狀態結點出n次棧
void?state_pop(struct?state?**headint?n);
//在鏈表中查找單詞是否已經存在,存在返回0,不存在返回-1
int?find_word(struct?word?*listchar?*p);
//判斷一個標識符是否合法合法返回0,不合法返回-1
int?is_identifier(char?*p);
//判斷一個單詞是否是一個整數,是返回0,不是返回-1
int?is_int(char?*p);
struct?word?*key_word;?????//保留關鍵字鏈表
struct?word?*identifier;???//用戶自定義標識符
struct?word?*operation;????//運算符
struct?word?*boundary;?????//界符
//四元式鏈表結點結構
struct?formula
{
int?i;??????????//四元式編號
char?op[10];???//操作符
char?p1[10];????//
char?p2[10];
char?res[10];???
struct?formula?*next;
};
//文法規則矩陣
?struct?rule?rule_gather[27];
//規則矩陣初始化
?void?init_rule();
?//SLR(1)分析表
//查找并返回單詞對應SLR(1)分析表中的列號如果不存在,則返回-1
int?find_num(const?char?*p);
//計算一個字符串有多少個字符
int?word_num(const?char?*p);
//產生一個新的臨時變量T1T2、、、、
char?*create_tem(char?*p);
//打印出一個四元式
void?printf_four(char?*operchar?*p1char?*p2char?*res);
//對歸約時會改變語義棧的規則編寫語義子程序
void?r_16(struct?word?**yuyi_stackchar?*p1char?*p2);
void?r_18(struct?word?**yuyi_stackchar?*p1char?*p2char?*tem);
void?r_19(struct?word?**yuyi_stackchar?*p1char?*p2char?*tem);
void?r_21(struct?word?**yuyi_stackchar?*p1char?*p2char?*tem);
void?r_22(struct?word?**yuyi_stackchar?*p1char?*p2char?*tem);
void?r_26(struct?word?**yuyi_stackchar?*p1);
//根據規則編號,調用相應的語義子程序
void?call_r(struct?word?**yuyi_stackint?mchar?*input_word);
//語法分析,整個程序語法正確返回0,否則返回-1
int?syntax_analyze(FILE?*fp);
//添加結點到鏈表,其實下面的鏈表就是一棧
void?add_list(struct?word?**headchar?*p)
{
if(*head==NULL)
{
*head=(struct?word*)malloc(sizeof(struct?word));
strcpy((*head)->wp);
(*head)->next=NULL;
}
else
{
struct?word?*tem=(struct?word*)malloc(sizeof(struct?word));
strcpy(tem->wp);
tem->next=*head;
*head=tem;
}
}
//結點出n次棧
void?pop(struct?word?**headint?n)
{
struct?word?*tem;
if(n<1)
return;
while(n)
{
if(*head==NULL)
return;
tem=*head;
*head=(*head)->next;
free(tem);
n--;
}
}
//添加結點到狀態到棧
void?state_add(struct?state?**headint?m)
{
if(*head==NULL)
{
*head=(struct?state*)malloc(sizeof(struct?state));
(*head)->s=m;
(*head)->next=NULL;
}
else
{
struct?state?*tem=(struct?state*)malloc(sizeof(struct?state));
tem->s=m;
tem->next=*head;
*head=tem;
}
}
//狀態結點出n次棧
void?state_pop(struct?state?**headint?n)
{
struct?state?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????38??2009-12-30?23:42??C語言子集編譯器\C語言子集編譯器\1.txt
?????文件?????????49??2011-01-07?02:53??C語言子集編譯器\C語言子集編譯器\123.txt
?????文件?????????42??2011-01-07?18:13??C語言子集編譯器\C語言子集編譯器\2.txt
?????文件?????????54??2011-01-07?18:19??C語言子集編譯器\C語言子集編譯器\3.txt
?????文件?????????78??2011-01-07?13:27??C語言子集編譯器\C語言子集編譯器\566.txt
?????文件?????????52??2011-01-07?02:52??C語言子集編譯器\C語言子集編譯器\77.txt
?????文件?????????52??2009-12-28?00:26??C語言子集編譯器\C語言子集編譯器\88
?????文件?????????52??2011-01-07?02:53??C語言子集編譯器\C語言子集編譯器\88.txt
?????文件?????????59??2011-01-07?02:53??C語言子集編譯器\C語言子集編譯器\99.txt
?????文件??????28794??2009-12-28?09:38??C語言子集編譯器\C語言子集編譯器\analy.cpp
?????文件??????????0??2009-12-25?01:42??C語言子集編譯器\C語言子集編譯器\analy.h
?????文件????????189??2011-01-07?18:19??C語言子集編譯器\C語言子集編譯器\analy_res.txt
?????文件??????22236??2011-01-07?12:43??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.APS
?????文件???????1470??2010-01-07?10:07??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.clw
?????文件???????2035??2009-12-15?21:20??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.cpp
?????文件???????4542??2009-12-28?09:42??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.dsp
?????文件????????555??2009-12-15?21:20??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.dsw
?????文件???????1311??2009-12-15?21:20??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.h
?????文件???????1479??2010-12-22?23:45??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.idc
?????文件???14027776??2011-01-07?18:42??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.ncb
?????文件??????????0??2009-12-25?13:11??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.ncb?(Can‘t?open)
?????文件??????48640??2010-01-14?11:52??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.opt
?????文件????????264??2010-01-13?23:02??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.plg
?????文件???????5794??2009-12-28?10:23??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.rc
?????文件????????908??2011-01-07?12:47??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.sln
????..A..H.?????15360??2011-01-07?18:42??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.suo
?????文件???????8228??2011-01-07?12:47??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.vcproj
?????文件???????1427??2011-01-07?18:42??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器.vcproj.D38387E719004BD.Administrator.user
?????文件???????7185??2009-12-28?10:25??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器Dlg.cpp
?????文件???????1409??2009-12-28?10:19??C語言子集編譯器\C語言子集編譯器\C語言子集編譯器Dlg.h
............此處省略26個文件信息
評論
共有 條評論