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

📄 yffx.c

📁 编译器的实现之二:语法分析
💻 C
字号:
         /*电信系 计算机科学与技术专业  0412班  04117207  钱兴*/
#include "stdio.h"
#include "string.h"
int xl[100];
char str[100],v[100];
char ch1[100]="begin",ch2[100]="if",ch3[100]="then",ch4[100]="while",ch5[100]="do",ch6[100]="end";
int i=0,k,z=0,w,j,u,s=0,a=0,b=0,t,i,nums,o=0,sgn;
main()
{puts("Please input code:");
 gets(str);
 k=strlen(str);
 for (i=0;i<k;i++)
 {if(str[i]==' ') continue;
  if (((str[i]>=65)&&(str[i]<=90))||((str[i]>=97)&&(str[i]<=122)))
     { z=0;
       do
      {
       v[z]=str[i];
       z++;
       i++;
       switch (str[i])
       {case ' ':w=0;break;
        case '\0':w=0;break;
        default:w=1;}
       }while ((w!=0));
       v[z]='\0';
    for(j=0;j<strlen(v);j++)
       {if (((v[j]>=65)&&(v[j]<=90))||((v[j]>=97)&&(v[j]<=122))||((v[j]>=48)&&(v[j]<=57)))
        u=1;
        else {u=0;break;}}
    if (u==1)
     {if (strcmp(v,ch1)==0){sgn=1;printf("(%d,%s)",sgn,v); xl[s]=sgn;s++;}
     else if (strcmp(v,ch2)==0){sgn=2;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
     else if (strcmp(v,ch3)==0){sgn=3;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
     else if (strcmp(v,ch4)==0){sgn=4;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
     else if (strcmp(v,ch5)==0){sgn=5;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
     else if (strcmp(v,ch6)==0){sgn=6;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
     else {sgn=10;printf("(%d,%s)",sgn,v);xl[s]=sgn;s++;}
     }
    else  {printf("(Error:%s) ",v);o=1;}
     }
   else if ((str[i]>=48)&&(str[i]<=57))
     { z=0;
       do
      {
       v[z]=str[i];
       z++;
       i++;
       switch (str[i])
       {case ' ':w=0;break;
       case '\0':w=0;break;
        default:w=1;}
       }while ((w!=0));
       v[z]='\0';
       for(j=0;j<strlen(v);j++)
       {if ((v[j]>=48)&&(v[j]<=57))
        u=1;
        else {u=0;break;}}
        if (u==1)
       {sgn=11;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
       else  {printf("(Error:%s )",v);o=1;}
       }
   else
      if ((str[i]=='#')||(str[i]=='+')||(str[i]=='-')||(str[i]=='*')||(str[i]=='/')||(str[i]=='=')||(str[i]=='(')||(str[i]==')')||(str[i]=='<')||(str[i]=='>')||(str[i]==';')||(str[i]==':'))
       { z=0;
        while ((str[i]!=' '))
       {
       v[z]=str[i];
       z++;
       i++;
       }
       v[z]='\0';
      if (strcmp(v,"+")==0){sgn=13;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"-")==0){sgn=14;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"*")==0){sgn=15;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"/")==0){sgn=16;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,":")==0){sgn=17;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,";")==0){sgn=26;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"=")==0){sgn=25;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"<")==0){sgn=20;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,">")==0){sgn=23;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"(")==0){sgn=27;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,")")==0){sgn=28;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"<>")==0){sgn=21;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,">=")==0){sgn=24;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"#")==0){sgn=0;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,":=")==0){sgn=18;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else if(strcmp(v,"<=")==0){sgn=22;printf("(%d,%s) ",sgn,v);xl[s]=sgn;s++;}
      else {printf("(Error:%s)",v);o=1;}
      }
    else {
         { z=0;
         while (str[i]!=' ')
         {
         v[z]=str[i];
         z++;
         i++;
             }
         v[z]='\0';
                 }
         {printf ("(Error:%s)",v);o=1;}}
    i--;
    printf("\n");
    }

 {
  if (o==1){puts("Error1");goto loop5;}
  if (xl[s-2]!=6) {puts("Error2");goto loop5;}
  else s=0;
  if (xl[s]!=1)  {puts("Error3");goto loop5;}
  else s++;
loop3: if (xl[s]!=10) {puts("Error4");goto loop5;}
  else{s++;
       if (xl[s]!=18)  {puts("Error5");goto loop5;}
       else s++;

loop1: {if ((xl[s]!=10)&&(xl[s]!=11)&&(xl[s]!=27))
       {puts("Error6");goto loop5;}
       else {if ((xl[s]==10)|| (xl[s]==11))
            {s++;
             loop2: if ((xl[s]!=13)&&(xl[s]!=14)&&(xl[s]!=15)&&(xl[s]!=16)&&(xl[s]!=26)&&(xl[s]!=6)&&(xl[s]!=15)&&(xl[s]!=28))
                    {puts("Error7");goto loop5;}
                    else { if (xl[s]==28) {b++;s++; goto loop2;}
                           else if (xl[s]==26) {s++; goto loop3;}
                           else if (xl[s]==6) goto loop4;
                           else  {s++;goto loop1;}
                           }
                            }
                             else {a++;s++;goto loop1;}
        }
         }
          }
loop4:if ((a==b)&&(xl[s+1]==0)) printf("success");
      else {puts("Error8");goto loop5;}
 }
loop5: ;
    getch();
    }


⌨️ 快捷键说明

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