資源簡介
山東大學計算機專業編譯原理實驗——構造一個pl0文法語言的編譯器,用c++寫的。其中語法分析和解釋執行部分有點bug,有興趣的同學可以參考一下。另外本人菜鳥一枚,代碼寫得比較亂,望各位大佬親噴!

代碼片段和文件信息
#include?“Iterpreter.h“
#define?STACK_SIZE?100
Iterpreter::Iterpreter(const?char*?filename){
readPl0(filename);//讀取pl0文件
ip?=?0;
sp?=?-1;
bp?=?0;
lev?=?0;
SL.push_back(0);//主程序的靜態鏈是0
stack?=?new?int[STACK_SIZE];
for?(int?i?=?0;?i? stack[i]?=?0;
}
run();
}
void?Iterpreter::readPl0(const?char*?filename){
FILE?*fp?=?fopen(filename?“rb“);//打開.pl0文件
int?i?=?0;
if?(!fp){
cout?<“打開.pl0文件失敗!“?< exit(-1);
}
while?(!feof(fp)){
codeSeg.push_back(CODE());//先push一個空的
fread(&codeSeg[i++]?sizeof(CODE)?1?fp);
}
}
void?Iterpreter::run(){
while?(ip?
runInst();
}
cout?<“代碼翻譯并執行完畢!“?<
}
void?Iterpreter::runInst(){
inst?=?codeSeg.at(ip++);//取指
int?temp;
switch?(inst.fun){
case?LIT: //常量放棧頂
stack[++sp]?=?inst.offset;
break;
case?LOD: //變量放棧頂
{
int?tempBp?=?stack[bp?+?2];
int?levOffset?=?inst.lev;
while?(levOffset--?!=?0){//沿著靜態鏈往外層找
tempBp?=?stack[tempBp?+?2];
}
temp?=?stack[tempBp?+?inst.offset];
stack[++sp]?=?temp;
break;
}
case?STO: //棧頂內容存到變量中
{
int?tempBp?=?stack[bp?+?2];
int?levOffset?=?inst.lev;
while?(levOffset--?!=?0){//沿著靜態鏈往外層找
tempBp?=?stack[tempBp?+?2];
}
temp?=?stack[sp];
stack[tempBp?+?inst.offset]?=?temp;
break;
}
case?CAL:
stack[sp?+?1]?=?bp;//push?bp.老bp,即動態鏈
stack[sp?+?2]?=?ip;//返回地址
stack[sp?+?3]?=?SL[lev?-?inst.lev];//靜態鏈
lev?=?lev?-?inst.lev;
SL.push_back(bp); //保存當前運行的bp.每call一次保存一次
bp?=?sp?+?1;//記錄被調用過程的基地址
ip?=?inst.offset;
break;
case?INT:
sp?+=?inst.offset;//棧頂加a
break;
case?JMP:
ip?=?inst.offset;//ip轉到a
break;
case?JPC:
if?(!stack[sp])//棧頂布爾值為非真
ip?=?inst.offset;//轉到a的地址
break;
case?OPR://關系和算術運算
{
switch?(inst.offset)
{
case?OPR::ADD:
temp?=?stack[sp?-?1]?+?stack[sp];
stack[--sp]?=?temp;
break;
case?OPR::SUB:
temp?=?stack[sp?-?1]?-?stack[sp];
stack[--sp]?=?temp;
break;
case?OPR::DIV:
temp?=?stack[sp?-?1]?/?stack[sp];
stack[--sp]?=?temp;
break;
case?OPR::MINUS:
stack[sp]?=?-stack[sp];
break;
case?OPR::MUL:
temp?=?stack[sp?-?1]?*?stack[sp];
stack[--sp]?=?temp;
break;
case?OPR::EQ:
temp?=?(stack[sp?-?1]?-?stack[sp]?==?0);
stack[--sp]?=?temp;
break;
case?OPR::UE:
temp?=?(stack[sp?-?1]?-?stack[sp]?!=?0);
stack[--sp]?=?temp;
break;
case?OPR::GE:
temp?=?(stack[sp?-?1]?-?stack[sp]?>=?0);
stack[--sp]?=?temp;
break;
case?OPR::GT:
temp?=?(stack[sp?-?1]?-?stack[sp]?>?0);
stack[--sp]?=?temp;
break;
case?OPR::LE:
temp?=?(stack[sp?-?1]?-?stack[sp]?<=?0);
stack[--sp]?=?temp;
break;
case?OPR::LT:
temp?=?(stack[sp?-?1]?-?stack[sp]0);
stack[--sp]?=?temp;
break;
case?OPR::ODD: //判斷棧頂操作數是否為奇數
stack[sp]?=?(stack[sp]?%
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4779??2017-12-16?09:58??compile_ex2\compile_ex2\compile_ex2.vcxproj
?????文件???????1777??2017-12-16?09:58??compile_ex2\compile_ex2\compile_ex2.vcxproj.filters
?????文件???????5527??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.log
?????文件???14680064??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.pch
?????文件???????3452??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\cl.command.1.tlog
?????文件??????26574??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\CL.read.1.tlog
?????文件???????6308??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\CL.write.1.tlog
?????文件????????196??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\compile_ex2.lastbuildstate
?????文件???????2090??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\li
?????文件???????3594??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\li
?????文件???????1278??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\compile_ex2.tlog\li
?????文件?????531460??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\Iterpreter.obj
?????文件?????419079??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\stdafx.obj
?????文件?????905048??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\SyntaxAnal.obj
?????文件?????437248??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\vc120.idb
?????文件?????716800??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\vc120.pdb
?????文件?????560444??2017-12-30?09:58??compile_ex2\compile_ex2\Debug\WordAnal.obj
?????文件???????4824??2017-12-30?09:22??compile_ex2\compile_ex2\Iterpreter.cpp
?????文件????????675??2017-12-30?09:24??compile_ex2\compile_ex2\Iterpreter.h
?????文件????????487??2018-01-01?23:45??compile_ex2\compile_ex2\ReadMe.txt
?????文件????????435??2017-12-16?16:12??compile_ex2\compile_ex2\stdafx.cpp
?????文件???????2401??2017-12-30?09:45??compile_ex2\compile_ex2\stdafx.h
?????文件??????23807??2017-12-30?09:25??compile_ex2\compile_ex2\SyntaxAnal.cpp
?????文件???????1308??2017-12-30?10:21??compile_ex2\compile_ex2\SyntaxAnal.h
?????文件????????236??2017-10-08?21:51??compile_ex2\compile_ex2\targetver.h
?????文件???????5668??2017-12-30?09:19??compile_ex2\compile_ex2\WordAnal.cpp
?????文件????????615??2017-12-30?10:20??compile_ex2\compile_ex2\WordAnal.h
?????文件????9437184??2017-12-30?10:45??compile_ex2\compile_ex2.sdf
?????文件????????979??2017-10-08?21:51??compile_ex2\compile_ex2.sln
????..A..H.?????52736??2017-12-30?10:45??compile_ex2\compile_ex2.v12.suo
............此處省略15個文件信息
- 上一篇:C++圖像偽彩色處理源代碼
- 下一篇:華中科技大學-C++實驗 共6次-源代碼+報告
評論
共有 條評論