資源簡介
基于C語言編寫的一個簡單的編譯器,涉及的功能不多,只限于簡單代碼的翻譯。
代碼片段和文件信息
#include
#include
#include
#include
#include“Compiler.h“
char?name[MAXC][MAXWORD]; //用來存儲標識符
int?NumOfC=1; //符號表內元素的個數
Character?Symlist[MAXC]; ????//符號表
int?numoflev[MAXLEV]; //各層的表中變量數
opr??pcode[MAXOP]; //存Pcode指令
int?oprnum; //指令數
int?stack[MAXGO]; //Pcode解釋執行階段的堆棧
int?dl[MAXGOLEV]bl[MAXGOLEV]; ????//stack堆棧運行過程中的指針
int?linenum=1; ????? //源程序每行的代號
int?errornum=0error[MAXERROR]errorline[MAXERROR];
int?gonum=0dlnum=0blnum=0;
int?type=0;
int?snum=0;
int?start;
int?lev=1;
int?breakoprIsbreak=0;
int?typeofexp1=11?typeofexp2=12;
int?oprnumcase=0;
int?Isret=1;
//字符獲取和處理
int?getsym()
{
char?chtmp;
int?ijm;
ch=fgetc(FIN);
while(isspace(ch))//處理空格
{
putc(chFOUT);
if(ch==‘\n‘)
{
linenum++;
printf(“%d:\t“linenum);
}
ch=fgetc(FIN);
}
i=0;
Word[i]=ch;
i++;
//token處理
if(isalpha(ch))
{
putc(chFOUT);
ch=fgetc(FIN);
while(isalpha(ch)||isdigit(ch)||(ch==‘_‘))??????//加入下劃線
{
Word[i]=ch;
i++;
/*if(i==MAXWORD)
{
errorline[errornum]=linenum;
error[errornum++]=1;
i--;
Word[i]=‘\0‘;
putc(chFOUT);
ch=fgetc(FIN);
while(isalpha(ch)||isdigit(ch))
{
putc(chFOUT);
}
ungetc(chFIN);
return?1;//是標識符
}*/
putc(chFOUT);
ch=fgetc(FIN);
}
ungetc(chFIN);
Word[i]=‘\0‘;??
for(m=0;m {
if(!(strcmp(Keyword[m]Word))) //讀取的字符串符合對key?word的判斷
{
if(m+10==12)
return?1;
return?m+10;//返回相應的keyword的對應數字
}
}
if(m==kk){
return?1;//不是key?word是標識符
}
}
//數值處理
if(isdigit(ch))
{
putc(chFOUT);
while(isdigit((ch=fgetc(FIN))))
{
putc(chFOUT);
Word[i++]=ch;
if(i==MAXWORD)
{
errorline[errornum]=linenum;
error[errornum++]=1;
Word[--i]=‘\0‘;
ch=fgetc(FIN);
while(isdigit(ch))
{
putc(chFOUT);
}
num=atoi(Word);//將字符串轉化為對應的數字
ungetc(chFIN);
return?2;//是數字
}
}
ungetc(chFIN);
Word[i]=‘\0‘;
num=atoi(Word);//將字符串轉化為對應的數字
return?2;//是數字
}
//分界符
i=34;
putc(chFOUT);
switch(ch){
case‘\‘‘:
ch=fgetc(FIN);
if(ch==‘+‘||ch==‘-‘||ch==‘*‘||ch==‘\‘‘||isalpha(ch))?//對應于printf語句中的各種參數
{
cischar=ch;
putc(chFOUT);
if?((ch=fgetc(FIN))==‘\‘‘)
{
putc(chFOUT);
return?4;//字符
}
else?
{
putc(chFOUT);
errorline[errornum]=linenum;
error[errornum++]=3;
return?-1;
}
}
case‘“‘:
j=0;
while((ch=fgetc(FIN))!=‘“‘)//若不是非空串
{
putc(chFOUT);
sisstring[j++]=ch;
if(j==MAXLENGTH)
{
printf(“warning!字符串過長\n“);
sisstring[--j]=‘\0‘;
putc(chFOUT);
ch=fgetc(FIN);
while(ch!=‘“‘)
{
putc(chFOUT);
j++;
if(ch==EOF||j>100) //字符串過長或缺少反引號
{
errorline[errornum]=linenum;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????36246??2009-04-11?21:19??Compiler.cpp
?????文件???????4263??2009-04-11?21:24??Compiler.h
-----------?---------??----------?-----??----
????????????????40509????????????????????2
- 上一篇:LOOK電梯調度算法
- 下一篇:opencv實現surf算法
評論
共有 條評論