⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pl0 in c.c

📁 PL/0教学语言的编译程序原代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
	{
		enter("variable",dx,tx,lev);
		getsym( );
		if (strcmp(sym,"lparen")==0)	
		{                               //dealing with daclaration of array
			getsym( );
			if (strcmp(sym,"number")==0)
			{
				table[(*tx)].foot=num;
				i=num+1;
				getsym( );
				if (strcmp(sym,"colon")==0)
				{
					getsym( );
					if (strcmp(sym,"number")==0)
					{
						do
						{
							enter("variable",dx,tx,lev);
							table[(*tx)].foot=i;
							i++;
						}
						while (i<=num);
						getsym( );
						if (strcmp(sym,"rparen")!=0) error(50);
						getsym( );
					}
					else error(50);
				}
				else error(50);
			}
			else error(50);
		}
	}	

	else error(4);
}*/



/*-----------------constdeclaration (&dx, &tx, lev)-----------*/

constdeclaration(dx,tx,lev)
int *dx,*tx,lev;
{
	if (strcmp(sym,"ident")==0)
	{
		getsym( );
		if ((strcmp(sym,"eql")==0) || (strcmp(sym,"becomes")==0))
		{
			if (strcmp(sym,"becomes")==0)
				error(1);
			getsym( );
			if (strcmp(sym,"number")==0)
			{
				enter("constant",dx,tx,lev);
				getsym( );
			}
			else error(2);
		}
		else error(3);
	}
	else error(4);
}


/*-----------------------------enter (k)-----------------------------------*/

enter(k,dx,tx,lev)
alfa k;
int *dx,*tx,lev;
{  
	 (*tx)++;
     strcpy(table[(*tx)].kind,k);
     strcpy(table[(*tx)].name,id);
     if (strcmp(k,"constant")==0)
     {  if (num>amax)
          {		error(31);
				num=0;
          }
          table[(*tx)].val=num;
       }
       if (strcmp(k,"variable")==0)
       {
          table[(*tx)].level=lev;
          table[(*tx)].adr=(*dx);
          (*dx)++;
		  //table[(*tx)].foot=0;
       }
       if (strcmp(k,"procedure")==0)  table[(*tx)].level=lev;
}



/*--------------------------test (s1,s2, n)-----------------------------*/

test(s1,s2,n)
symset s1,s2;  int n;
{
	if (in(sym,s1)==0)
	{
		error(n);
		while ((in(sym,s1)==0)&&(in(sym,s2)==0))
			getsym( );
	}
}



/*---------------------------gen (x,y,z)-----------------------------*/

//void gen(char x[5],int y,int z)
gen(x, y, z)
alfa x;
int y,z;
{
	if (cx>cxmax)
	{
		printf("program too long !\n");
		exit(0);
	}
	//strcpy(x,code[cx].f);
	strcpy(code[cx].f,x);
	code[cx].l=y;
	code[cx].a=z;
	cx++;    
}




/*-----------------------------getsym ( )------------------------------*/

getsym( )
{
	int i,j,k;
	kk=9;
	while (ch==' ' )
		getch( );
	if ((ch>='a')&& (ch<='z'))       
	{
		k=0-1;
		do
		{
			if (k<al)
			{
				k++;
				a[k]=ch;
			}
			getch( );
		}
		while ((ch>='a')&&(ch<='z')||(ch>='0')&&(ch<='9')); 
		if  (k>=kk)
			kk=k;
		else
			do
			{
				a[kk]='\0';
				kk--;
			} while (kk>k);
		strcpy (id,a);
		i=1;
		j=norw;
		do
		{
			k=(i+j)/2;
			if  (strcmp(id,word[k])<=0)
				j=k-1;
			if  (strcmp(id,word[k])>=0)
				i=k+1;
		} while (i<=j);
		if (i-j==2)
			strcpy(sym,wsym[k]);
		else
			strcpy(sym,"ident");
	}
	else
	if ((ch>='0')&&(ch<='9'))
	{
		k=0;
		num=0;
		strcpy(sym,"number");
		do 
		{
			num=10*num+ch-'0';
			k++;
			getch( );
		} while ((ch>='0')&&(ch<='9'));
		if (k>nmax)
			error(30);
	}
	else
	if (ch==':')
	{
		getch( );
		if (ch=='=')
		{
			strcpy(sym,"becomes");
			getch( );
		}
		else
			strcpy(sym,"nul");  
    }
    else
    if (ch=='<')
    {
       getch( );
       if (ch=='=')
       {
          strcpy(sym,"leq");
          getch( );
       }
       else
          strcpy(sym,"lss");
	}
	else
    if (ch=='>')
    {
        getch( );
        if (ch=='=')
        {
			strcpy(sym,"geq");
			getch( );
        }
        else
			strcpy(sym,"gtr");
	}
	else
    {
/* notice*/   strcpy(sym,ssym[ch-75]);  //-32
              getch( );
    }
	
}



/*----------------------------getch ( )------------------------*/

getch( )       
{
  if (cc==ll)
  {
	if (ch=='EOF')
	{
		printf("program incomplete !\n");
		exit(0);
	}
	//ll=0;
	cc=0;
	printf("\n"); /*add a statement*/
	printf("%4d",cx);
	//add begin
	/*while (ch!='\n')
	{
		ll++;
		ch=getc(finf);
		putchar(ch);
		line[ll]=ch;
	}
	line[ll]=' ';
  }
  cc++;
  ch=line[cc];*/
/*while  (ch= 'n')*/   /*notice*/
	for  (ll=1; ll<=80;  ll++)  /*get a  ltne  of input  file*/
	{                    /*ibm  format: a line is a record*/
		//ch=getchar();
		ch=getc(finf); /*ibn  format: a record  has 80char*/
		if ((ch>=193)&&(ch<=233))  ch=ch+64;   //?
		putchar(ch); /*ibm  format:no lf,instead  it with space*/
		line[ll]=ch;
	}
	ll=80;
	while  (line[ll]==' ')
		ll--;
	ll++;
	if  (line[ll-1]=='.')
		line[ll]='eof';
	else
		line[ll]=' ';
  }
	cc++;
	ch=line[cc];
}




/*-------------------error(n)------------*/
error(n)
int n;
{
	int i=0;
	printf("*****");
	for (i=0;i<cc;i++)
	putchar(' ');
	putchar(94);//'i');
	printf("%2d",n) ;
	err++;
}


/*--------------------listcode (cx0)-----------------------*/
listcode(cx0)
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,code[i].l,code[i].a);
}


/*-----------------------------insert (symi,fsys,&fsysi)--------------------*/
insert(sym1,fsys,fsys1)
symset fsys,*fsys1;
alfa sym1;
{
	int i=0;
	if (fsys1==0)
		fsys1=(symset *)calloc(30,sizeof(symset));
	while (strcmp(fsys.sy[i],"*")!=0)
	{
		strcpy((*fsys1).sy[i],fsys.sy[i]);
		i++;
	}
	strcpy((*fsys1).sy[i],fsys.sy[i]);
	i=0;
	while (strcmp(sym1,(*fsys1).sy[i])!=0 && strcmp((*fsys1).sy[i], "*")!=0)
		i++;
	if (strcmp(sym1,(*fsys1).sy[i])!=0)
	{
		strcpy((*fsys1).sy[i],sym1);
		i++;
		strcpy((*fsys1).sy[i], "*");
	}
}




/*--------------------------------int in (sm,fsys)--------------------*/
int in(sm,fsys)
alfa sm;
symset fsys;
{
	int i;
	i=0;
	//while (strcmp(fsys.sy[i], "*")==0)  i++;
	while (strcmp(sm,fsys.sy[i]) && strcmp(fsys.sy[i], "*"))
		i++;
	if (strcmp(sm,fsys.sy[i])!=0)
		i=0;
	else
		i=1;
	return(i);
}





/*------------------------------int position (id,tx)------------------*/
int position(id,tx)
alfa id;
int tx;
{
	int i;
	strcpy(table[0].name,id);
	i=tx;
	while (strcmp(table[i].name,id)!=0)
		i--;
	return(i);
}







/*----------------------int ifarr-----------------------*/
/*int ifarr(fsys,tx,lev,tt)
symset fsys;
int tx,lev,tt;
{
	alfa sm;
	if (table[tt].foot!=0)
	{
		strcpy(sm,table[tt].name);
		while (strcmp(table[tt].name,sm)==0)
			tt--;
		tt++;
		getsym();
		if (strcmp(sym,"lparen")!=0)
			error(51);
		else
		{
			getsym();
			insert("rparen",fsys,&fsys);
			expression(fsys,tx,lev);
			if (strcmp(sym,"rparen")!=0)
				error(52);
		}
	}
	else
		gen("lit",0,0);
	return(tt);
}
*/



/*-----------------------int base (l,b)------------------------*/
int base(l,b)
int l,b;
{
	int b1;
	b1=b;
	while (l>0)
	{
		b1=s[b1];
		l--;
	}
	return (b1);
}





/*-----------------------interpret() ------------------------*/
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;
		strcpy(i.f,code[p].f);
		p++;
		if (strcmp(i.f,"lit")==0)
		{
			t++;
			s[t]=i.a;
		}
		if (strcmp(i.f,"opr")==0)
		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]=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:
				printf("%5d",s[t]);
				t--;
				break;
			case 15:
				printf("\n");
				break;
			case 16:
				t++;
				printf("?");
				scanf("%d",&s[t]);
				printf("%d\n",s[t]);
				break;
			default:
				exit(0);
		}
		if (strcmp(i.f,"lod")==0)         //defferent from paper
		{
			//s[t]=s[base(i.l,b)+i.a+s[t]];
			t++;
			s[t]=s[base(i.l,b)+i.a];
		}
		if (strcmp(i.f,"sto")==0)          //defferent from paper
		{
			//s[base(i.l,b)+i.a+s[t-1]]=s[t];
			s[base(i.l,b)+i.a]=s[t];
			t--;  //t--;
		} 
		if (strcmp(i.f,"cal")==0)
		{
			s[t+1]=base(i.l,b);
			s[t+2]=b;
			s[t+3]=p;
			b=t+1;
			p=i.a;
		}
		if (strcmp(i.f,"int")==0)
			t=t+i.a;
		if (strcmp(i.f,"jmp")==0)
			p=i.a;
		if (strcmp(i.f,"jpc")==0)
			if (s[t]==0)
				p=i.a;
	} while (p!=0);
	printf("end pl/0 !\n");
}





/*---------------------------end--------------------*/
/*---------------------------result----------------*/






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -