资源简介

该程序为编译原理作业,完成了C编译器的实现,包括词法分析,语法分析,语义分析,是学习编译原理的好素材。

资源截图

代码片段和文件信息

# include “common.h“

void INTERPRET()  //翻译的主过程
{
//////////  初始化部分  /////////
oldTop=0;     
stop=0;
top=0;
bp=0;
pc=0;
DISPLAY[1]=0;
S[1]=0;
S[2]=0;
S[3]=0;
/////////  初始化结束  //////////

printf(“翻译开始\n“);
do
{
instruction=CODE[pc];  //取指令
pc++;                  //PC加一

switch(instruction.func)  //翻译执行
{
case LIT:
case LIT1:
top++;
S[top]=instruction.address;
break;
case LOD:
top++;
S[top]=S[DISPLAY[instruction.level]+instruction.address];
break;
case LODA:
top++;
S[top]=DISPLAY[instruction.level]+instruction.address;
break;
case ILOD:
top++;
S[top]=S[S[DISPLAY[instruction.level]+instruction.address]];
break;
case LODT:
S[top]=S[S[top]];
break;
case LODB:
h=S[top];
top--;
hh=instruction.address+top;
while(top {
top++;
S[top]=S[h];
h++;
}
break;
case CPYB:
h=S[top-1];
hh=S[top];
hhh=h+instruction.address;
while(h {
S[h]=S[hh];
h++;
hh++;
}
top-=2;
break;
case STO:
S[S[top-1]]=S[top];
top-=2;
break;
case OPAC:
oldTop=top;
top+=3;
break;
case CAL:
S[oldTop+1]=pc;
S[oldTop+2]=DISPLAY[instruction.level];
S[oldTop+3]=bp;
pc=instruction.address;
break;
case ENTP:
bp=oldTop+1;
DISPLAY[instruction.level]=bp;
top=oldTop+instruction.address;
break;
case UDIS:
h=instruction.address;
hh=instruction.level;
hhh=bp;
do
{
DISPLAY[h]=hhh;
h--;
hhh=S[hhh+1];
}while(h!=hh);
break;
case JMP:
pc=instruction.address;
break;
case JPC:
if(S[top]==0)
{
pc=instruction.address;
}
top--;//改正
break;
case RETP:
top=bp-1;
pc=S[top+1];
bp=S[top+3];
break;
case ENDP:
stop=1;
break;
case RED:
if(instruction.address==0)
{
printf(“Your Input:“);
scanf(“%d“&temp);
}
else
getch();
S[S[top]]=temp;
break;
case WRT:
if(instruction.address==0)
printf(“Your Output:%d\n“S[top]);
else
{
ch=(char)S[top];
printf(“Your Output%c\n“ch);
}
top--;
break;
case MUS:
S[top]=-S[top];
case ADD:
case ADD1:
top--;
S[top]=S[top]+S[top+1];
break;
case SUB:
top--;
S[top]=S[top]-S[top+1];
break;
case MULT:
top--;
S[top]=S[top]*S[top+1];
break;
case IDIV:
top--;
S[top]=S[top]/S[top+1];
break;
case IMOD:
top--;
S[top]=S[top]%S[top+1];
break;
case ANDS:
top--;
S[top]=S[top]&S[top+1];
break;
case ORS:
top--;
S[top]=S[top]|S[top+1];
break;
case NOTS:
top--;
S[top]=~S[top];
break;
case EQ:
top--;
S[top]=(S[top]==S[top+1])?1:0;
break;
case NE:
top--;
S[top]=(S[top]!=S[top+1])?1:0;
break;
case LS:
top--;
S[top]=(S[top] break;
case GE

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     180316  2004-09-11 08:28  bin\interpret.exe

     文件     184398  2004-09-11 08:28  bin\PL.exe

     文件       5624  2003-01-09 15:12  src\PL\common.h

     文件      47846  2003-12-30 13:33  src\PL\PL.cpp

     文件       3357  2009-09-30 18:46  src\PL\PL.dsp

     文件        527  2009-09-30 18:46  src\PL\PL.dsw

     文件      82944  2009-09-30 18:46  src\PL\PL.ncb

     文件      48640  2009-09-30 18:46  src\PL\PL.opt

     文件       1506  2009-09-30 18:45  src\PL\pl.plg

     文件    1426432  2009-09-30 18:45  src\PL\Debug\PL.bsc

     文件     175327  2009-09-30 18:45  src\PL\Debug\PL.obj

     文件    3679908  2009-09-30 18:45  src\PL\Debug\PL.pch

     文件      25600  2009-09-30 18:45  src\PL\Debug\PL.pdb

     文件          0  2009-09-30 18:45  src\PL\Debug\PL.sbr

     文件     123904  2009-09-30 18:45  src\PL\Debug\vc60.idb

     文件      69632  2009-09-30 18:45  src\PL\Debug\vc60.pdb

     文件        882  2003-01-09 17:22  src\interpret\common.h

     文件       4466  2003-12-30 12:25  src\interpret\interpret.cpp

     文件       3437  2004-05-21 22:03  src\interpret\interpret.dsp

     文件        543  2004-05-21 22:03  src\interpret\interpret.dsw

     文件      41984  2004-09-11 08:28  src\interpret\interpret.ncb

     文件      53760  2004-09-11 08:28  src\interpret\interpret.opt

     文件       1169  2004-09-11 08:28  src\interpret\interpret.plg

     目录          0  2009-11-16 15:17  src\PL\Debug

     目录          0  2009-11-16 15:17  src\PL

     目录          0  2009-11-16 15:17  src\interpret

     目录          0  2009-11-16 15:17  bin

     目录          0  2009-11-16 15:17  src

----------- ---------  ---------- -----  ----

              6162202                    28

............此处省略1个文件信息

评论

共有 条评论