小白求助(下面的代码求分析)

Oliverjian 2020-01-12 06:34:16
这段代码针对的是编译器,目标语言是简化后的PASCAL语言(),用C++编写编译器 生成目标文件.obj.
程序是教授给我的,让我去看,但是我真的看不懂啊,求大神帮我从大体上解释一下 都是什么意思 因为c++我基本不会。。。只希望用最快的时间读懂。


#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include "DecompileCode.h"


using namespace std;

vector<string> Lexer(string code);
vector<string> Operatorr(string code);

// vector<string> patterns = {
// "READ.{3}",
// "WRITE.{3}",
// "WHILE(.+)DO",
// "ENDWHILE"
// };

vector<string> patterns{
"READ",
"WRITE",
"WHILEDO",
"ENDWHILE"
};


int main()
{
/*FILE *fp = NULL;

fp = fopen("SAMPLE.pas","r");
*/

vector<string> decompilers;
//FILE *fp2 = NULL;fp2 = fopen("SAMPLE.obj","w");
ifstream fp;
ofstream fp2;
fp.open("SAMPLE.pas",ios::in);
fp2.open("SAMPLE.obj", ios::out);

char ch;
string file;
while (fp.get(ch)) {
file += ch;
//cout.put(ch);
}
//cout<<GET;
decompilers=Lexer(file);

for (int i = 0; i < decompilers.size(); i++) {
fp2 << decompilers[i] << endl;
}

fp.close();
fp2.close();
return 0;
}

vector<string> Lexer(string code)
{
vector<string> codes;
vector<string> decompilers;
string sub = "";
string sub_r = "";
int i = 0;
int start = 0;
int len = code.length();
int UJPi = 0;
int CJPi = 0;
int Ji = 0;


for (; i < len; i++) {
if (code[i] == ';'|| code[i] == '.'){
sub = code.substr(start, i - start);
int j = 0;

for (int i = 0; i < sub.length(); i++) {
if (sub[i] != ' ') {
sub_r += sub[i];
}
}
codes.push_back(sub_r);
sub = "";
sub_r = "";
i = i + 1;
start = i + 1;
}
if ((code[i] == 'O'&&code[i + 1] == '\n'&&code[i - 1] == 'D')) {
sub = code.substr(start, i - start+1);
int j = 0;

for (int i = 0; i < sub.length(); i++) {
if (sub[i] != ' ') {
sub_r += sub[i];
}
}
codes.push_back(sub_r);
sub = "";
sub_r = "";
i = i + 1;
start = i + 1;
}
}
for (int i = 0; i < codes.size(); i++) {
cout <<codes[i] <<endl;
}


vector<regex> regs;
for (int i = 0; i < patterns.size(); i++) {
regs.push_back(regex(patterns[i]));
}
string temp = "";


for (int i = 0; i < codes.size(); i++) {


bool read = regex_match(codes[i],regs[0]);
bool write = regex_match(codes[i], regs[1]);
bool whiledo = regex_match(codes[i], regs[2]);
bool endwhile = regex_match(codes[i], regs[3]);
if (read) {
temp = temp + GET + " ";
for (int j = 0; j < codes[i].length(); j++) {
if (codes[i][j] == '('&&codes[i][j + 1] >= 65 && codes[i][j + 1] <= 90)
{
int v = codes[i][j + 1] - 65;
stringstream s;
string v_s;
s << v;
s >> v_s;
temp += v_s;
s.clear();
}
}
decompilers.push_back(temp);
cout << temp<<endl; temp = "";
}



else if (write) {
temp = temp + PUT + " ";
for (int j = 0; j < codes[i].length(); j++) {
if (codes[i][j] == '('&&codes[i][j + 1] >= 65 && codes[i][j + 1] <= 90)
{
int v = codes[i][j + 1] - 65;
stringstream s;
string v_s;
s << v;
s >> v_s;
temp += v_s;
s.clear();
}
}
decompilers.push_back(temp);
cout << temp << endl; temp = "";
}



else if (whiledo) {
UJPi = decompilers.size();
vector<string> operations;
int k = 5;
while(!(codes[i][k]=='D'&&codes[i][k+1]=='O')) {
temp = temp += codes[i][k];
k++;
}
operations = Operatorr(temp);
for (int j = 0; j < operations.size(); j++) {
cout << operations[j] << endl;
decompilers.push_back(operations[j]);
}
Ji = decompilers.size();
operations.clear();
temp = "";
temp = CJP + " ";
decompilers.push_back(temp);
temp = "";
}



else if (endwhile) {
temp = UJP + " ";
decompilers.push_back(temp);
temp = "";
CJPi = decompilers.size();
}


else {
if (whiledo == false) {
vector<string> operations;
operations = Operatorr(codes[i]);
for (int j = 0; j < operations.size(); j++) {
cout << operations[j] << endl;
decompilers.push_back(operations[j]);
}
operations.clear();
}
}

}

for (int i = 0; i < decompilers.size(); i++) {
if (i == Ji) {
stringstream sss;
string CJPI_s;
sss << CJPi;
sss >> CJPI_s;
decompilers[i] = decompilers[i] + CJPI_s;
sss.clear();
}
if (i == CJPi-1)
{
stringstream sss;
string UJPI_s;
sss << UJPi;
sss >> UJPI_s;
decompilers[i] = decompilers[i] + UJPI_s;
sss.clear();
}
}

return decompilers;
}

vector<string> Operatorr(string code) {
vector<string> operations;
vector<string> variables;
vector<string> real_operators;
string calculation="";
bool equaltion = false;
bool greater = false;
bool lesser = false;
bool equal = false;
stringstream s;
string v_s;
for (int i = 0; i < code.length(); i++) {


if (code[i] == ':'&&code[i + 1] == '=') {
equaltion = true;
int v = code[i - 1] - 65;
s << v;
s >> v_s;
s.clear();
}

else if (code[i] == '>') {
greater = true;
int v = code[i - 1] - 65;
s << v;
s >> v_s;
calculation = calculation + LOD + " " + v_s; operations.push_back(calculation); calculation = "";
s.clear();
}
else if (code[i] == '<') {
lesser = true;
int v = code[i - 1] - 65;
s << v;
s >> v_s;
calculation = calculation + LOD + " " + v_s; operations.push_back(calculation); calculation = "";
s.clear();
}
else if (code[i] == '='&&code[i - 1] != ':') {
equal = true;
int v = code[i - 1] - 65;
s << v;
s >> v_s;
calculation = calculation + LOD + " " + v_s; operations.push_back(calculation); calculation = "";
s.clear();
}

else if (code[i]>=48&& code[i] <= 57) {
string con = "";
con=con + LDC + " ";
while (code[i] >= 48 && code[i] <= 57&&i<code.length()) {
con +=code[i++];
}
variables.push_back(con);
i--;
}
else if (code[i] >= 65 && code[i] <= 90 &&i!=0&& i + 1 < code.length()&&(code[i+1]<65|| code[i + 1]>90)) {
string con = "";
con = con + LOD + " ";
int v = code[i] - 65;
string v_ss;
stringstream ss;
ss << v;
ss >> v_ss;
con += v_ss;
variables.push_back(con);
ss.clear();
}
else if (code[i] >= 65 && code[i] <= 90&&i+1==code.length()&& (code[i + 1]<65 || code[i + 1]>90)) {
string con = "";
con=con + LOD + " ";
int v = code[i] - 65;
string v_ss;
stringstream ss;
ss << v;
ss >> v_ss;
con += v_ss;
variables.push_back(con);
ss.clear();
}

else if (code[i] == '+' || code[i] == '-' || code[i] == '*' || code[i] == '/')
{


if (code[i] == '+') {
calculation = ADD; real_operators.push_back(calculation); calculation = "";
}
if (code[i] == '-') {
calculation = SUB; real_operators.push_back(calculation); calculation = "";
}
if (code[i] == '*') {
calculation = MLT; real_operators.push_back(calculation); calculation = "";
}
if (code[i] == '/') {
calculation = DIV; real_operators.push_back(calculation); calculation = "";
}
}
}

for (int i = 0; i < variables.size(); i++) {
operations.push_back(variables[i]);
if (i > 0) {
operations.push_back(real_operators[i - 1]);
}
}

if (equaltion) {
calculation = calculation + STR + " " + v_s; operations.push_back(calculation); calculation = "";
}
if (greater) {
calculation += GRT; operations.push_back(calculation); calculation = "";
}
if (lesser) {
calculation += LES; operations.push_back(calculation); calculation = "";
}
if (equal) {
calculation += EQL; operations.push_back(calculation); calculation = "";
}
return operations;
}
...全文
24 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧