資源簡介
編譯課程設計的課程設計,里面包含一個“班級”的編譯課程設計,供大家參考!

代碼片段和文件信息
//有效字符串:?IFELSETHEN?abx>=
/*
文法:
??S->id=id?|?if?E?then?S?else?S
??E->id?
*/
#include
#include
#include
#define?$ASSIGN?249
#define?$IF?250
#define?$THEN?251
#define?$ELSE?252
#define?$GREAT?253
#define?$LESS?248?
#define?$ID?254
typedef?struct?Wtoken
{
int?type;
char?ch;
}Wtoken;
typedef?enum{JUMPJGJLASSIGNEND}OpKind;
typedef?struct
{
int?label;//標號
OpKind?op;
char?par1par2;
union{
char?result;
int?address;
};
}Fourtable;//四元式
#define?MAX_TOKEN?256?//Wtoken表大小
#define?MAX_QUAD?256?//四元式數組大小
Wtoken?tokentable[MAX_TOKEN];
Fourtable?quad[MAX_QUAD];
int?token_index;//token表索引
int?total_len;//token表有效長度
int?quad_len;//四元式表有效長度
int?quad_index;//四元式索引
int?label;
ifstream?ins;
bool?init(char?filename[255]);
bool?cifafenxi();
void?print();
void?ERROR();
void?S(intint);
void?E(intintint);
bool?nexttoken();
int?newlabel();
void?yuyifenxi();
void?printFourtable();
void?main()
{
cout<<“Please?input?the?name?of?file:“;
char?fname[200];
cin>>fname;
if(!init(fname))
return;
if(!cifafenxi())
return;
char?ch;
while(1)
{
if(ins.eof())
break;
ins>>ch;
}
cout<<“The?result?of?CIFA?analyse?:“< print();
cout< nexttoken();
cout<<“The?Grammar:“< yuyifenxi();
cout<<“The?FourTable?as?followings:“< printFourtable();
}
int?newlabel()
{
return?label++;
}
bool?init(char?filename[255])
{
token_index=0;
total_len=0;
quad_len=0;
quad_index=0;
label=100;
ins.open(filenameios::nocreate?|?ios::in);
if(ins.fail())
{
cout<<“文件打開出錯!“< return?false;
}
return?true;
}
//
bool?cifafenxi()
{
char?buf[16];
char?ch;
while(1)
{
ins>>ch;
if(ins.fail())
break;
if(ch==‘I‘)
{
ins>>buf;
if(strcmp(buf“F“)==0)
tokentable[total_len++].type=$IF;
}
else?if(ch==‘T‘)
{
ins>>buf;
if(strcmp(buf“HEN“)==0)
tokentable[total_len++].type=$THEN;
}
else?if(ch==‘E‘)
{
ins>>buf;
if(strcmp(buf“LSE“)==0)
tokentable[total_len++].type=$ELSE;
}
else?if(ch==‘>‘)
{
tokentable[total_len++].type=$GREAT;
}
else?if(ch==‘<‘)
{
tokentable[total_len++].type=$LESS;
}
else?if(ch==‘=‘)
{
tokentable[total_len++].type=$ASSIGN;
}
else?if(ch==‘x‘?||?(ch>=‘a‘?&&?ch<=‘z‘))
{
tokentable[total_len].type=$ID;
tokentable[total_len++].ch=ch;
}
}
return?true;
}
#define?AD_RESULT(nlabelnopnpar1npar2nresult)?{quad[quad_len].label=nlabel;?quad[quad_len].op=nop;?quad[quad_len].par1=npar1;?quad[quad_len].par2=npar2;?quad[quad_len].result=nresult;?quad_len++;?}
#define?AD_ADDRESS(nlabelnopnpar1npar2naddress)?{ quad[quad_len].label=nlabel;? quad[quad_len].op=nop;? quad[quad_len].par1=npar1;? quad[quad_len].par2=npar2;? quad[quad_len].address=naddress;? quad_len++;?}
Wtoken?cur;
bool?nexttoken()
{
if(token_ind
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????92??2009-06-21?12:58??1\1\01\源程序\bianyi.txt
?????文件?????225380??2009-06-21?16:25??1\1\01\源程序\Debug\條件語句.exe
?????文件?????????31??2009-06-19?07:33??1\1\01\源程序\tiaojian.txt
?????文件???????6207??2009-06-21?13:06??1\1\01\源程序\條件語句.cpp
?????文件?????196691??2005-12-30?09:08??1\1\01\源程序\條件語句.exe
?????文件?????????68??2009-06-20?22:13??1\1\02\chanic.txt
?????文件??????14871??2009-06-19?20:54??1\1\02\chenzhuo.cpp
?????文件?????233567??2009-06-20?22:12??1\1\02\Debug\chenzhuo.exe
?????文件??????13969??2009-06-18?13:19??1\1\03\cjt.cpp
?????文件?????581727??2009-06-18?15:49??1\1\03\Debug\cjt.exe
?????文件?????????37??2009-06-18?12:15??1\1\03\input.txt
?????文件???????7600??2009-06-21?16:47??1\1\04\源代碼以及測試文件\LRcompiler.cpp
?????文件?????????39??2009-06-21?16:53??1\1\04\源代碼以及測試文件\test0.txt
?????文件?????????86??2009-06-21?16:53??1\1\04\源代碼以及測試文件\test1.txt
?????文件???????3449??2010-01-06?10:58??1\1\04\源代碼以及測試文件\LRcompiler.dsp
?????文件??????74752??2010-01-06?10:59??1\1\04\源代碼以及測試文件\Debug\vc60.idb
?????文件?????110592??2010-01-06?10:58??1\1\04\源代碼以及測試文件\Debug\vc60.pdb
?????文件????2148736??2010-01-06?10:58??1\1\04\源代碼以及測試文件\Debug\LRcompiler.pch
?????文件?????322746??2010-01-06?10:58??1\1\04\源代碼以及測試文件\Debug\LRcompiler.obj
?????文件?????823296??2010-01-06?10:58??1\1\04\源代碼以及測試文件\Debug\LRcompiler.ilk
?????文件?????589953??2010-01-06?10:58??1\1\04\源代碼以及測試文件\Debug\LRcompiler.exe
?????文件????1131520??2010-01-06?10:58??1\1\04\源代碼以及測試文件\Debug\LRcompiler.pdb
?????文件??????33792??2010-01-06?10:59??1\1\04\源代碼以及測試文件\LRcompiler.ncb
?????文件????????766??2010-01-06?10:58??1\1\04\源代碼以及測試文件\LRcompiler.plg
?????文件??????48640??2010-01-06?10:59??1\1\04\源代碼以及測試文件\LRcompiler.opt
?????文件????????528??2010-01-06?10:59??1\1\04\源代碼以及測試文件\LRcompiler.dsw
?????文件?????????94??2009-06-21?16:20??1\1\05\2.txt
?????文件???????6766??2009-06-21?14:22??1\1\05\if.cpp
?????文件??????????0??2009-06-21?12:59??1\1\06\bianyi\build\classes\.netbeans_automatic_build
?????文件??????????8??2009-06-21?12:59??1\1\06\bianyi\build\classes\bianyi\bianyi.rs
............此處省略279個文件信息
評論
共有 條評論