• 大小: 76KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-08-17
  • 语言: C/C++
  • 标签:

资源简介

设计、编制并调试一个词法分析程序,加深对词法分析原理的理解。

资源截图

代码片段和文件信息

#include          /*定义I/O库所用的某些宏和变量*/
#include          /*定义字符串库函数*/
char prog[80]token[8]ch;
int synpmnsum;        /*p是缓冲区prog的指针,m是token的指针*/
char *rwtab[6]={“begin““if““then““while““do““end“};
 scaner();   /*词法扫描程序*/
main()
{p=0;
 printf(“\n please input a string(end with ‘#‘):\n“);
 do{
    scanf(“%c“&ch);
    prog[p++]=ch;
    }while(ch!=‘#‘);
 p=0;
 do{
    scaner();
    switch(syn)
     {case 11:printf(“( %-10d%5d )\n“sumsyn);
          break;
      case -1:printf(“you have input a wrong string\n“);
          getch();
          exit(0);
      default: printf(“( %-10s%5d )\n“tokensyn);
          break;
      }
    }while(syn!=0);
    getch();
 }

 scaner()
 {  sum=0;
    for(m=0;m<8;m++)token[m++]=NULL;
    ch=prog[p++];
    m=0;
    while((ch==‘ ‘)||(ch==‘\n‘))ch=prog[p++];
    if(((ch<=‘z‘)&&(ch>=‘a‘))||((ch<=‘Z‘)&&(ch>=‘A‘)))
      { while(((ch<=‘z‘)&&(ch>=‘a‘))||((ch<=‘Z‘)&&(ch>=‘A‘))||((ch>=‘0‘)&&(ch<=‘9‘)))
      {token[m++]=ch;
       ch=prog[p++];
      }
      p--;
      syn=10;
      for(n=0;n<6;n++)
    if(strcmp(tokenrwtab[n])==0)
       { syn=n+1;
         break;
       }
      }
    else if((ch>=‘0‘)&&(ch<=‘9‘))
      { while((ch>=‘0‘)&&(ch<=‘9‘))
    { sum=sum*10+ch-‘0‘;
      ch=prog[p++];
    }
    p--;
    syn=11;
      }
    else switch(ch)
    { case ‘<‘:token[m++]=ch;
          ch=prog[p++];
           if(ch==‘=‘)
            {  syn=22;
               token[m++]=ch;
            }
          else
            {  syn=20;
               p--;
            }
          break;
     case ‘>‘:token[m++]=ch;
          ch=prog[p++];
          if(ch==‘=‘)
            { syn=24;
              token[m++]=ch;
            }
          else
            { syn=23;
              p--;
            }
          break;
     case ‘+‘: token[m++]=ch;
          ch=prog[p++];
          if(ch==‘+‘)
            { syn=17;
              token[m++]=ch;
            }
          else
            { syn=13;
              p--;
            }
          break;

     case ‘-‘:token[m++]=ch;
          ch=prog[p++];
          if(ch==‘-‘)
            { syn=29;
              token[m++]=ch;
            }
          else
            { syn=14;
              p--;
            }
          break;

     case ‘!‘:ch=prog[p++];
          if(ch==‘=‘)
           { syn=21;
             token[m++]=ch;
           }
          else
          { syn=31;
             p--;
          }
          break;

     case ‘=‘:token[m++]=ch;
          ch=prog[p++];
          if(ch==‘=‘)
            { syn=25;
              token[m++]=ch;
            }
          else
            { syn=18;
              p--;
            }
          break;
     case ‘*‘: syn=15;
           token[m++]=ch;
           break;
     case ‘/‘: syn=16;
           token[m++]=ch;
           break;
     case ‘(‘: syn=27;
           token[m++]=ch;
           break;
    

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10909  2008-06-03 07:08  词法分析\1.jpg

     文件      16946  2008-06-03 07:10  词法分析\2.jpg

     文件       3599  2008-06-03 07:07  词法分析\qiyubingcifa.c

     文件      12908  2008-06-03 07:08  词法分析\qiyubingcifa.exe

     文件       3162  2008-06-03 07:08  词法分析\qiyubingcifa.obj

     文件      12908  2008-06-03 07:08  词法分析\tempfile.exe

    ..A..H.       162  2008-06-03 06:33  词法分析\~$冰200507096-词法分析程序报告.doc

     文件     112128  2008-06-03 07:07  词法分析\綦玉冰200507096-词法分析程序报告.doc

     目录          0  2008-06-14 13:58  词法分析

----------- ---------  ---------- -----  ----

               172722                    9


评论

共有 条评论

相关资源