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

📄 jp jp(0).c

📁 是词法分析程序,可以对词法分析有更深的了解与认识
💻 C
📖 第 1 页 / 共 3 页
字号:
strcpy(clh1->name,"");
clh1->type=record;
clh1->line[0]=getdq[0].line;
clh1->layer=-1;
clh1->layer=llayer;
clh->next=NULL;


switch(record)
{
case 1:if(getdq[0].t!=2)
{
free(clh1);
yfchc("","The type is unequal");/*记录出错误信息*/
return;
}
clh->a=atoi(getdq[0].name);
clh1->a=clh->a;
*cln=clh1;
fprintf(out,"\n%d%d",2,clh1);
getsy();/*重新读入一个单词到输入栈*/
break;

case 2:fprintf(out,"\n%d%d",0,38);/*output '"'*/
getsy();/*重新读入一个单词到输入栈*/
clh->c[0]='\0';
if(getdq[0].t==5)
{
strcpy(clh->c,getdq[0].name);
getsy();
}
if(getdq[0].t=='\0')
free(clh1);
else{
strcpy(clh->c,clh->c);
i=2;
while(getdq[0].t==5&&i<80&&doorkey<=3)
{
i=i+1;
strcpy(clh->c,getdq[0].name);
getsy();
}
*cln=clh1;
fprintf(out,"\n%d%d",2,clh1);
}
if(strcmp(getdq[0].name,"\"")==0)
{
fprintf(out,"\n%d%d",0,38);/*output '"'*/
getsy();/*重新读入一个单词到输入栈*/

} 
else{ 
yfchc("","Statemen missing'\"'");/*记录出错误信息*/
return;
  }
  break;

case 3:if(getdq[0].t!=4)
{
free(clh1);
yfchc("","The type is unequal");/*记录出错误信息*/
return;
}
clh->b=atoi(getdq[0].name);
clh1->b=clh->b;
*cln=clh1;
fprintf(out,"\n%d%d",2,clh1);
getsy();/*重新读入一个单词到输入栈*/
}/*end switch*/
if(strcmp(getdq[0].name,",")==0)
{
fprintf(out,"\n%d%d",0,32);/*output ','*/
getsy();/*重新读入一个单词到输入栈*/
}
else if(strcmp(getdq[0].name,";")==0)
{
fprintf(out,"\n%d%d",0,33);/*output ';'*/
getsy();/*重新读入一个单词到输入栈*/
return;/*处理完毕,退出常量定义处理函数*/
} 
else{
yfchc("","The type is unequal");/*记录出错误信息*/
return;/*常量定义出错,退出常量定义处理函数*/
} 
}/*end else   完成变量定义处理*/
}/*end if 变量名合法*/
else{
yfchc(getdq[0].name,"illegal name of variable");/*记录出错误信息*/
return;
}
}/*end while*/

}/*end cldy*/





int cfql(void)   /*词法处理程序,如果词法分析通过,则返回1;否则返回0*/
{
int i=0,ii;
void *temp;
struct hsh *hshh;
struct cjbl *cjh;
struct bl *blh,**bln;
struct cl *clh,**cln;
struct shz *shzh,**shzn;
struct cch *cchh,**cchn;
/*printf("\n\n");
while(i<46)
{
printf("   %d%s",word[i],number,word[i].name);
i=i+1;
}
getch();*/
addr=0;     /*全局变量初始化*/
fl=0;
ta=0;
hh=0;
doorkey=0;
in=fopen("aa.c","r");
out=fopen("aaa.c","w");
INPUT;
getsy();getsy();
getsy();
/*printf("         name   hf   line \n");
printf(".....................\n");
i=1;
while(feof(in)==0)
{i=i+1;
if(i==20)
{getch();i=1;printf("\n");}
getey();
printf("\n%15s%5d%5d",geted[0].name,getdy[0].t,getdy[0].line);
} */
head=malloc(sizeof(struct cjbl));/*初始化链表*/
head->layer=0;
head->a=NULL;
head->b=NULL;
head->c=NULL;
head->f=NULL;
head->next=NULL;
llayer=0;
while(doorkey<=3)
{
   switch(getdq[0].t)
      {
         case 0:i=4;
                    temp=NULL;
                    hshh=NULL;
                    temp=set(-1,&i,getdq[0].name);
                    if(temp!=NULL)
                     {
                        if(strcmp(getdq[1].name,"(")==0)
                           { ii=0;
                             hshh=temp;
                             while(ii<19&&hshh->line[ii]!=-1)
                              ii=ii+1;
                              if(ii<19)
                                {
                                  hshh->line[ii]=getdq[0].line;
                                  hshh->line[ii+1]=-1;
                                  }   
                           } 
                      }
        i=6;
        temp=NULL;
        temp=set(0,&i,getdq[0].name); 
        if(temp!=NULL)
          { 
            ii=0;
            cchh=temp;
            while(ii<19&&hshh->line[ii]!=-1)
            ii=ii+1;
            if(ii<19)
                {
                    cchh->line[ii]=getdq[0].line;
                    cchh->line[ii+1]=-1;
                   }
             if(hshh!=NULL)
               fprintf(out,"\n%d%d",4,hshh);
              else  fprintf(out,"\n%d%d",6,cchh);
              getsy();
              break;       
           }  
       fprintf(out,"\n%d%d",6,yfchc(getdq[0].name,"illeal word"));
       getsy();
       break;

       case 1:i=chjtb(getdq[0].name);
             if(i>6&&i<14)/*在静态表中找到*/
                 { 
                     fprintf(out,"\n%d%d",0,i);  /*output  保留字代码*/
                     getsy();
                   }
                   else
                        if(i<5&&i>0)
                           if(strcmp(getdq[2].name,"(")==0)
                           ; /* hshdy(i);*/
                           else{
                                 if(i>0&&i<4)
                                    bldy(i);
                                     else
                                       { fprintf(out,"\n%d%d",0,i);
                                          getsy();
                                       } 
                                 } 
                     else if(i==5)
                           cldy();/*此处是常量处理函数*/
             if(i==0)  /*在静态表中找不到*/
               { 
                 i=4;
                 temp=NULL;
                 temp=set(-1,&i,getdq[0].name);
                   if(temp!=NULL)
                     {
                        if(strcmp(getdq[1].name,"(")==0)
                           { ii=0;
                             hshh=temp;
                             while(ii<19&&hshh->line[ii]!=-1)
                              ii=ii+1;
                              if(ii<19)
                                {
                                  hshh->line[ii]=getdq[0].line;
                                  hshh->line[ii+1]=-1;
                                  } 
                             fprintf(out,"\n%d%d",4,hshh);
                             getsy();
                             break;    
                           }  
               }
        i=0;
        temp=set(llayer,&i,getdq[0].name); 
        if(temp==NULL)         
            temp=set(0,&i,getdq[0].name);
        if(temp==NULL)    
           {
             fprintf(out,"\n%d%d",6,yfchc(getdq[0].name,"undefined word"));
             getsy();    
             }   
         else{
                switch(i)
                   { 
                     case 1:blh=temp;
                            ii=0;
                        while(ii<19&&blh->line[ii]!=-1)
                        ii=ii+1;
                        if(ii<19)
                          {
                             blh->line[ii]=getdq[0].line;
                             blh->line[ii+1]=-1;
                          }
                        fprintf(out,"\n%d%d",i,blh);
                        getsy();
                        break;                                                                                                                                                                                                                         
          case 2: clh=temp;
        ii=0;   
        while(ii<19&&clh->line[ii]!=-1)
        ii=ii+1;
        if(ii<19){
            clh->line[ii]=getdq[0].line;
            clh->line[ii+1]=-1;
            }
        fprintf(out,"\n%d %d",i,clh);/*output define word*/
        getsy();
        break;
    case 3:shzh=temp;
        ii=0;
        while(ii<19&&shzh->line[ii]!=-1)
        ii=ii+1;
        if(ii<19)
        {
            shzh->line[ii]=getdq[0].line;
            shzh->line[ii+1]=-1;
        }
        fprintf(out,"\n%d %d",i,shzh);
        getsy();
        break;
    case 6:cchh=temp;
        ii=0;
        while(ii<19&&cchh->line[ii]!=-1)
        ii=ii+1;
        if(ii<19)
        {
            cchh->line[ii]=getdq[0].line;
            cchh->line[ii+1]=-1;
        }
        fprintf(out,"\n%d %d",i,cchh);
        getsy();
                        }/*end switch*/
                        }
              }
                        break;
                    case 2:clh=malloc(sizeof(struct cl));
                              i=2;
                              cln=set(llayer,&i,"");
                              *cln=clh;
                              strcpy(clh->name,"");
                              clh->type=1;
                              clh->a=atoi(getdq[0].name);
                              clh->line[0]=getdq[0].line;
                              clh->line[1]=-1;
                              clh->layer=llayer;
                              clh->next=NULL;
                              fprintf(out,"\n%d %d",2,clh);
                              getsy();
                              break;
                   case 3:i=chjtb(getdq[0].name);
                              if(i==0)
                                {
                                   fprintf(out,"\n%d %d",6,yfchc(getdq[0].name,"illegal word"));
                                 getsy();
                                }
                                 else{
                                       fprintf(out,"\n%d %d",0,i);
                                       getsy();
                                       }
                                 break;
                      case 4: clh=malloc(sizeof(struct cl));
                              i=2;
                              cln=set(llayer,&i,"");
                              *cln=clh;
                              strcpy(clh->name,"");
                              clh->type=3;
                              clh->b=atof(getdq[0].name);
                              clh->line[0]=getdq[0].line;
                              clh->line[1]=-1;
                              clh->layer=llayer;
                              clh->next=NULL;
                 fprintf(out,"\n%d %d",2,clh);
            getsy();
            break;
    case 5:clh=malloc(sizeof(struct cl));
        i=2;
        cln=set(llayer,&i," ");
        *cln=clh;
        strcpy(clh->name," ");
        clh->type=2;
        clh->line[0]=getdq[0].line;
        clh->line[1]=-1;
        clh->c[0]='\0';
        i=1;
        while(getdq[0].t==5&&i<80&&doorkey<=3){
            i=i+1;
            strcat(clh->c,getdq[0].name);
            getsy();
            }
        clh->layer=llayer;
        clh->next=NULL;
        fprintf(out,"\n%d %d",2,clh);
        break;
        }/*end switch*/
    }/*end while*/
    fclose(in);
    fclose(out);
    if(head->f==NULL)
        return(1);
    else{
        ii=1;
        cchh=head->f;
        printf("\n\n\n");
        while(cchh!=NULL&&ii<=10){
            printf("\n Foundq errot at ");
          i=0;
    while(cchh->line[i]!=-1)
    {
     printf("%d",cchh->line[i]);
    if(cchh->line[i+1]==-1)
    printf(" : ");
        else printf(" , ");
        i=i+1;
       }/*end while(pp.f->line[i]!=-1)*/
    if(strlen(cchh->name)!=0)
    printf("'%s' is ",cchh->name);
    printf("%s",cchh->why);
    cchh=cchh->next;
    if(ii==10)
    {
      ii=1;
      printf("\n Press any key to continue.");
    getch();
    printf("\n");
    }
    ii=ii+1;
    }/*end while(cchh!=NULL&&ii<=10)*/
    return(0);
    }
}

  void freenode(void)/*free the node*/
    {
        union all   
        {
            struct bl *a;/*1*/
                        struct cl *b;/*2*/
                        struct shz *c;/*3*/
                        struct cjbl *e;/*5*/
                        struct cch *f;/*6*/
            }pp,ll;
            struct hsh * hshh,* hshf;
    if(head->a!=NULL)
    {
    pp.a=head->a;
    while(pp.a!=NULL)
    {
    ll.a=pp.a;
    pp.a=pp.a->next;
    free(ll.a);
    }
    }/*end if(a)*/
        if(head->b!=NULL)
    {
    pp.b=head->b;
    while(pp.b!=NULL)
    {
    ll.b=pp.b;
    pp.b=pp.b->next;
    free(ll.b);
    }
    }/*end if(b)*/

         if(head->c!=NULL)
    {
    pp.c=head->c;
    while(pp.c!=NULL)
    {
    ll.c=pp.c;
    pp.c=pp.c->next;
    free(ll.c);
    }
    }/*end if(c)*/
    
       if(head->f!=NULL)
    {
    pp.f=head->f;
    while(pp.f!=NULL)
    {
    ll.f=pp.f;
    pp.f=pp.f->next;
    free(ll.f);
    }
    }/*end if(f)*/

      hshh=head->next;
    free(head);
    while(hshh!=NULL)
    {
     if(head->a!=NULL)
      {
        pp.a=head->a;
         while(pp.a!=NULL)
         {
          ll.a=pp.a;
         pp.a=pp.a->next;
         free(ll.a);
         }
      }/*end if(a)*/
        if(head->b!=NULL)
    {
           pp.b=head->b;
      while(pp.b!=NULL)
        {
      ll.b=pp.b;
          pp.b=pp.b->next;
             free(ll.b);
                }
        }/*end if(b)*/

        if(head->c!=NULL)
    {
      pp.c=head->c;
        while(pp.c!=NULL)
           {
                ll.c=pp.c;
             pp.c=pp.c->next;
              free(ll.c);
               }
            }/*end if(c)*/

            hshf=hshh;
        hshh=hshh->next;
        free(hshf);
    }/*end while(hshh!=NULL)*/
    }/*end freenode(void)*/


     int cffx(void){
    char c;
    while(1){
        if(cfql()==1)
        {
            printf("\n\n You are success!");
            getch();
            return(1);
        }
        else{
            printf("\n Do you want to compile again! y/n");
            while((c=getch())!='y'&&c!='n');
            if(c=='n')return(0);
                else freenode();
                }
            }
        }


       void main()/*the main programe*/
       {
    cffx();
    out=fopen("aaa.c","w");
    testpro();
    fclose(out);
    }   
    
  

⌨️ 快捷键说明

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