📄 test.cpp
字号:
}
else if(readchar=='<'){
CheckLeft();
Concat();
if(searchpoint>=50)
break;
readchar=buffer[searchpoint];
searchpoint++;
if(readchar=='='){ //"<="
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=29;
if(searchpoint>=50)
break;
}
else if(readchar=='>'){ //"<>"
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=25;
if(searchpoint>=50)
break;
}
else{ //"<"
searchpoint--;
Insert();
Clear(strToken,21);
Dualist.tail->index=28;
if(searchpoint>=50)
break;
}
}
else if(readchar=='>'){
CheckLeft();
Concat();
if(searchpoint>=50)
break;
readchar=buffer[searchpoint];
searchpoint++;
if(readchar=='='){ //">="
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=27;
if(searchpoint>=50)
break;
}
else{
searchpoint--;
Insert();
if(strlen(strToken)==2)
Dualist.tail->index=25; //"<>"
else
Dualist.tail->index=26; //">"
Clear(strToken,21);
if(searchpoint>=50)
break;
}
}
else if(readchar=='('){
CheckLeft();
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=30;
if(searchpoint>=50)
break;
}
else if(readchar==')'){
CheckLeft();
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=31;
if(searchpoint>=50)
break;
}
else if(readchar==','){
CheckLeft();
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=32;
if(searchpoint>=50)
break;
}
else if(readchar==';'){
CheckLeft();
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=33;
if(searchpoint>=50)
break;
}
else if(readchar=='.'){
CheckLeft();
Concat();
Insert();
Clear(strToken,21);
Dualist.tail->index=34;
if(searchpoint>=50)
break;
}
else if(readchar=='\n'){
linenum++;
CheckLeft();
strcat(strToken,"newline");
Insert();
Clear(strToken,21);
Dualist.tail->index=35;
if(searchpoint>=50)
break;
}
else if(readchar=='\0') //源文件结束标志,结束词法分析
break;
else //非法字符
printf("Line %d: unknown word.",linenum);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数为词法分析器
void WordAnalyser(){
int index=1;
int flagbuffer=false; //判断源文件是否读完标志
char strIndex[10];
Duatype* p;
initilize();
while(!isFile()); //直到正确打开文件
while(!flagbuffer){
flagbuffer=BeforehandInput(buffer);
WordAnalyse(buffer);
}
dfp=fopen("duality.txt","w");
for(p=Dualist.head->next;p!=NULL;p=p->next){
itoa(p->index,strIndex,10);
putc('(',dfp);
fputs(p->value,dfp);
putc(',',dfp);
fputs(strIndex,dfp);
putc(')',dfp);
putc('\n',dfp);
}
fclose(dfp);
}
///////////////////////////////////////////////////////////////////////////////////////
//查变量名表模块
//参数:待查变量的名称
//返回值:若查到,返回该变量在变量表中的位置;否则返回“-1”。
/*int searchVTable(char *temp)
{
int i;
i=0;
while(strcmp(VariableTable.table[i].vname,temp)!=0&&i<50)
{
i++;
}
if(i==50) return -1;
return i;
}*/
int searchVTable(char *v);
void enterVTable(int flag);
///////////////////////////////////////////////////////////////////////////////////////
//回填模块
//参数:要回填的四元式序号,要跳转到的四元式序号
//返回值:无
void BACKPATCH(int QuaS,int QuaA)
{
Qualist[QuaS].opr3.name=NULL; //常数
Qualist[QuaS].opr3.type=1;
Qualist[QuaS].opr3.offset=QuaA;
Qualist[QuaS].opr3.rlevel=0;
}
///////////////////////////////////////////////////////////////////////////////////////
//填四元式表模块
//参数:1个操作符,3个操作数
//返回值:无
void GEN(int strOperator,Operand op1,Operand op2,Operand op3)
{
Quatype temp; //四元式
if(isTrue==false) return;
temp.strOperator=strOperator; //给四元式操作码赋值
//给四元式第一操作数赋值
temp.opr1.type=op1.type;
if(op1.type==-1) //不使用
{
temp.opr1.name=NULL;
temp.opr1.offset=-1;
temp.opr1.rlevel=-1;
}
else if(op1.type==1) //常数
{
temp.opr1.name=NULL;
temp.opr1.offset=op1.offset; //常数值
temp.opr1.rlevel=0;
}
else if(op1.type==2) //临时变量
{
temp.opr1.name=op1.name;
temp.opr1.offset=op1.offset;
temp.opr1.rlevel=0;
}
else //变量
{
temp.opr1.name=op1.name;
temp.opr1.offset=op1.offset;
temp.opr1.rlevel=op1.rlevel;
}
//给四元式第二操作数赋值
temp.opr2.type=op2.type;
if(op2.type==-1) //不使用
{
temp.opr2.name=NULL;
temp.opr2.offset=-1;
temp.opr2.rlevel=-1;
}
else if(op2.type==1) //常数
{
temp.opr2.name=NULL;
temp.opr2.offset=op2.offset; //常数值
temp.opr2.rlevel=0;
}
else if(op2.type==2) //临时变量
{
temp.opr2.name=op2.name;
temp.opr2.offset=op2.offset;
temp.opr2.rlevel=0;
}
else //变量
{
temp.opr2.name=op2.name;
temp.opr2.offset=op2.offset;
temp.opr2.rlevel=op2.rlevel;
}
//给四元式第三操作数赋值
temp.opr3.type=op3.type;
if(op3.type==-1) //不使用
{
temp.opr3.name=NULL;
temp.opr3.offset=-1;
temp.opr3.rlevel=-1;
}
else if(op3.type==1) //常数
{
temp.opr3.name=NULL;
temp.opr3.offset=op3.offset; //常数值
temp.opr3.rlevel=0;
}
else if(op3.type==2) //临时变量
{
temp.opr3.name=op3.name;
temp.opr3.offset=op3.offset;
temp.opr3.rlevel=0;
}
else //变量
{
temp.opr3.name=op3.name;
temp.opr3.offset=op3.offset;
temp.opr3.rlevel=op3.rlevel;
}
Qualist[PC++]=temp;
}
///////////////////////////////////////////////////////////////////////////////////////
//过程调用合理性检查模块
//参数:被调用过程的名字
//返回值:调用合理返回过程序号,不合理返回-1
int CHECK(char *temp)
{
int i,j;
i=0;
while(strcmp(ProcTable.table[i].pname,temp)!=0&&i<20)
{
i++;
}
if(i==20) return -1; //检查是否已定义
if(ProcTable.table[i].paranum!=part.size()) return -1; //检查参数个数
j=level-ProcTable.table[i].plevel; //检查调用关系
if(j==-1) return i; //外过程调用内一层
if(j==0) return i; //同层调用(已定义)
if(j==1) //内过程调用外过程
{
j=spro.top(); //取该语句所在过程序号
if(ProcTable.table[j].outposition==i) return i;
}
return -1;
}
///////////////////////////////////////////////////////////////////////////////////////
//输出四元式模块
//参数:无
//返回值:无
void OUTQ(void)
{
char *temp;
int i;
FILE *fp;
Qualist[PC].strOperator=-1; //设置四元式表结束标志。(多输出一条无效的四元式)
fp=fopen("qualist.txt","w");
for(i=0;i<100;i++)
{
switch(Qualist[i].strOperator)
{
case 0:
temp=(char*)malloc(2*sizeof(char));
strcpy(temp,"a");
break;
case 1:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"add");
break;
case 2:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"sub");
break;
case 3:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"mul");
break;
case 4:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"div");
break;
case 5:
temp=(char*)malloc(7*sizeof(char));
strcpy(temp,"become");
break;
case 6:
temp=(char*)malloc(5*sizeof(char));
strcpy(temp,"call");
break;
case 7:
temp=(char*)malloc(7*sizeof(char));
strcpy(temp,"return");
break;
case 8:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"int");
break;
case 9:
temp=(char*)malloc(5*sizeof(char));
strcpy(temp,"stop");
break;
case 10:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"par");
break;
case 11:
temp=(char*)malloc(5*sizeof(char));
strcpy(temp,"jump");
break;
case 12:
temp=(char*)malloc(3*sizeof(char));
strcpy(temp,"j>");
break;
case 13:
temp=(char*)malloc(3*sizeof(char));
strcpy(temp,"j<");
break;
case 14:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"j>=");
break;
case 15:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"j<=");
break;
case 16:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"j<>");
break;
case 17:
temp=(char*)malloc(3*sizeof(char));
strcpy(temp,"j=");
break;
case 18:
temp=(char*)malloc(5*sizeof(char));
strcpy(temp,"read");
break;
case 19:
temp=(char*)malloc(7*sizeof(char));
strcpy(temp,"readln");
break;
case 20:
temp=(char*)malloc(6*sizeof(char));
strcpy(temp,"write");
break;
case 21:
temp=(char*)malloc(8*sizeof(char));
strcpy(temp,"writeln");
break;
case 22:
temp=(char*)malloc(5*sizeof(char));
strcpy(temp,"main");
break;
default:
temp=(char*)malloc(4*sizeof(char));
strcpy(temp,"^_^");
break;
}
// printf("*****************************************************************************\n");
printf("%2d%8s%12s,%2d,%3d,%2d%12s,%2d,%3d,%2d%12s,%2d,%3d,%2d\n",i,temp,Qualist[i].opr1.name,
Qualist[i].opr1.type,Qualist[i].opr1.offset,Qualist[i].opr1.rlevel,Qualist[i].opr2.name,
Qualist[i].opr2.type,Qualist[i].opr2.offset,Qualist[i].opr2.rlevel,Qualist[i].opr3.name,
Qualist[i].opr3.type,Qualist[i].opr3.offset,Qualist[i].opr3.rlevel);
fprintf(fp,"%2d%8s%8d,%3d,%2d%8d,%3d,%2d%8d,%3d,%2d\n",i,temp,
Qualist[i].opr1.type,Qualist[i].opr1.offset,Qualist[i].opr1.rlevel,
Qualist[i].opr2.type,Qualist[i].opr2.offset,Qualist[i].opr2.rlevel,
Qualist[i].opr3.type,Qualist[i].opr3.offset,Qualist[i].opr3.rlevel);
if(i%23==0) scanf("%c",temp);
if(Qualist[i].strOperator==-1) return;
}
fclose(fp);
}
///////////////////////////////////////////////////////////////////////////////////////
//LR语义处理总控模块
bool LRSemanticor(int productor)
{
switch(productor)
{
char *value; //用于存放规约式的字符名
char c[4];
int num; //用于存放变量在变量表中的下标
Operand temp1,temp2,temp3; //用于存放操作数
case 1: return true;
case 2: return true;
case 3: return true;
case 4: return true;
case 5:
value=signCache[2]; //取ident
num=searchVTable(value); //查变量表
if(num==-1) //变量未定义
{
enterVTable(1);
return false;
}
temp3.name=value; //该变量为目的操作数
temp3.offset=VariableTable.table[num].relposition;
temp3.rlevel=level-VariableTable.table[num].plevel;
temp3.type=0;
temp1=place.top(); //取第一操作数
place.pop();
temp2.type=-1; //第二操作数不使用
GEN(5,temp1,temp2,temp3); //四元式:赋值
return true;
case 6:
BACKPATCH(chain.top(),PC); //回填
chain.pop();
return true;
case 7:
BACKPATCH(chain.top(),PC); //回填
chain.pop();
return true;
case 8:
temp3.type=1;
temp3.name=NULL;
temp3.rlevel=0;
temp3.offset=chain.top();
temp1.type=temp2.type=-1;
GEN(11,temp1,temp2,temp3);
BACKPATCH(chain.top(),PC);
chain.pop();
return true;
case 9:
temp3.type=1;
temp3.name=NULL;
temp3.offset=chain.top();
temp3.rlevel=0;
temp1.type=temp2.type=-1;
GEN(11,temp1,temp2,temp3);
BACKPATCH(chain.top(),PC);
chain.pop();
return true;
case 10:
value=signCache[3]; //取过程名
num=CHECK(value); //检查过程调用是否合理
if(num==-1) return false;
temp1.type=temp2.type=-1;
while(part.size()!=0)
{
temp3=part.front();
part.pop();
GEN(10,temp1,temp2,temp3);
}
temp1.type=1; //常数
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -