一个简单的C语言编译器

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

个简单的C语言编译器

源代码:

//

//

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include

#include

#include

#include

#include

#include

using namespace std;

class Symbol

{

public:

int line;

string word;

char group;

Symbol();

Symbol(const Symbol &b);

virtual ~Symbol();

operator =(const Symbol &b);

string code;

};

class Label

{

public:

Label();

virtual ~Label();

string text;

private:

int n;

static int next();

static int _label;

};

class Action

{

public:

static int lookUp(char v,int s); private:

Action();

~Action();

static int Table[54][19];

static string vs;

};

class Goto

{

public:

static int lookUp(char v,int s); private:

Goto();

~Goto();

static int Table[54][9];

static string vs;

};

class Compiler

{

public:

optimize();

string code;

char nextChar();

preProcess();//预处理器

parser();//语法分析器

Symbol *lexer();//词法分析器

void emitter();//生成器

Compiler(string CmdLine);

virtual ~Compiler();

err(int no,int line);

int hasError;//错误发生状态

private:

int lookup(string m);

char currentChar;

string fileName;

int line;//行数状态

Compiler();

int hasFile;//源文件打开状态

int needOutSuppose;//输出支持状态

ifstream in;//输入CRR文件

ofstream log;//输出日志文件

ofstream out;//输出ASM文件

list symbolList;//符号表

};

Compiler::Compiler(string CmdLine)

{

line=1;

hasError=0;

needOutSuppose=0;

hasFile=0;

fileName=CmdLine;

log.open((fileName + "_Log.txt").c_str(),ios::out);

//char c; //测试nextChar()

//do{c=nextChar();log<

Compiler::Compiler()

{

}

Compiler::~Compiler()

{

log.close();

}

Symbol *Compiler::lexer() {

char c;

int s=1;

Symbol *r;

r=new Symbol();

c=currentChar;

while(s){

switch(s){

case 1:

if(c=='\x0A')line++;

else if(isspace(c))

s=1;

else if(isalpha(c)||c=='_'){ s=2;

r->word=c;

}

else if(isdigit(c)){

s=3;

r->word=c;

}

else{

switch(c){

case '+':

case '-':

s=0;

r->word=c;

r->group='+';

r->line=line;

相关文档
最新文档