📄 mycompiler.cpp
字号:
getsym();
if(strcmp(sym,"becomes")==0||strcmp(sym,"eql")==0)
{
if(strcmp(sym,"eql")==0)
error(1);
getsym();
if(strcmp(sym,"number")==0)
{
enter("const");
}
else
{
error(2);
}
}
else
{
error(4);
}
}
}
else
{
error(4);
}
}
void MyCompiler::vardeclaration(char *type)
{
int i;
if(ch=='[')
{
i_isarray=1;//shen申明数组
getsym(); //
i=position(id);
if(i==0)
{
while(strcmp(sym,"lsquarebrackets")==0)
{
getsym(); //某一维大小num
if(strcmp(sym,"number")!=0)
error(27);
enter(type);
getsym();
if(strcmp(sym,"rsquarebrackets")!=0)
error(26);
getsym();
i_isarray=2;//数组已经初始化过
}
i_isarray=3; //数组申明结束前的处理
enter(type);
}
else
{
error(0);
getsym();
}
i_isarray=0; //数组申明结束
return;
}
if(strcmp(sym,"ident")==0)
{
i=position(id);
if(i!=0)
{
error(0);
getsym();
}
else
{
enter(type);
}
}
else
{
error(4);
}
}
int MyCompiler::factor(MyCompiler::node *fsys)
{
int factor_type; //因子类型,当factor_type值为0时,因子类型为常数或者常量
int m=0,n=0,i; //当factor_type值为1时,因子类型为整型变量,当factor_type
char *tempset[]={"rparen",NULL}; //值为4时,因子类型为长整型变量.函数最后要返回该变量的值.
char *factorendset[]={"rsquarebrackets",NULL};
node *temp,*factor;
temp=new node;
factor=new node;
while(tempset[m]!=NULL)
temp->pa[n++]=tempset[m++];
temp->pa[n]=NULL;
n=0;m=0;
while(factorendset[m]!=NULL)
factor->pa[n++]=factorendset[m++];
factor->pa[n]=NULL;
//factor=add(factor,fsys);
/////开始符号,结束符号
test(facbegsys,fsys,25);
while(in(sym,facbegsys)==1)
{
if(strcmp(sym,"ident")==0)
{
i=position(id);
if(i==0)
{
error(11);
}
else
{
if(strcmp(IdentTable[i].kind,"array")==0)
{
int i_elementno=1;
int i_arrayadr=IdentTable[i].val;
int i_elementcount=ArrayTable[IdentTable[i].val].count;
int i_arrayindex=ArrayTable[IdentTable[i].val].i_startadr;
int i_count=0;//元素相对于数组起始位置的位移
int i_longflag=0;//数组地址是否long
getsym();
if(strcmp(sym,"lsquarebrackets")!=0)
error(29);
gen("lit",0,0,line);
while(strcmp(sym,"lsquarebrackets")==0)//标识符的类型是array
{
i_elementno++;
i_count=1;
getsym(); //某一维大小num
//gen("lit",0,num,line);
int expression_typearray=expression(fsys);
for(int i=i_elementno;i<=i_elementcount;i++)
i_count=i_count*(ArrayTable[i_arrayadr].elementsize[i]);
gen("lit",0,i_count,line);
//i_count=i_count*IdentTable[i].val;
if(expression_typearray==4&&i_longflag==0) //
{
//gen("opr",1,4,line); //int*long
//i_longflag=1;
error(41);
}
else
if(expression_typearray==4&&i_longflag==1) //long*long
//gen("opr",0,4,line);
error(41);
else
if(i_longflag==0) //int*int
gen("opr",0,4,line);
else
error(41);
//gen("opr",0,4,line); //long *int
//getsym();
gen("opr",0,2,line); //
if(strcmp(sym,"rsquarebrackets")!=0)
error(26);
getsym();
}
//i_arrayindex=i_arrayindex+i_count;
if(strcmp("long",IdentTable[i].BackKind)==0) //数组,long 类型
{
gen("lit",0,2,line); //2*index
gen("opr",0,4,line); //int*int
}
//将常量方到栈顶
gen("lit",IdentTable[i].level,IdentTable[i].adr,line);
gen("opr",0,2,line);
//gen("stoex",IdentTable[i].level,0,line);
if(strcmp("long",IdentTable[i].BackKind)==0) //数组
{
factor_type=4;
gen("lodex",0,1,line); //lod long to top
}
else //int,char array
{
factor_type=0;
gen("lodex",0,0,line); //int int ,char to top
}
// ArrayTable[i_arrayadr].i_elementindex++;
// ArrayTable[i_arrayadr].arrayadr[ArrayTable[i_arrayadr].i_elementindex]=i_arrayindex;
// IdentTable[i].val=i_arrayindex;
// i_isarray=3; //数组结束前的处理
//getsym();
}
else
{
if(strcmp(IdentTable[i].kind,"const")==0)
{
factor_type=0;
gen("lit",0,IdentTable[i].val,line);
}
else
if(strcmp(IdentTable[i].kind,"int")==0)
{
factor_type=1;
gen("lod",IdentTable[i].level,IdentTable[i].adr,line);
}
else
if(strcmp(IdentTable[i].kind,"long")==0)
{
factor_type=4;
gen("lod",IdentTable[i].level,IdentTable[i].adr-1,line);
gen("lod",IdentTable[i].level,IdentTable[i].adr,line);
}
else
{
error(4);
}
getsym();
}
}
}
else
if(strcmp(sym,"number")==0)
{
factor_type=0;
gen("lit",0,num,line);
getsym();
}
else
if(strcmp(sym,"lparen")==0)
{
getsym();
factor_type=expression(add(temp,fsys));
if(strcmp(sym,"rparen")==0)
getsym();
else
error(22);
}
else
error(11);
}
//合法结束符
factor=add(factor,fsys);
test(factor,facbegsys,4);
delete temp;
return factor_type;
}
int MyCompiler::term(MyCompiler::node *fsys)
{
int factor_type;
int term_type,term_type1;
int opr_type; //表示操作类型,opr_type为0时,表示两个操作数中只有常数,常量或整形变量
int i=0,j=0; //opr_type为1时,表示两个操作数都是长整型变量,opr_type为2时,表示第一
char mulop[10]; //个操作数为常数或常量,第二数为操作数为长整型变量,opr_type为3时与opr_type为2时相反
char *tempset[]={"times","slash",NULL};
node *temp;
temp=new node;
while(tempset[i]!=NULL)
temp->pa[i++]=tempset[j++];
temp->pa[i]=NULL;
term_type=factor(add(temp,fsys));
while(in(sym,temp)==1)
{
strcpy(mulop,sym);
getsym();
factor_type=factor(add(temp,fsys));
term_type1=factor_type+term_type;
switch(term_type1)
{
case 0:term_type=0;
opr_type=0;
break;
case 1:term_type=1;
opr_type=0;
break;
case 2:term_type=1;
opr_type=0;
break;
case 4:term_type=4;
if(factor_type==0) //long *int
opr_type=1;
else
opr_type=2;
break;
case 5://error(99); //int*long 2,
term_type=4;
if(factor_type==1) //long *int
opr_type=1;
else
opr_type=2; //long*long
break;
case 8:term_type=4; //long*long
opr_type=3;
break;
}
if(strcmp(mulop,"times")==0)
gen("opr",opr_type,4,line);
else
gen("opr",opr_type,5,line);
}
delete temp;
return term_type;
}
int MyCompiler::expression(MyCompiler::node *fsys)
{
int expression_type,expression_type1;
int term_type;
int opr_type;
int m=0,n=0;
char addop[10];
char *tempset[]={"plus","minus",NULL};
node *temp;
temp=new node;
while(tempset[m]!=NULL)
temp->pa[n++]=tempset[m++];
temp->pa[n]=NULL;
if(in(sym,temp)==1)
{
strcpy(addop,sym);
getsym();
expression_type=term(add(fsys,temp));
if(strcmp(addop,"minus")==0)
if(expression_type==4)
gen("opr",1,1,line);
else
gen("opr",0,1,line);
}
else
{
expression_type=term(add(fsys,temp));
}
while(in(sym,temp)==1)
{
strcpy(addop,sym);
getsym();
term_type=term(add(fsys,temp));
expression_type1=expression_type+term_type;
switch(expression_type1)
{
case 0:expression_type=0;
opr_type=0;
break;
case 1:expression_type=1;
opr_type=0;
break;
case 2:expression_type=1;
opr_type=0;
break;
case 4:expression_type=4;
if(term_type==0) //long+int
opr_type=3;
else
opr_type=2;
break;
case 5://error(99);
expression_type=4;
if(term_type==1) //
opr_type=3;
else
opr_type=2;
break;
case 8:expression_type=4;
opr_type=3;
break;
}
if(strcmp(addop,"plus")==0)
gen("opr",opr_type,2,line);
else
gen("opr",opr_type,3,line);
}
delete temp;
return expression_type;
}
void MyCompiler::condition(MyCompiler::node *fsys)
{
int i=0,j=0;
char *tempset[]={"or",NULL};
node *temp;
temp=new node;
while(tempset[i]!=NULL)
temp->pa[i++]=tempset[j++];
temp->pa[i]=NULL;
conditionterm(add(temp,fsys));
while(in(sym,temp)==1)
{
getsym();
conditionterm(add(temp,fsys));
gen("opr",0,15,line);
}
delete temp;
}
void MyCompiler::conditionterm(MyCompiler::node *fsys)
{
int i=0,j=0;
char *tempset[]={"and",NULL};
node *temp;
temp=new node;
while(tempset[i]!=NULL)
temp->pa[i++]=tempset[j++];
temp->pa[i]=NULL;
conditionfactor(add(temp,fsys));
while(in(sym,temp)==1)
{
getsym();
conditionfactor(add(temp,fsys));
gen("opr",0,14,line);
}
delete temp;
}
void MyCompiler::conditionfactor(MyCompiler::node *fsys)
{
int expression_type,expression_type1;
int factor_type;
int opr_type;
int i=0,j=0;
char relop[10];
char *tempset[]={"eql","neq","lss","leq","gtr","geq",NULL};
char *tempset1[]={"rparen",NULL};
node *temp,*temp1;
temp=new node;
temp1=new node;
while(tempset[i]!=NULL)
temp->pa[j++]=tempset[i++];
temp->pa[j]=NULL;
i=0;j=0;
while(tempset1[i]!=NULL)
temp1->pa[j++]=tempset1[i++];
temp1->pa[j]=NULL;
test(conditionbegsys,fsys,25);
while(in(sym,conditionbegsys)==1)
{
if(strcmp(sym,"not")==0) //非操作编译
{
getsym();
factor_type=factor(fsys);
if(factor_type==4)
opr_type=1;
else
opr_type=0;
gen("opr",opr_type, 6,line);
}
else
if(strcmp(sym,"lparen")==0)//条件递归
{
getsym();
condition(add(temp1,fsys));
if(strcmp(sym,"rparen")==0)
getsym();
else
error(22);
}
else
{
expression_type=expression(add(temp,fsys));
if(in(sym,temp)==0)
{
if(in(sym,fsys))//表达式作为条件因子
{
if(expression_type==4)
opr_type=1;
else
opr_type=0;
gen("opr",opr_type, 7,line);
}
else
{
error(20);
}
}
else
{
strcpy(relop,sym);
getsym();
expression_type1=expression(fsys);
expression_type=expression_type+expression_type1;
if(expression_type<3)
{
opr_type=0;
}
else
if(expression_type==8)
{
opr_type=1;
}
else
{ if(expression_type==4)
opr_type=2;
else
{
error(202);
opr_type=0;
}
}
if(strcmp(relop,"eql")==0) //相等操作编译
{
gen("opr",opr_type,8,line);
}
else
if(strcmp(relop,"neq")==0) //不相等操作编译
{
gen("opr",opr_type,9,line);
}
else
if(strcmp(relop,"lss")==0) //小于操作编译
{
gen("opr",opr_type,10,line);
}
else
if(strcmp(relop,"leq")==0)//小于等于编译
{
gen("opr",opr_type,11,line);
}
else
if(strcmp(relop,"gtr")==0)//大于编译
{
gen("opr",opr_type,12,line);
}
else//大于等于编译
{
//if(strcmp(relop,"geq")==0)
gen("opr",opr_type,13,line);
}
}
}
}
test(fsys,conditionbegsys,4);
delete temp;
delete temp1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -