📄 mycompiler.cpp
字号:
// MyMyCompilerr.cpp: implementation of the MyMyCompilerr class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyCompiler.h"
#include "c02ide.h"
#include "MainFrm.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MyCompiler::MyCompiler()
{
str_filepath=
KeyWord[0]="define";
KeyWord[1]="int";
KeyWord[2]="short";
KeyWord[3]="void";
KeyWord[4]="if";
KeyWord[5]="else";
KeyWord[6]="long";
KeyWord[7]="register";
KeyWord[8]="for";
KeyWord[9]="do";
KeyWord[10]="while";
KeyWord[11]="return";
KeyWord[12]="const";
KeyWord[13]="break";
KeyWord[14]="signed";
KeyWord[15]="unsigned";
KeyWord[16]="main";
KeyWord[17]="char";
// KeyWord[18]="char";
KeyWord[18]="struct";
KeyWord[19]="typedef";
ch_count=1;
line_len=0;
ch=' ';
line=-1;
i_errno=0;
subcount=0;
tx=0;
dx=1;
lev=0;
i_cultx=0;
i_isarray=0;
i_atx=0;
labcount[0]='l';
labcount[1]='a';
labcount[2]=96;
labcount[3]=0;
cx=0;
i_pass=0;
//语句开始符号集
statbegsys=(node *)malloc(sizeof(node));
statbegsys->pa[0]="if";
statbegsys->pa[1]="ident";
statbegsys->pa[2]="while";
statbegsys->pa[3]="lbigparen";
statbegsys->pa[4]="for";
statbegsys->pa[5]="return";
statbegsys->pa[6]="else";
statbegsys->pa[7]="do";
statbegsys->pa[8]=NULL;
//申明语句开始符号集
declbegsys=(node *)malloc(sizeof(node));
declbegsys->pa[0]="int";
declbegsys->pa[1]="long";
declbegsys->pa[2]="void";
declbegsys->pa[3]="char";
declbegsys->pa[4]="array";
declbegsys->pa[5]=NULL;
//因子开始符号集
facbegsys=(node *)malloc(sizeof(node));
facbegsys->pa[0]="ident";
facbegsys->pa[1]="number";
facbegsys->pa[2]="lparen";
facbegsys->pa[3]=NULL;
//语句结束符
tempsetsys=(node *)malloc(sizeof(node));
tempsetsys->pa[0]="semicolon";//分号
// tempsetsys->pa[1]="rbigparen";
tempsetsys->pa[1]=NULL;
//条件语句开始符号集
conditionbegsys=(node *)malloc(sizeof(node));//条件语句开始符
conditionbegsys->pa[0]="ident";
conditionbegsys->pa[1]="number";
conditionbegsys->pa[2]="lparen";
conditionbegsys->pa[3]="not";
conditionbegsys->pa[4]=NULL;
//中间代码指令
operation[0]="ini";
operation[1]="lit";
operation[2]="lod";
operation[3]="sto";
operation[4]="substo";
operation[5]="subpush";
operation[6]="subpop";
operation[7]="jmp";
operation[8]="sub";
operation[9]="goto";
operation[10]="gosub";
operation[11]="jpc";
operation[12]="jfc";
operation[13]="return";
operation[14]="main";
operation[15]="resever";
operation[16]="opr";
operation[17]="stoex";
operation[18]="lodex";
}
MyCompiler::~MyCompiler()
{
int i;
struct param *p1,*p2;
for(i=0;i<subcount;i++)
{
delete subparam[i].kind;
p1=subparam[i].p;
while(p1!=NULL)
{
delete p1->kind;
p2=p1;
p1=p1->p;
delete p2;
}
}
}
void MyCompiler::Init()
{
ch_count=1;
line_len=0;
ch=' ';
line=-1;
i_errno=0;
subcount=0;
tx=0;
dx=1;
lev=0;
i_cultx=0;
i_isarray=0;
i_atx=0;
labcount[0]='l';
labcount[1]='a';
labcount[2]=96;
labcount[3]=0;
cx=0;
i_pass=0;
CMDIFrameWnd * nowframe = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
//活动子窗口指针
CMDIChildWnd *nowchild = nowframe->MDIGetActive();
m_mappro.Lookup(nowchild->GetSafeHwnd(),(void*&)m_pfile);
m_pfile->oa_funcinfstru.RemoveAll();
}
void MyCompiler::get_ch()
{
if(ch_count==line_len+1)
{
// line_len=0;
ch_count=0;
fin.get(buf,300);
fin.get();
line++;
line_len=strlen(buf);
if(fin.eof())
ch='@';
else
ch='\n';
buf[line_len]=ch;
}
ch=buf[ch_count++];
if(ch>='A'&&ch<='Z')
ch=ch+32;
}
void MyCompiler::getsym()
{
int i,k;
while(ch==' '||ch=='\t'||ch=='\n') //过滤文件中的空格、tab键和回车
{
get_ch();
}
if(ch>='a'&&ch<='z') //如果是一字母为开头的字符串,
{ //则是标示符,并且拼写标示符
k=0;
do{
if(k<AL)
{
id[k]=ch;
k++;
}
get_ch();
}while((ch>='a'&&ch<='z'||ch=='_')||(ch>='0'&&ch<='9'));
if(k<AL)
{
while(k<AL)
{
id[k++]=0;
}
}
i=0;
k=NORW;
do { //在关键字表中查询拼写的单词
if(strcmp(id,KeyWord[i])==0) //是否是保留字
k=i;
i++;
}while(i<NORW);
if(k<NORW)
strcpy(sym,KeyWord[k]);
else
strcpy(sym,"ident");
}
else
if(ch>='0' && ch<='9'){
k=0;
num=0;
strcpy(sym,"number");
do{ //拼写数字
num=10*num+(int)ch-'0';
k++;
get_ch();
}while(ch>='0'&&ch<='9');
}
else
if(ch=='=')
{
get_ch();
if(ch=='=')
{
strcpy(sym,"eql");
get_ch();
}
else
{
strcpy(sym,"becomes");
}
}
else
if(ch=='<')
{
get_ch();
if(ch=='=')
{
strcpy(sym,"leq");
get_ch();
}
else
if(ch=='<')
{
get_ch();
if(ch=='*')
{
strcpy(sym,"jinlshift");
get_ch();
}
else
{
strcpy(sym,"lshift");
}
}
else
{
strcpy(sym,"lss");
}
}
else
if(ch=='>')
{
get_ch();
if(ch=='=')
{
strcpy(sym,"geq");
get_ch();
}
else
{
strcpy(sym,"gtr");
}
}
else
if(ch=='+')
{
get_ch();
if(ch=='+')
{
strcpy(sym,"increase");
get_ch();
}
else
{
strcpy(sym,"plus");
}
}
else
if(ch=='-')
{
get_ch();
if(ch=='-')
{
strcpy(sym,"decrease");
get_ch();
}
else
{
strcpy(sym,"minus");
}
}
else
if(ch=='&')
{
get_ch();
if(ch=='&')
{
strcpy(sym,"and");
get_ch();
}
else
{
strcpy(sym,"bitand");
}
}
else
if(ch=='|')
{
get_ch();
if(ch=='|')
{
strcpy(sym,"or");
get_ch();
}
else
{
strcpy(sym,"bitor");
}
}
else
if(ch=='!')
{
get_ch();
if(ch=='=')
{
strcpy(sym,"neq");
get_ch();
}
else
{
strcpy(sym,"not");
}
}
else
{
switch(ch){
case'@':strcpy(sym,"over");break;
case'^':strcpy(sym,"notor");break;
case'*':strcpy(sym,"times");break;
case'/':strcpy(sym,"slash");break;
case'(':strcpy(sym,"lparen");break;
case')':strcpy(sym,"rparen");break;
case',':strcpy(sym,"comma");break;
case';':strcpy(sym,"semicolon");break;
case'{':strcpy(sym,"lbigparen");break;
case'}':strcpy(sym,"rbigparen");break;
case'[':strcpy(sym,"lsquarebrackets");break;
case']':strcpy(sym,"rsquarebrackets");break;
default :strcpy(sym,"not exist");
error(3);
break;
}
get_ch();
}
}
void MyCompiler::error(int n)
{
char *err;
switch(n)
{
case 0: err="标示符重复定义";break;
case 1: err="==错误";break;
case 2: err="在 = 应该是数字";break;
case 3: err="字符错误";break;
case 4: err="语法不匹配";break;
case 5: err="缺少main函数";break;
case 6: err="语句结束符不合法";break;
case 10: err="缺少语句结束符";break;
case 11: err="标示符未定义";break;
case 12: err="语句结束符不合法";break;
case 13: err="main函数定义错误";break;
case 15: err="程序没有结束";break;
case 16: err="函数名未声明,不能使用";break;
case 17: err="函数形参声明出错";break;
case 18: err="函数声明中缺少右括号 ";break;
case 19: err="函数声明末尾缺少 ';'";break;
case 20: err="条件表达式有错误";break;
case 21: err="函数声明类型与定义类型不一致";break;
case 22: err="表达式中缺少 ')' ";break;
case 23: err="函数定义中缺少左括号 ";break;
case 24: err="函数声明类型与定义参数个数不一致";break;
case 25: err="表达式不正确";break;
case 26: err="缺少右方括号";break;
case 27: err="缺少数字指明数组这一维的大小";break;
case 28: err="子函数参数类型语申明不匹配";break;
case 29: err="数组使用缺少左方括号";break;
case 31: err="函数定义的参数比声明的参数少";break;
case 32: err="函数定义的参数比声明的参数多";break;
case 41: err="数组的下标请用int类型";break;
case 55: break;
case 72:err="if—else语句不匹配";break;
case 73:err="复合语句中缺少 '}' ";break;
case 74:err="缺少main函数";break;
case 93:err="赋值符号错误";break;
case 94:err="赋值类型不匹配";break;
case 98:err="void不能定义变量类型";break;
case 99: err="语法错误";break;
default : err="语法错误";
}
ferror<<str_filepath<<"("<<line+1<<"): "<<"error "<<n<<err<<endl;
struct ErrStruct *p_err;
p_err=new ErrStruct;
CMDIFrameWnd * nowframe = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
//活动子窗口指针
CMDIChildWnd *nowchild = nowframe->MDIGetActive();
p_err->m_pChildWnd=nowchild;
p_err->i_errline=line-1;
oa_error.Add((CObject*)p_err);
i_errno++;
}
struct MyCompiler::node *MyCompiler::add(MyCompiler::node *set1, MyCompiler::node *set2)
{
int i=0,j=0,k=0,cnt;
node *pt;
pt=(node *)malloc(sizeof(node));
for(cnt=0;cnt<32;cnt++)
pt->pa[cnt]=(char*)malloc(15*sizeof(char));
while(set1->pa[i]!=NULL)
strcpy(pt->pa[j++],set1->pa[i++]);
while(set2->pa[k]!=NULL)
{
if(in(set2->pa[k],set1)==0)
strcpy(pt->pa[j++],set2->pa[k++]);
else
k++;
}
pt->pa[j]=NULL;
return pt;
}
int MyCompiler::in(char *str, MyCompiler::node *set)
{
int i=0;
if(str==NULL||str=="")
return(0);
while(set->pa[i]!=NULL)
{
if(strcmp(str,set->pa[i])==0)
return(1);
else
i++;
}
return(0);
}
//生成一行三元式
void MyCompiler::gen(char *x, int y, int z, int line)
{
code[cx].operation=x;
code[cx].l=y;
code[cx].a=z;
code[cx].sourceline=line;
cx++;
}
void MyCompiler::test(MyCompiler::node *s1, MyCompiler::node *s2, int n)
{
if(in(sym,s1)==0)
{
error(n);
s1=add(s1,s2);
while(in(sym,s1)==0&&strcmp(sym,"over")!=0)
getsym();
}
}
int MyCompiler::position(char ident[15])
{
int i;
strcpy(IdentTable[0].name,ident);
i=tx;
while(strcmp(IdentTable[i].name,ident)!=0)
i--;
return i;
}
char *MyCompiler::newlab()
{
if(labcount[2]<'z')
labcount[2]++;
else{
labcount[1]++;
labcount[2]='a';
}
return labcount ;
}
void MyCompiler::constdeclaration()
{
int i;
if(strcmp(sym,"ident")==0)
{
i=position(id);
if(i!=0)
{
error(0);
getsym();
getsym();
getsym();
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -