zoukankan      html  css  js  c++  java
  • 词法分析程序2

    符号与种别码对照表:
    单词符号 种别码 单词符号 种别码
    begin        1       :          17
    if              2       :=        18
    then         3       <          20
    while         4      <=        21
    do ,{        5      <>        22
    end ,}      6        >         23
    l(l|d)*      10     >=         24
    dd*         11      =          25
    +            13       ;          26
    -             14      (           27
    *            15       )           28
    /             16      #           0
    !             31      !=         30
    \ 32

    #include <stdio.h>
    #include <string.h>
    char c[80],t[8],ch;
    int s,p,m,n,sum;
    char *w[6]={"begin","if","then","while","do","end"};
    void scaner();
    main()
    {
     p=0;
     printf("请输入一段字符(以'#'结束)");
     do{
        scanf("%c",&ch);
        c[p++]=ch;       
        }while(ch!='#');
     p=0;
     do{
        scaner();
        switch(s)
         {
          case 11:printf("< %-10d%5d>\n",sum,s);
              break;
          case -1:printf("你输入了错误的单词符号\n");
              getch();
              exit(0);
          default: printf("<%-10s%5d >\n",t,s);
              break;
          }
        }while(s!=0);
        getch();
     }
    
    void scaner()
     {  
    	sum=0;
        for(m=0;m<8;m++)
            t[m++]=NULL;
        ch=c[p++];
        m=0;
        while((ch==' ')||(ch=='\n'))
        ch=c[p++];
        if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
          { 
          while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
          {
    		  t[m++]=ch;
              ch=c[p++];
          }
          p--;
          s=10;
          for(n=0;n<6;n++)
        if(strcmp(t,w[n])==0)
           { 
    		s=n+1;
             break;
           }
          }
        else if((ch>='0')&&(ch<='9'))
          { 
    		while((ch>='0')&&(ch<='9'))
    		{ sum=sum*10+ch-'0';
              ch=c[p++];
    		}
        p--;
        s=11;
          }
        else switch(ch)
        { 
         case '<':t[m++]=ch;
              ch=c[p++];
    		   if(ch=='>')
                {  s=22;
                   t[m++]=ch;
                }
              else
                { 
    			  s=20;
                   p--;
    		  }
    		   if(ch=='=')
                {  s=21;
                   t[m++]=ch;
                }
    			 
              break;
         case '>':t[m++]=ch;
              ch=c[p++];
              if(ch=='=')
                { s=24;
                  t[m++]=ch;
                }
              else
                { s=23;
                  p--;
                }
              break;
         case '+': s=13;
                 t[m++]=ch;
                 break;
    
         case '-':
                s=14;
                t[m++]=ch;  
              break;
    
         case '!':ch=c[p++];
              if(ch=='=')
               { s=30;
                 t[m++]=ch;
               }
              else
              { s=31;
                 p--;
              }
              break;
    
         case '=':
                s=25;
                t[m++]=ch;
                break;
    
         case '*': s=15;
               t[m++]=ch;         
    		   break;
    
         case '/': s=16;
               t[m++]=ch;
               break;
         case '(': s=27;
               t[m++]=ch;
               break;
         case ')': s=28;
               t[m++]=ch;
               break;
         case '{': s=5;
               t[m++]=ch;
               break;
         case '}': s=6;
               t[m++]=ch;
               break;
         case ';': s=26;
              t[m++]=ch;
              break;
         case '\"': s=32;
               t[m++]=ch;
               break;
         case '#': s=0;
               t[m++]=ch;
               break;
         case ':':t[m++]=ch;
              ch=c[p++];
               if(ch=='=')
                {  s=18;
                   t[m++]=ch;
                }
    		   else
              { s=17;
                 p--;
    		  }
               break;
        default: s=-1;
             break;
           }
    	   t[m++]='\0';
      }
    

      

  • 相关阅读:
    P1352 没有上司的舞会
    P1879 [USACO06NOV]玉米田Corn Fields
    P1896 [SCOI2005]互不侵犯
    2019寒假纪中happy之旅
    JZOJ 4249. 【五校联考7day1】游戏
    JZOJ 4248. 【五校联考7day1】n染色
    JZOJ 4252. 【五校联考7day2】QYQ的图
    STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'
    STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'
    重踏学习Java路上_Day02(java 基础上)
  • 原文地址:https://www.cnblogs.com/cgz123/p/5936649.html
Copyright © 2011-2022 走看看