資源簡介
計算機科學與技術11級。19周的課程設計編譯原理實習
代碼片段和文件信息
#include
#include
#include
#include
#include
#include
#define?bs?100??//Buffer?size=100
char?buf[bs];??
char?cw; //current?word
int?cc=0;
int?cx=1;
int?ll=-1;?
int?err=0;
char?bfs[bs];
int?used=-1;
ifstream?in(“input.txt“);
ofstream?out(“output.txt“);
//符號表
char?SymTableol[17][3]={“+?““-?““*?““/?““>?““““>=““<=““!=““==““?““;?““(?““)?““{?““}?““=?“};
//關鍵字表
char?keyword[8][6]={“void““int““if““else““while““do““main““const“};
struct?CODE{
????char?op[10];
char?a1[10];
char?a2[10];
????char?t[10];
}code[100];
struct?Sym{
char?str[10];
int?n;
}SymTable;
struct?Idtifier{
????char?id[20];
int?n;
}IdenTable[bs];??
void?getch(char?&ch);????//取字符
void?getsym(char?*chSym?&SymTable);???//取符號
void?copy(char?*strchar?*chint?n);?
int?searchKeyword(char?*ch);??
int?searchSymTableol(char?*ch);?
void?program();???
void?subprogram();??
void?vardef();??
void?idTabel();??
void?idtifier();?
void?idTabel1();?
int?createIdTable(Sym?SymTableint?n);?
int?searchidTable(Sym?SymTable);???
void?sentencepart();?
void?sentencepart1();?
void?sentence();??
void?int_to_char(int?achar?*ch);??
void?expression();?
void?expression1();
void?term();?
void?term1();
void?factor();?
void?condition();
void?error(int?n);
void?gencode(char?*opchar?*a1char?*a2char?*t);?
void?getch(char?&ch)??
{
assert(in);
in.get(cw);
}
void?getsym(char?*chSym??&?SymTable)?
{
int?ll;
cc=-1;
while(1)??//放過空字符
{
????if(in.eof())
{
??error(0);
??return;
}
????else?if(cw==‘\n‘)
{
cx++;
getch(cw);
}
else?if(cw==‘?‘)?getch(cw);
else?break;
}
if(cw>=‘a‘&&cw<=‘z‘)
{
ch[++cc]=cw;
while(1)?
{
getch(cw);
if((cw>=‘a‘&&cw<=‘z‘)||(cw>=‘0‘&&cw<=‘9‘))
{
ch[++cc]=cw;
}
else?break;
}
ch[cc+1]=‘\0‘;
ll=searchKeyword(ch);
if(ll!=-1)?
SymTable.n=20+ll;
else?????
SymTable.n=1;
copy(SymTable.strchcc);
}
else?if(cw>=‘0‘&&cw<=‘9‘)
{
ch[++cc]=cw;
while(1)
{
in.get(cw);
if(cw>=‘0‘&&cw<=‘9‘)
{
ch[++cc]=cw;
}
else?break;
}
ch[cc+1]=‘\0‘;
copy(SymTable.strchcc);
SymTable.n=2;
}
else
{
char?sh[3];
sh[0]=cw;
getch(cw);
sh[1]=cw;
sh[2]=‘\0‘;
ll=searchSymTableol(sh);
if(ll==-1)
{
sh[1]=‘?‘;
ll=searchSymTableol(sh);
if(ll==-1)?error(1);
else?
{
ch[++cc]=sh[0];
copy(SymTable.strchcc);
SymTable.n=ll+3;
}
}
else
{
ch[++cc]=sh[0];
ch[++cc]=sh[1];
copy(SymTable.strchcc);
SymTable.n=ll+3;
getch(cw);
}
}
}
void?copy(char?*strchar?*chint?n)?
{
for(int?cx=0;cx<=n;cx++)
{
str[cx]=ch[cx];
}
str[n+1]=‘\0‘;
}
int?searchKeyword(char?*ch)?
{
int a=0;
while(a<=7)
{
if(!strcmp(chkeyword[a]))
return?a;
else?
a++;
}
if(a>7)?
return?-1;
}
int?searchSymT
- 上一篇:微機原理交通燈控制系統設計C語言實現
- 下一篇:C語言解八數碼問題
評論
共有 條評論