📄 pl0complier.cpp
字号:
error(12);
i= 0;
}
getsym();
if(sym ==BECOMES)
getsym();
else
error(13);
expression(fsys,tx,lev);
if(sym!=SEMICOLON)
error(10);
if( i!= 0)
gen(STO,lev-table[i].other.inOther.level,table[i].other.inOther.adr);
}
else if( sym==READSYM)
{
getsym();
if( sym!=LPAREN)
error(34);
else
do{
getsym();
if (sym==IDENT)
i=position(id,tx);
else
i=0;
if( i==0 )
error(35);
else
{
gen(OPR,0,16);
gen(STO,lev-table[i].other.inOther.level,table[i].other.inOther.adr);
}
getsym();
}while(sym==COMMA);
if (sym!=RPAREN)
{
error(33);
while (fsys.find(sym)!=fsys.end()) getsym();
}
else
getsym();
}
else if( sym==WRITESYM)
{
getsym();
if (sym==LPAREN)
{
do{
getsym();
symset tmp=fsys;
for(int t=RPAREN;t<=COMMA;t++)
tmp.insert((symbol)t);
expression(tmp,tx,lev);
gen(OPR,0,14);
}while(sym==COMMA);
if (sym!=RPAREN)
error(33);
else
getsym();
}
gen(OPR,0,15);
}
else if( sym ==CALLSYM)
{
getsym();
if( sym!=IDENT)
error(14);
else
{
i= position(id,tx);
if (i == 0)
error(11);
else if (table[i].kind = PROCEDURE)
gen(CAL,lev-table[i].other.inOther.level,table[i].other.inOther.adr);
else
error(15);
getsym();
}
}
else if( sym ==IFSYM)
{
getsym();
symset tmp=fsys;
for(int i=THENSYM;i<=DOSYM;i++)
tmp.insert((symbol)i);
condition(tmp,tx,lev);
if( sym == THENSYM)
getsym();
else
error(16);
cx1= cx;
gen(JPC,0,0);
statement(fsys,tx,lev);
code[cx1].a= cx;
}
else if( sym ==BEGINSYM)
{
getsym();
symset tmp=fsys;
for(int i=SEMICOLON;i<=ENDSYM;i++)
tmp.insert((symbol)i);
statement(tmp,tx,lev);
tmp=statbegsys;
tmp.insert(SEMICOLON);
while( tmp.find(sym)!=tmp.end())
{
if(sourceEnd)return;
if (sym ==SEMICOLON||sym ==ENDSYM)
getsym();
else if(sym=PERIOD)
{
error(26);
getsym();
}
else
error(10);
tmp=fsys;
for(i=SEMICOLON;i<=ENDSYM;i++)
tmp.insert((symbol)i);
if(sourceEnd)return;
if(sym==ENDSYM)
break;
statement(tmp,tx,lev);
}
if( sym ==ENDSYM)
getsym();
else if(!sourceEnd)
error(17);
}
else if(sym ==DOSYM)
{
cx1= cx;
getsym();
symset tmp=fsys;
tmp.insert(WHILESYM);
statement(tmp,tx,lev);
if(sym ==WHILESYM)
getsym();
else
error(18);
condition(fsys,tx,lev);
gen(JPC,0,cx1);
}
symset setT;
test(fsys,setT,19);
}
void block(int lev,int tx,symset fsys)
{
if(sourceEnd)
return;
int dx;
int tx0;
int cx0;
dx= 3;
tx0= tx;
table[tx].other.inOther.adr= cx;
gen(JMP,0,0);
if( lev>levmax)
error(32);
do{
if( sym ==CONSTSYM)
{
getsym();
do{
constdeclaration(tx,dx,lev);
while (sym ==COMMA)
{
getsym();
constdeclaration(tx,dx,lev);
}
if (sym ==SEMICOLON)
getsym();
else
error(5);
}while(sym==IDENT);
}
if( sym ==VARSYM)
{
getsym();
do{
vardeclaration(tx,dx,lev);
while( sym ==COMMA){
getsym();
vardeclaration(tx,dx,lev);
}
if( sym ==SEMICOLON)
getsym();
else
error(5);
}while(sym==IDENT);
}
while( sym ==PROCSYM)
{
getsym();
if (sym ==IDENT)
{
enter(PROCEDURE,tx,dx,lev);
getsym();
}
else
error(4);
if( sym ==SEMICOLON)
getsym();
else
error(5);
symset tmp=fsys;
tmp.insert(SEMICOLON);
block(lev+1,tx,tmp);
if (sym ==SEMICOLON){
getsym();
symset tmp=statbegsys;
for(int i=IDENT;i<=PROCSYM;i++)
tmp.insert((symbol)i);
test(tmp,fsys,6);
}
else
error(5);
}
symset tmp=statbegsys;
tmp.insert(IDENT);
test(tmp,declbegsys,7);
}while(declbegsys.find(sym)!=declbegsys.end());
code[table[tx0].other.inOther.adr].a= cx;
table[tx0].other.inOther.adr= cx;
table[tx0].other.inOther.size=dx;
cx0= cx;
gen(INT,0,dx);
symset tmp=statbegsys;
for(int i=SEMICOLON;i<=ENDSYM;i++)
tmp.insert((symbol)i);
statement(tmp,tx,lev);
gen(OPR,0,0);
symset s2;
test(fsys,s2,8);
listcode(cx0);
}
int base(int l,int b,int s[])
{
int b1;
b1= b;
while(l>0){
b1= s[b1];
l= l-1;
}
return b1;
}
void interpret()
{
int err1=errorString.size();
if(err1>0)
{
cout<<"存在错误:"<<err1<<endl;
for(int i=0;i<err1;i++)
cout<<errorString[i]<<endl;
return;
}
const stacksize = 500;
int p=0,b=1,t=0;
instruction i;
int s[stacksize+1]={0};
cout<<" 成功!\n";
do{
i= code[p];
p= p+1;
switch(i.f)
{
case LIT:
t= t+1;
s[t]= i.a;
break;
case OPR:
switch(i.a)
{
case 0:
t= b-1;
p= s[t+3];
b= s[t+2];
break;
case 1:
s[t]= -s[t];
break;
case 2:
t= t-1;
s[t]= s[t]+s[t+1];
break;
case 3:
t= t-1;
s[t]= s[t]-s[t+1];
break;
case 4:
t= t-1;
s[t]= s[t]*s[t+1];
break;
case 5:
t= t-1;
s[t]= s[t] / s[t+1];
break;
case 6:
if(s[t]%2)
s[t]=1;
else
s[t]=0;
break;
case 8:
t= t-1;
if(s[t]==s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 9:
t= t-1;
if(s[t]==s[t+1])
s[t]=0;
else
s[t]=1;
break;
case 10:
t= t-1;
if(s[t]<s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 11:
t= t-1;
if(s[t]>=s[t+1])
s[t]= 1;
else
s[t]=0;
break;
case 12:
t= t-1;
if(s[t]>s[t+1])
s[t]= 1;
else
s[t]=0;
break;
case 13:
t= t-1;
if(s[t]<=s[t+1])
s[t]= 1;
else
s[t]=0;
break;
case 14:
cout<<" "<<s[t];
t=t-1;
break;
case 15:
cout<<endl;
break;
case 16:
t=t+1;
cout<<"请输入变量,并按回车结束。"<<endl;
s[t]=0;
cin>>s[t];
break;
};
break;
case LOD:
t= t+1;
s[t]= s[base(i.l,b,s)+i.a];
break;
case STO:
s[base(i.l,b,s)+i.a]= s[t];
t= t-1;
break;
case CAL:
s[t+1]= base(i.l,b,s);
s[t+2]= b;
s[t+3]= p;
b= t+1;
p=i.a;
break;
case INT:
t= t+i.a;
break;
case JMP:
p= i.a;
break;
case JPC:
if (s[t] == 0)
p= i.a;
t= t-1;
break;
}
}while(p!=0);
cout<<" 结束\n";
}
void SaveCode()
{
if(fout)
for (int i=0;i<cx;i++)
fprintf(fout,"%d %s %d %d\n ",i,mnemonic[code[i].f],code[i].l,code[i].a);
}
void main()
{
char source[20];
char destination[20];
listswitch=true,sourceEnd=false;
strcpy(word[1],"begin");
strcpy(word[2],"call");
strcpy(word[3],"const");
strcpy(word[4],"do");
strcpy(word[5],"end");
strcpy(word[6],"if");
strcpy(word[7],"odd");
strcpy(word[8],"procedure");
strcpy(word[9],"read");
strcpy(word[10],"then");
strcpy(word[11],"var");
strcpy(word[12],"while");
strcpy(word[13],"write");
wsym[1]= BEGINSYM; wsym[2]=CALLSYM;
wsym[3]= CONSTSYM; wsym[4]=DOSYM;
wsym[5]=ENDSYM; wsym[6]=IFSYM;
wsym[7]=ODDSYM; wsym[8]=PROCSYM;
wsym[9]=READSYM; wsym[10]=THENSYM;
wsym[11]=VARSYM; wsym[12]=WHILESYM;
wsym[13]=WRITESYM;
memset(code,0,sizeof(code));
memset(ssym,0,100*sizeof(symbol));
memset(table,0,sizeof(table));
memset(line,0,sizeof(line));
ssym['+']= PLUS;
ssym['-']= MINUS;
ssym['*']= TIMES;
ssym['/']= SLASH;
ssym['(']= LPAREN;
ssym[')']= RPAREN;
ssym['=']= EQL;
ssym[',']= COMMA;
ssym['.']= PERIOD;
ssym['<']= LSS;
ssym['>']= GTR;
ssym[';']= SEMICOLON;
strcpy(mnemonic[LIT]," lit ");
strcpy(mnemonic[OPR]," opr ");
strcpy(mnemonic[LOD]," lod ");
strcpy(mnemonic[STO]," sto ");
strcpy(mnemonic[CAL]," cal ");
strcpy(mnemonic[INT]," int ");
strcpy(mnemonic[JMP]," jmp ");
strcpy(mnemonic[JPC]," jpc ");
declbegsys.insert(CONSTSYM),declbegsys.insert(VARSYM),declbegsys.insert(PROCSYM);
statbegsys.insert(BEGINSYM),statbegsys.insert(CALLSYM),statbegsys.insert(IFSYM),statbegsys.insert(WHILESYM);
facbegsys.insert(IDENT),facbegsys.insert(NUMBER),facbegsys.insert(LPAREN);
err= 0;
cc= 0;
cx= 0;
ll= 0;
ch= ' ';
kk= al;
codeNo=0;
printf("请输入测试程序所在的文件名:\n");
for(int i=0;i<20;i++)
source[i]='\0';
scanf("%s",source);
printf("请输入输出文件名:\n");
for(int j=0;j<20;j++)
destination[j]='\0';
scanf("%s",destination);
char*outputf;
outputf=&destination[0];
fin=fopen(&source[0],"r");
fout=fopen(outputf,"w");
symset fsys;
fsys.insert(PERIOD);
fsys.insert(CONSTSYM),fsys.insert(VARSYM),fsys.insert(PROCSYM);
fsys.insert(BEGINSYM),fsys.insert(CALLSYM),fsys.insert(IFSYM),fsys.insert(WHILESYM);
getsym();
block(0,0,fsys);
SaveCode();
interpret();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -