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

📄 steve.c

📁 简单的编译器
💻 C
📖 第 1 页 / 共 2 页
字号:


void statement(fsys,plev)
struct node *fsys;
int plev;
{int i,cx1,cx2,m=0,n=0;
char *tempset1[]={"rparen","comma",NULL};
char *tempset2[]={"thensym","dosym",NULL};
char *tempset3[]={"semicolon","endsym",NULL};
char *tempset4[]={"semicolon",NULL};
char *tempset5[]={"dosym",NULL};
char *tempset6[]={NULL};
 
struct node *temp1,*temp2,*temp3,*temp4,*temp5,*temp6;
temp1=(struct node *)malloc(sizeof(struct node));
temp2=(struct node *)malloc(sizeof(struct node));
temp3=(struct node *)malloc(sizeof(struct node));
temp4=(struct node *)malloc(sizeof(struct node));
temp5=(struct node *)malloc(sizeof(struct node));
temp6=(struct node *)malloc(sizeof(struct node));

while(tempset1[m]!=NULL)
   temp1->pa[n++]=tempset1[m++];
temp1->pa[n]=NULL;

m=0;n=0;
while(tempset2[m]!=NULL)
   temp2->pa[n++]=tempset2[m++];
temp2->pa[n]=NULL;

m=0;n=0;
while(tempset3[m]!=NULL)
   temp3->pa[n++]=tempset3[m++];
temp3->pa[n]=NULL;

m=0;n=0;
while(tempset4[m]!=NULL)
   temp4->pa[n++]=tempset4[m++];
temp4->pa[n]=NULL;

m=0;n=0;
while(tempset5[m]!=NULL)
   temp5->pa[n++]=tempset5[m++];
temp5->pa[n]=NULL;

m=0;n=0;
while(tempset6[m]!=NULL)
   temp6->pa[n++]=tempset6[m++];
temp6->pa[n]=NULL;

m=0;n=0;

if(strcmp(sym,"ident")==0)
 {  i=position(id);
    if(i==0)  error(11);
    else  
        {  if(table[i].kind!=variable)
               {error(12);
                i=0;}
               
        }
    getsym();
    if(strcmp(sym,"becomes")==0)  getsym();
    else error(13);
    expression(fsys);
    if(i!=0) gen(sto,plev-table[i].level,table[i].adr+2);
 }
else if(strcmp(sym,"readsym")==0)
 { getsym();
   if(strcmp(sym,"lparen")!=0) error(24);
   else 
      { do
          {getsym();
           if(strcmp(sym,"ident")==0)  i=position(id);
           else i=0;
           if(i==0) error(35);
           else
              { gen(opr,0,16);
                gen(sto,plev-table[i].level,table[i].adr+2);
               }
           getsym();
          }while(strcmp(sym,"comma")==0);
       }
  if(strcmp(sym,"rparen")!=0)
     { error(22);
       while(in(sym,fsys)==0) getsym();
     }
  else getsym();
 }
else if(strcmp(sym,"writesym")==0)
  { getsym();
    if(strcmp(sym,"lparen")==0)
       { do
            { getsym();
              expression(add(temp1,fsys));
              gen(opr,0,14);
            }while(strcmp(sym,"comma")==0);
        if(strcmp(sym,"rparen")!=0) error(33);
        else getsym();
      }
    gen(opr,0,15);
 }

/*               297         */
else if(strcmp(sym,"callsym")==0)
 { getsym();
   if(strcmp(sym,"ident")!=0)  error(14);
   else
      { i=position(id);
        if(i==0)error(11);
        else 
           { if(table[i].kind==procedur)
                       gen(cal,plev-table[i].level,table[i].adr+2);
             else error(15);
           }
        getsym();
       }
 }
else if(strcmp(sym,"ifsym")==0)
 {  getsym();
    condition(add(temp2,fsys));
    if(strcmp(sym,"thensym")==0)  getsym();
    else error(16);
    cx1=cx;
    gen(jpc,0,0);
    statement(fsys,plev);
    code[cx1].a=cx;
  }
else if(strcmp(sym,"beginsym")==0)
 {  getsym();
    statement(add(temp3,fsys),plev);
    while(in(sym,add(temp4,statbegsys))==1)
        {  if(strcmp(sym,"semicolon")==0) getsym();
           else error(10);
           statement(add(temp3,fsys),plev);
        }
    if(strcmp(sym,"endsym")==0)  getsym();
    else error(17);
 }
else 
 {  if(strcmp(sym,"whilesym")==0) 
       {  cx1=cx;
          getsym();
          condition(add(temp5,fsys));
          cx2=cx;
          gen(jpc,0,0);
          if(strcmp(sym,"dosym")==0)  getsym();
          else error(18);
          statement(fsys,plev);
          gen(jmp,0,cx1);
          code[cx2].a=cx;
        }
 }

test(fsys,temp6,19);
}



void block(plev,tx,fsys)
int plev,tx;
struct node *fsys;
{int m=0,n=0;
int dx=3;
int tx0;
int cx0;
char *tempset1[]={"semicolon","endsym",NULL};
char *tempset2[]={"ident","procsym",NULL};
char *tempset3[]={"semicolon",NULL};
char *tempset4[]={"ident",NULL};
char *tempset5[]={NULL};
struct node *temp1,*temp2,*temp3,*temp4,*temp5;

temp1=(struct node *)malloc(sizeof(struct node));
temp2=(struct node *)malloc(sizeof(struct node));
temp3=(struct node *)malloc(sizeof(struct node));
temp4=(struct node *)malloc(sizeof(struct node));
temp5=(struct node *)malloc(sizeof(struct node));

while(tempset1[m]!=NULL)
  temp1->pa[n++]=tempset1[m++];
temp1->pa[n]=NULL;

m=0;n=0;
while(tempset2[m]!=NULL)
  temp2->pa[n++]=tempset2[m++];
temp2->pa[n]=NULL;

m=0;n=0;
while(tempset3[m]!=NULL)
  temp3->pa[n++]=tempset3[m++];
temp3->pa[n]=NULL;

m=0;n=0;
while(tempset4[m]!=NULL)
  temp4->pa[n++]=tempset4[m++];
temp4->pa[n]=NULL;

m=0;n=0;
while(tempset5[m]!=NULL)
  temp5->pa[n++]=tempset5[m++];
 temp5->pa[n]=NULL;
 m=0;n=0;
 lev=plev;
 dx=3;
 tx0=tx;
 table[tx].adr=cx;
 gen(jmp,0,1);
 if(plev>levmax) error(32);
 do
   { if(strcmp(sym,"constsym")==0)
        { getsym();
          do
              { constdeclaration();
                while(strcmp(sym,"comma")==0)
                    { getsym();
                      constdeclaration();
                    }
                if(strcmp(sym,"semicolon")==0) getsym();
                else error(5);
              }while(strcmp(sym,"ident")==0);
        }
     if(strcmp(sym,"varsym")==0)
         { getsym();
           do  { vardeclaration();
                 while(strcmp(sym,"comma")==0)
                     { getsym();
                       vardeclaration();
                      }
                 if(strcmp(sym,"semicolon")==0) getsym();
                 else error(5);
                }while(strcmp(sym,"ident")==0);
          }
    while(strcmp(sym,"procsym")==0)
          { getsym();
            if(strcmp(sym,"ident")==0)
                { enter(procedur);
                  getsym();
                 }
            else error(4);
            if(strcmp(sym,"semicolon")==0)  getsym();
            else error(5);
            block(plev+1,tx,add(temp3,fsys));
            lev=lev-1;
            if(strcmp(sym,"semicolon")==0)
                 { getsym();
                   test(add(statbegsys,temp2),fsys,6);
                   }
            else error(5);
          }
    test(add(statbegsys,temp4),declbegsys,7);
 }while(in(sym,declbegsys)==1);

code[table[tx0].adr].a=cx;
table[tx0].adr=cx;
table[tx0].size=dx;
cx0=cx;
gen(ini,0,dx);
statement(add(temp1,fsys),plev);
gen(opr,0,0);
test(fsys,temp5,8);
listcode(&cx0);
}


/*                    300             */


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



void interpret()
{ int p=0;
  int b=1;
  int t=0;
  struct instruction i;
  int s[stacksize];
  printf("start pl0\n");
  s[0]=0;
  s[1]=0;
  s[2]=0;
  s[3]=0;
  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==0) s[t]=0; else s[t]=1;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:printf("%d",s[t]);
				fprintf(fa2,"%d",s[t]);t=t-1;break;
                        case 15:printf("\n");fprintf(fa2,"\n");break;
                        case 16:t=t+1;printf("?");fprintf(fa2,"?");
                                scanf("%d",&s[t]);fprintf(fa2,"%d",s[t]);break;
                       }
                      break;
		case lod:t=t+1;s[t]=s[base(1,&b,s)+i.a];break;
		case sto:s[base(1,&b,s)+i.a]=s[t];t=t-1;break;
		case cal:s[t+1]=base(1,&b,s);s[t+2]=b;s[t+3]=p;b=t+1;p=i.a;break;
		case ini:t=t+i.a;break;
		case jpc:if(s[t]==0)p=i.a;t=t-1;break;
		}
          }while(p!=0);
  fclose(fa2);
}



main()
{int m=0,n=0;
char *declbeg[]={"constsym","varsym","procsym",NULL};
char *statbeg[]={"beginsym","callsym","ifsym","whilesym",NULL};
char *facbeg[]={"ident","number","lparen",NULL};
char *tempset[]={"period","constsym","varsym","procsym",NULL};

declbegsys=(struct node *)malloc(sizeof(struct node));
statbegsys=(struct node *)malloc(sizeof(struct node));
facbegsys=(struct node *)malloc(sizeof(struct node));
tempsetsys=(struct node *)malloc(sizeof(struct node));

while(declbeg[m]!=NULL)
    declbegsys->pa[n++]=declbeg[m++];
declbegsys->pa[n]=NULL;
m=0;n=0;
while(statbeg[m]!=NULL)
    statbegsys->pa[n++]=statbeg[m++];
statbegsys->pa[n]=NULL;
m=0;n=0;
while(facbeg[m]!=NULL)
    facbegsys->pa[n++]=facbeg[m++];
facbegsys->pa[n]=NULL;
m=0;n=0;
while(tempset[m]!=NULL)
    tempsetsys->pa[n++]=tempset[m++];
tempsetsys->pa[n]=NULL;

if((fa1=fopen("fa1.txt","w"))==NULL)
    { printf("Cannot open file \n"); exit(0); }
printf("Input file?\n");
fprintf(fa1,"Input file?\n");
scanf("%s",fname);
fprintf(fa1,"%s",fname);

if((fin=fopen(fname,"r"))==NULL)
     { printf("Cannot open file according to given filename\n");exit(0); }

printf("list object code?\n");
scanf("%s",fname);
fprintf(fa1,"list object code?\n");

if(fname[0]=='y')
   listswitch=true;
else
   listswitch=false;
err=0;
cc=1;cx=0;ll=0;ch=' ';kk=al;
getsym();
if((fa=fopen("fa.txt","w"))==NULL)
    {  printf("Cannot open fa.txt file\n");exit(0);}
if((fa2=fopen("fa2.txt","w"))==NULL)
    {  printf("Cannot open fa2.txt file\n");exit(0);}

block(0,0,add(statbegsys,tempsetsys));
fclose(fa);
fclose(fa1);
if(strcmp(sym,"period")!=0)
    error(9);
if(err==0)
    interpret();
else
    printf("%d error in PASCAL program\n",err);
fclose(fin);
}

⌨️ 快捷键说明

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