📄 work.cpp
字号:
#include "PL.h"
#define stacksize 500
int main()
{
cout<<"Please Enter So File Name?";
cin>>fname;
fin.open(fname);
if(fin.is_open())
{
fa1.open("fa1.tmp");
fa1<<"Input pl/0 file?"<<fname<<"\n";
init();
err = 0;
cc = cx = ll = 0;
ch = ' ';
if(-1 != getsym())
{
fa.open("fa.tmp");
if(-1==expression())
{
fa.close();
fa1.close();
fas.close();
fin.close();
cout<<endl;
return 0;
}
listcode();
fa.close();
fa1.close();
fas.close();
if(sym != eql)
{
error(9);
}
if(err == 0)
{
fa2.open("fa2.tmp");
interpret();
fa2.close();
}
else
cout<<"Errors in pl/0 program";
}
}
return 0;
}
void init()
{
int i;
for(i = 0; i <= 255; i++)
{
ssym[i] = nul;
}
ssym['+']=plus;
ssym['-']=minus;
ssym['*']=times;
ssym['/']=slash;
ssym['(']=lparen;
ssym[')']=rparen;
ssym['=']=eql;
strcpy(&(mnemonic[lit][0]),"lit");
strcpy(&(mnemonic[opr][0]),"opr");
strcpy(&(mnemonic[lod][0]),"lod");
strcpy(&(mnemonic[sto][0]),"sto");
strcpy(&(mnemonic[cal][0]),"cal");
strcpy(&(mnemonic[inte][0]),"int");
strcpy(&(mnemonic[jmp][0]),"jmp");
strcpy(&(mnemonic[jpc][0]),"jpc");
}
void error(int n)
{
char space[81];
memset(space,32,81);
space[cc-1]=0;
// printf("****%s!%d\n",space,n);
cout<<"****"<<space<<"!"<<n<<endl;
fa1<<"****"<<space<<"!"<<n<<endl;
err++;
}
int getch()
{
if(cc == ll)
{
if(fin.eof())
{
cout<<"program incomplete!\n";
return -1;
}
ll=0;
cc=0;
cout<<cx<<' ';
fa1<<cx<<' ';
ch=' ';
while(ch!=10)
{
//fscanf(fin,"%c",&ch))
ch = fin.get();
if(EOF == ch)
{
line[ll]=0;
break;
}
cout<<ch;
fa1<<ch;
line[ll]=ch;
ll++;
}
cout<<"\n";
fa1<<"\n";
}
ch=line[cc];
cc++;
return 0;
}
int getsym()
{
int k;
while(ch==' ' || ch==10 || ch==9)
getchdo;
if(ch>='0' && ch<='9')
{
k = 0;
num = 0;
sym = number;
do {
num = 10*num+ch-'0';
k++;
getchdo;
} while(ch>='0' && ch<='9');
k--;
if(k > nmax)
{
error(30);
}
}
else
{
sym = ssym[ch];
getchdo;
}
return 0;
}
int gen(enum fct x,int y,int z)
{
if(cx >= cxmax)
{
cout<<"Program too long";
return -1;
}
code[cx].f=x;
code[cx].l=y;
code[cx].a=z;
cx++;
return 0;
}
void listcode()
{
for(int i=0; i<cx; i++)
{
cout<<i<<" "<<mnemonic[code[i].f]<<" "<<code[i].a<<endl;
fa<<i<<" "<<mnemonic[code[i].f]<<" "<<code[i].a<<endl;
}
}
int expression()
{
enum symbol addop;
if(sym==plus||sym==minus)
{
addop = sym;
getsymdo;
term();
if(addop == minus)
gendo(opr,0,1);
}
else
{
term();
}
while(sym==plus||sym==minus)
{
addop = sym;
getsymdo;
term();
if(addop == plus)
{
gendo(opr,0,2);
}
else
{
gendo(opr,0,3);
}
}
if(sym ==eql)
{
gen(opr,0,14);
}
return 0;
}
int term()
{
enum symbol mulop;
factor();
while(sym==times || sym==slash)
{
mulop = sym;
getsymdo;
factor();
if(mulop == times)
{
gendo(opr,0,4);
}
else
{
gendo(opr,0,5);
}
}
return 0;
}
int factor()
{
if(sym == number)
{
if(num > amax)
{
error(31);
num = 0;
}
gen(lit,0,num);
getsymdo;
}
else if(sym == lparen)
{
getsym();
expression();
if(sym == rparen)
getsym();
else
error(22);
}
return 0;
}
void interpret()
{
struct instruction i;
int s[stacksize];
cout<<"start pl0\n";
int t = 0;
int p = 0;
s[0]=s[1]=s[2]=0;
do {
i=code[p];
p++;
switch(i.f)
{
case lit:
s[t] = i.a;
t++;
break;
case opr:
switch(i.a)
{
case 1:
s[t-1] = -s[t-1];
break;
case 2:
t--;
s[t-1] = s[t-1] + s[t];
break;
case 3:
t--;
s[t-1] = s[t-1] - s[t];
break;
case 4:
t--;
s[t-1] = s[t-1] * s[t];
break;
case 5:
t--;
s[t-1] = s[t-1] / s[t];
break;
case 14:
cout<<s[t-1]<<"\n";
fa2<<s[t-1]<<"\n";
t--;
break;
}
}
} while(t != 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -