⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个类c语言的解释器
💻 CPP
字号:
/*******************************************************
main.c
武汉大学pp国际软件学院软件工程05级7班
崔灿
200532580235
2007-9-21
********************************************************/

#include"globals.h"
#include"scan.h"
#include "tree.h"
#include "parse.h"
#include<stdio.h>
#include <windows.h> 
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

FILE* source;
FILE* listing;
int lineno;
int solely;
TokenRecord currentRecord;
HANDLE hStdout,hStdin;
vector<TokenRecord>* tokenList;
vector<string>* errorList;
bool scan_err;
bool parse_err;
bool scan_only;
int main(int argc, char *argv[]){
	char filename[255];
    if(argc<2){
        printf("请输入文件名\n");
        return 1;
    }
	int i = 0;
	solely=1;
	strcpy(filename,argv[1]);
	if(argc>2){
		i = atoi(argv[2]);
	}
	else{
		i = 3;
	}
	/*scan_only = false;
	if (i==1)
	{
		scan_only = true;
		solely = 0;
	}
	else if(i==2){
		scan_only = false;
		solely = 0;
	}*/
	DWORD dwRead, dwWritten; 
	scan_err = false;
	parse_err = false;
	tokenList = new vector<TokenRecord>;
	errorList = new vector<string>;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
	hStdin = GetStdHandle(STD_INPUT_HANDLE);
	source = fopen(filename,"r");
	listing = fopen("c:\\scan.txt","w+");
	currentRecord = getToken();
	tokenList->push_back(currentRecord);
	do 
	{
		if(i==1)
			printToken(currentRecord);
		currentRecord = getToken();
		tokenList->push_back(currentRecord);
	} 
    while(currentRecord.tp!=_ENDFILE);
	tokenList->push_back(currentRecord);
	/*if(i==1){
		printToken(currentRecord);
		CloseHandle(hStdout);
		CloseHandle(hStdin);
	}*/
	TreeNode *t;
	string out = "";
	if (!scan_err&&i!=1)
	{
		t = parse();
		if (i==2)
		{
			toString(t,0,out);
			if (parse_err)
			{
				ZeroMemory(chBuf,strlen(chBuf));
				strcpy(chBuf,"FALSE,按任意键显示错误列表");
				WriteFile(hStdout, chBuf, strlen(chBuf), &dwWritten, NULL);
				out = "parse error, can't build a syntax tree";
				int size = errorList->size();
				for (int i=0;i<size;i++)
				{
					out += "\n" + errorList->at(i);
				}
			}
			else {
				ZeroMemory(chBuf,strlen(chBuf));
				strcpy(chBuf,"TRUE,按任意键显示语法树");
				WriteFile(hStdout, chBuf, strlen(chBuf), &dwWritten, NULL);
				//生成中间代码
				string s(filename);
				s = s + ".cmmo";
				ofstream ofs;
				ofs.open(s.c_str());
				ofs<<".data\n";
				for (int i=0;i<data->size();i++)
				{
					//out = out+data->at(i)+"\n";
					ofs<<data->at(i)<<endl;
				}
				ofs<<".global_init"<<endl;
				for (int i=0;i<global_init->size();i++)
				{
					ofs<<global_init->at(i)<<endl;
				}
				ofs<<".code\n";
				int no = 0;
				for(int k=0;k<code->size();k++){
					//cout<<no++<<"  ";
					ofs<<code->at(k)<<endl;
					//out = out+code->at(k)+"\n";
				}
			}
			ReadFile( hStdin, chBuf, 1024, &dwRead, NULL);
			out = "\n"+out;
			out.append("\0");
			WriteFile(hStdout,out.c_str(),out.size(),&dwWritten,NULL);

		}
		else{
			if (!parse_err)
			{
				string s(filename);
				s = s + ".cmmo";
				ofstream ofs;
				ofs.open(s.c_str());
				ofs<<".data\n";
				for (int i=0;i<data->size();i++)
				{
					//out = out+data->at(i)+"\n";
					ofs<<data->at(i)<<endl;
				}
				ofs<<".global_init"<<endl;
				for (int i=0;i<global_init->size();i++)
				{
					ofs<<global_init->at(i)<<endl;
				}
				ofs<<".code\n";
				int no = 0;
				for(int k=0;k<code->size();k++){
					//cout<<no++<<"  ";
					ofs<<code->at(k)<<endl;
					//out = out+code->at(k)+"\n";
				}
			}
			else{
				for (int i=0;i<errorList->size();i++)
				{
					cout<<errorList->at(i)<<endl;
				}
			}
		}
		
	}
	else if(i!=1){
		ZeroMemory(chBuf,strlen(chBuf));
		strcpy(chBuf,"false");
		WriteFile(hStdout, chBuf, strlen(chBuf), &dwWritten, NULL);
		ZeroMemory(chBuf,strlen(chBuf));
		strcpy(chBuf,"词法分析有错误,请先词法分析");
		WriteFile(hStdout, chBuf, strlen(chBuf), &dwWritten, NULL);
	}
	CloseHandle(hStdout);
	CloseHandle(hStdin);
	/*ofstream of("c:\\1234.txt");
	of<<out;
	cout<<out;
	t = build_tree(out,0);
	getchar();*/
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -