词法规则写法

#include<stdio.h>
main()
{
    char n[100],n1[100],n2[100];
    int i,i1=0,i2=0,j;
    printf("请你输入字符串:\n");
    gets(n);
      for(i=0;i<n;i++)
    {
        j=(int) n[i];
        n1[i]=n[100];
        if((j>=65) && (j<=90)||(j>=90) && (j<=122))
        {
            n1[i1]=n[i];
            i1++;
        }
        else{
             while(i1!=0)
            {
               n1[i1]='\0';
               if(strcmp(n1,"auto")==0||strcmp(n1,"int")==0||strcmp(n1,"double")==0||strcmp(n1,"long")==0||strcmp(n1,"char")==0||strcmp(n1,"float")==0||strcmp(n1,"short")==0||strcmp(n1,"signed")==0||strcmp(n1,"unsigned")==0||strcmp(n1,"struct")==0||strcmp(n1,"union")==0||strcmp(n1,"enum")==0||strcmp(n1,"static")==0||strcmp(n1,"switch")==0||strcmp(n1,"case")==0||strcmp(n1,"default")==0
                         ||strcmp(n1,"break")==0||strcmp(n1,"register")==0||strcmp(n1,"const")==0||strcmp(n1,"volatile")==0||strcmp(n1,"typedef")==0||strcmp(n1,"extern")==0||strcmp(n1,"return")==0||strcmp(n1,"void")==0||strcmp(n1,"continue")==0||strcmp(n1,"do")==0||strcmp(n1,"while ")==0||strcmp(n1,"if")==0||strcmp(n1,"else")==0||strcmp(n1,"for")==0||strcmp(n1,"goto")==0||strcmp(n1,"sizeof")==0)
             {
               n1[i1]=')';
               printf("(关键字,");
               puts(n1);
               i1=0;
               memset(n1,0,100);
             }
              else
             {
               n1[i1]=')';
               printf("(标识符,");
               puts(n1);
               i1=0;
               memset(n1,0,100);
             }

           }

          if((j>48)&& (j<57))
         {
             n2[i2]=n[i];
             i2++;
         }
         else{
               if((int) n[i-1]>48 && (int) n[i-1]<57)
               {n2[i2]=')';
               printf("(常数,");
               puts(n2);
               i2=0;
               memset(n2,0,100);}

         if(j==42||j==43||j==45||j==47||j==61||j==62||j==60)
        {
           if((int) n[i+1]==61)
            {

                  printf("(运算符,%c=)",n[i]);
                  printf("\n");
                  i=i+1;
            }
            else
            {

                printf("(运算符,%c)",n[i]);
                printf("\n");
            }
        }
        else if(j==32)
        {

        }
        else if(j==33||j==44||j==46||j==58||j==59||j==63||j==44)
        {
            printf("(界符,%c)",n[i]);
            printf("\n");
        }
        else if(j==34||j==35||j==38||j==39||j==40||j==41||j==123||j==124||j==125||j==126)
        {
            printf("(特殊符号,%c)",n[i]);
            printf("\n");
        }
        else
        {
            break;
        }

    }

    }
  }
}

posted on 2016-09-29 21:55  217-吴文成  阅读(173)  评论(0编辑  收藏  举报