📄 20041016212439689.txt
字号:
getsym ( );
}
else if (strcmp (sym, "lparen")== 0)
{
if (num > amax)
{ error (31);
num=0;
}
gen ("lit", 0, num);
getsym ( );
}
else if (strcmp (sym, "lparen")== 0)
{ getsym ();
insert ("rparen", fsys, &fsys1);
expression (fsys1, tx, lev);
if (strcmp (sym, "rparen")== 0)
getsym( );
else error (22);
}*/
test(fsys,facbegsys,23);
}
}
/*-------------------vardeclaration (&dx, &tx, lev)---------------------*/
void vardeclaration(int * dx,int *tx,int lev) file://very different with paper
{
if (sym=="ident")
{
enter("variable",dx,tx,lev);
getsym();
}
else error(4);
}
/* ---------vardeclaration(&dx,&tx,lev)-------------*/
/*vardeclaration(dx,tx,lev)
int *dx,*tx,lev;
{
int i;
if (sym=="ident")==0)
{
enter("variable",dx,tx,lev);
getsym( );
if (sym=="lparen")==0)
{ file://dealing with daclaration of array
getsym( );
if (sym=="number")==0)
{
table[(*tx)].foot=num;
i=num+1;
getsym( );
if (sym=="colon")==0)
{
getsym( );
if (sym=="number")==0)
{
do
{
enter("variable",dx,tx,lev);
table[(*tx)].foot=i;
i++;
}
while (i<=num);
getsym( );
if (sym=="rparen")!=0) error(50);
getsym( );
}
else error(50);
}
else error(50);
}
else error(50);
}
}
else error(4);
}*/
/*-----------------constdeclaration (&dx, &tx, lev)-----------*/
void constdeclaration(int *dx,int *tx,int lev)
{
if (sym=="ident")
{
getsym( );
if ((sym=="eql") || (sym=="becomes"))
{
if (sym=="becomes")
error(1);
getsym( );
if (sym=="number")
{
enter("constant",dx,tx,lev);
getsym( );
}
else error(2);
}
else error(3);
}
else error(4);
}
/*-----------------------------enter (k)-----------------------------------*/
void enter(string k,int *dx,int *tx,int lev)
{
(*tx)++;
table[(*tx)].kind=k;
table[(*tx)].name=id;
if (k=="constant")
{ if (num>amax)
{ error(31);
num=0;
}
table[(*tx)].val=num;
}
if (k=="variable")
{
table[(*tx)].level=lev;
table[(*tx)].adr=(*dx);
(*dx)++;
file://table[(*tx)].foot=0;
}
if (k=="procedure") table[(*tx)].level=lev;
}
/*--------------------------test (s1,s2, n)-----------------------------*/
void test(symset s1,symset s2,int n)
{
#if 0
cout<<"begin test\n";
for(string * ips=s1.sy;(*ips)=="*")!=0; ips++){
cout<<(*ips)<<"\n";
}
cout<<endl;
#endif
if (in(sym,s1)==0)
{
cout<<"!!"<<sym<<"!!"<<endl;
error(n);
while ((in(sym,s1)==0)&&(in(sym,s2)==0)&&ch!='.')
getsym( );
}
}
/*---------------------------gen (x,y,z)-----------------------------*/
file://void gen(char x[5],int y,int z)
void gen(string x, int y, int z)
{
if (cx>cxmax)
{
cout<<"program too long !\n";
exit(0);
}
file://strcpy(x,code[cx].f);
code[cx].f=x;
code[cx].l=y;
code[cx].a=z;
cx++;
}
/*-----------------------------getsym ( )------------------------------*/
void getsym()
{
int i,j,k;
bool isblank=(ch==' '||ch=='\n'||ch=='\t'||'\r');
while (isblank&&ch!='.'){
getch();
isblank=(ch==' '||ch=='\n'||ch=='\t'||ch=='\r');
}
if(ch==EOF||ch=='.')return;
bool find=false;
char * tp=new char[al];
if ((ch>='a')&& (ch<='z'))
{
k=0-1;
id=string();
do
{ int p;
if (k<al)
{
k++;
a[k]=ch;
id.push_back(ch);
}
getch( );
} while ((ch>='a')&&(ch<='z')||(ch>='0')&&(ch<='9'));
ptext--;
i=1;
j=norw;
for(k=i;k<i+j;k++){
if (id==word[k]){
sym=wsym[k];
find=true;
break;
}
}
if(!find){
sym="ident";
}
return;
}
if ((ch>='0')&&(ch<='9')){//combine a number
k=0;
num=0;
sym="number";
do {
num=10*num+ch-'0';
k++;
getch( );
} while ((ch>='0')&&(ch<='9'));
ptext--;
if (k>nmax)
error(30);
return;
}
file://not a-z and not 0-9
if (ch=='='){
sym="eql";
file://getch( );
return;
}
if (ch==':'){
getch( );
if (ch=='='){
sym="becomes";
// getch( );
}
else
sym="nul";
return;
}
if (ch=='<'){
getch( );
if (ch=='='){
sym="leq";
// getch( );
}
else sym="lss";
return;
}
if (ch=='>'){
getch( );
if (ch=='='){
sym="geq";
// getch();
}
else sym="gtr";
return;
}
sym=what_identify(ch);
// getch( );
}
/*----------------------------getch ( )------------------------*/
void getch()
{
ch=text[ptext++];
}
/*-------------------error(n)------------*/
void error(int n)
{
int i=0;
printf("*****");
printf("%2d",n) ;
err++;
}
/*--------------------listcode (cx0)-----------------------*/
void listcode(int cx0)
{
int i;
if (listswitch==1)
for (i=cx0-1;i<cx;i++) /*has been changed */
printf("%4d%5s%3d%5d\n",i,code[i].f.c_str(),code[i].l,code[i].a);
}
/*-----------------------------insert (symi,fsys,&fsysi)--------------------*/
void insert(string sym1,symset fsys,symset *fsys1)
{
int i=0;
if (fsys1==0)
fsys1=(symset *)calloc(30,sizeof(symset));
while (fsys.sy[i]!="*")
{
(*fsys1).sy[i]=fsys.sy[i];
i++;
}
(*fsys1).sy[i]=fsys.sy[i];
i=0;
while (sym1!=(*fsys1).sy[i] && (*fsys1).sy[i]!="*")
i++;
if (sym1!=(*fsys1).sy[i])
{
(*fsys1).sy[i]=sym1;
i++;
(*fsys1).sy[i]="*";
}
}
/*--------------------------------int in (sm,fsys)--------------------*/
int in(string sm,symset fsys)
{
int i;
i=0;
while (sm!=fsys.sy[i] && fsys.sy[i]!="*")
i++;
if (sm==fsys.sy[i])
i=1;
else
i=0;
return(i);
}
/*------------------------------int position (id,tx)------------------*/
int position(string id,int tx)
{
int i;
table[0].name=id;
i=tx;
while (table[i].name!=id)
i--;
return(i);
}
/*----------------------int ifarr-----------------------*/
/*int ifarr(fsys,tx,lev,tt)
symset fsys;
int tx,lev,tt;
{
string sm;
if (table[tt].foot!=0)
{
strcpy(sm,table[tt].name);
while (strcmp(table[tt].name,sm)==0)
tt--;
tt++;
getsym();
if (sym=="lparen")!=0)
error(51);
else
{
getsym();
insert("rparen",fsys,&fsys);
expression(fsys,tx,lev);
if (sym=="rparen")!=0)
error(52);
}
}
else
gen("lit",0,0);
return(tt);
}
*/
/*-----------------------int base (l,b)------------------------*/
int base(int l,int b)
{
int b1;
b1=b;
while (l>0)
{
b1=s[b1];
l--;
}
return (b1);
}
/*-----------------------interpret() ------------------------*/
void interpret()
{
int p,b,t;
instruction i;
printf("start pl/0 :\n");
t=p=0;
b=1;
s[1]=s[2]=s[3]=0;
do
{
i.a=code[p].a;
i.l=code[p].l;
i.f=code[p].f;
p++;
if (i.f=="lit")
{
t++;
s[t]=i.a;
}
if (i.f=="opr")
switch(i.a)
{
case 0:
t=b-1;
p=s[t+3];
b=s[t+2];
break;
case 1:
s[t]=0-s[t];
break;
case 2:
t--;
s[t]=s[t]+s[t+1];
break;
case 3:
t--;
s[t]=s[t]-s[t+1];
break;
case 4:
t--;
s[t]=s[t]*s[t+1];
break;
case 5:
t--;
s[t]=s[t]/s[t+1];
break;
case 6:
if (s[t]!=0)
s[t]=1;
else s[t]=0;
break;
case 8:
t--;
if (s[t]==s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 9:
t--;
if (s[t]!=s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 10:
t--;
if (s[t]<s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 11:
t--;
if (s[t]>=s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 12:
t--;
if (s[t]>s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 13:
t--;
if (s[t]<=s[t+1])
s[t]=1;
else
s[t]=0;
break;
case 14:
cout<<s[t];
t--;
break;
case 15:
printf("\n");
break;
case 16:
t++;
cout<<"?";
cin>>s[t];
cout<<s[t];
break;
default:
exit(0);
}
if (i.f=="lod") file://defferent from paper
{
file://s[t]=s[base(i.l,b)+i.a+s[t]];
t++;
s[t]=s[base(i.l,b)+i.a];
}
if (i.f=="sto") file://defferent from paper
{
file://s[base(i.l,b)+i.a+s[t-1]]=s[t];
s[base(i.l,b)+i.a]=s[t];
t--;
}
if (i.f=="cal")
{
s[t+1]=base(i.l,b);
s[t+2]=b;
s[t+3]=p;
b=t+1;
p=i.a;
}
if (i.f=="int")
t=t+i.a;
if (i.f=="jmp")
p=i.a;
if (i.f=="jpc")
if (s[t]==0)
p=i.a;
} while (p!=0);
cout<<"end pl/0 !\n";
}
/*---------------------------end--------------------*/
/*---------------------------result----------------*/
/*-----------------------------main( )-----------------------------------*/
void main()
{
cout<<"input file ?";
cin>>fname;
cout<<fname<<endl;
finf.open(fname.c_str());
finf.unsetf(ios::skipws);
file://printf(fopen(fname, "r"));
cout<<"list code ? ";
cin>>fname;
cout<<fname<<endl;
if (fname[0]=='y')
listswitch=1;
else
listswitch=0;
err=cc=ll=cx=ptext=0;
kk=10;
ch=' ';
while (!finf.eof()){
finf>>ch;
cout<<ch;
text.push_back(ch);
}
getsym();
block(0,0,fsysa);
if (sym!="period")
// error(9);
if (err==0)
interpret( );
else
printf("error in pl/0 program !\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -