词法分析实验

实验一、词法分析实验

商业软件3班  谭颖欣 244

一、        实验目的

 

编制一个词法分析程序。通过设计一个编译词法分析程序,实现对词法分析转换的理解,加深对转换过程的认识,通过这个软件把理论知识运用到实际之中。

 

二、        实验内容和要求

 

输入:源程序字符串

输出:二元组(种别,单词本身

 

三、        实验方法、步骤及结果测试

 

  1. 1.      源程序名:词法分析_244谭颖欣.c

可执行程序名:词法分析_244谭颖欣.exe

  1. 2.      原理分析及流程图
    #include<stdio.h>
    #define max 200
    int i,n,syn;
    
    char pro[max],lin[20];
    char *word[6]={"begin","if","then","while","do","end"};
    char ch;
    scaner()
    {
        int j=0;
        for(n=0;n<20;n++)
            lin[n]=NULL;
            ch=pro[i++];
            while(ch==' ')
                ch=pro[i++];
            if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z')||(ch>=48&&ch<=57))
            {
               if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z'))
               {
                   lin[j++]=ch;
                   ch=pro[i++];
                   while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z')||(ch>=48&&ch<=57))
                   {
                       lin[j++]=ch;
                   ch=pro[i++];
                   }
                    lin[j]='\0';
                    i--;
                    syn=10;
                    for(n=0;n<6;n++)
                        if(strcmp(lin,word[n])==0)
                    {
                        syn=n+1;
                        break;
                    }
               }
               else
               {
                   while(ch>=48&&ch<=57)
                   {
                       lin[j++]=ch;
                       ch=pro[i++];
                   }
                   i--;
                   lin[j]='\0';
                   syn=11;
               }
            }
            else
            {
                if(ch=='<')
                    {lin[j++]=ch;
                ch=pro[i++];
                if (ch=='>')
                    {
                        syn=21;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else if(ch=='=')
                    {
                        syn=22;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else
                    {
                        syn=20;
                lin[j]='\0';
                i--;
                }
                    }
                else if(ch=='>')
                    { lin[j++]=ch;
                ch=pro[i++];
                if (ch=='=')
                    {
                        syn=24;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else
                    {
                        syn=23;
                lin[j]='\0';
                i--;
                }
                }
                else if(ch==':')
                    {
                        lin[j++]=ch;
                ch=pro[i++];
                if (ch=='=')
                    {
                        syn=18;
                lin[j++]=ch;
                lin[j]='\0';}
                else
                    {
                        syn=17;
                lin[j]='\0';
                i--;
                }
                }
                else if(ch=='+')
                    {syn=13;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else if(ch=='-')
                    {
                        syn=14;
                lin[j++]=ch;
                lin[j]='\0';}
                else if(ch=='*')
                    {
                        syn=15;
                lin[j++]=ch;
                lin[j]='\0';}
                else if(ch=='/')
                    {
                        syn=16;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else if(ch=='=')
                    {
                        syn=25;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else if(ch==';')
                    {
                        syn=26;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else if(ch=='(')
                            {
                                syn=27;
                lin[j++]=ch;
                lin[j]='\0';
                }
                else if(ch==')')
                    {
                        syn=28;
                lin[j++]=ch;
                lin[j]='\0';}
                else if(ch=='#')
                {
                    syn=0;
                    lin[j++]=ch;
                    lin[j]='\0';
                }
                else
                        syn=-1;
                }
                        return syn;
            }
    
    void main()
    {
        int i=0 ;
        printf("**************************************************************************\n");
        printf("*********************************种别编码*********************************\n");
        printf("**************************************************************************\n");
        printf("(1,begin)       (2,if)                 (3,then)         (4,while)    (5,do)\n");
        printf("(6,end)         (10,基本标示符)        (11,数字)        (13, )       (14,-)\n");
        printf("(15,*)          (16,/)                 (17,:)           (18,:=)      (20,<)\n");
        printf("(21,<>)         (22,<=)                (23,>)           (24,>=)      (25,=)\n");
        printf("(26,;)          (27,()                 (28,))           (0,#)\n");
        read();
        printf( "读入的字符串如下: \n\n" );
        puts(pro);
        printf( "词法分析结果如下:\n" );
        printf( "\n" );
        do   {
                scaner();
        switch(syn)
        {
            case 11:
                printf ("(%d,%s)\n",syn,lin);
                break;
                case -1:
                    printf("错误符号\n");
                    break;
                    default:
                        printf ("(%d,%s)",syn,lin); }
                        } while (syn!=0);
                        printf( "\n词法分析结束\n" );
                         scanf("%c",&i);
    1. 3.      主要程序段及其解释:
    2.  if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z')||(ch>=48&&ch<=57))
              {
                 if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z'))
                 {
                     lin[j++]=ch;
                     ch=pro[i++];
                     while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z')||(ch>=48&&ch<=57))
                     {
                         lin[j++]=ch;
                     ch=pro[i++];
                     }
                      lin[j]='\0';
                      i--;
                      syn=10;
                      for(n=0;n<6;n++)
                          if(strcmp(lin,word[n])==0)
                      {
                          syn=n+1;
                          break;
                      }
                 }
                 else
                 {
                     while(ch>=48&&ch<=57)
                     {
                         lin[j++]=ch;
                         ch=pro[i++];
                     }
                     i--;
                     lin[j]='\0';
                     syn=11;
                 }
              }
              else
              {
                  if(ch=='<')
                      {lin[j++]=ch;
                  ch=pro[i++];
                  if (ch=='>')
                      {
                          syn=21;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else if(ch=='=')
                      {
                          syn=22;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else
                      {
                          syn=20;
                  lin[j]='\0';
                  i--;
                  }
                      }
                  else if(ch=='>')
                      { lin[j++]=ch;
                  ch=pro[i++];
                  if (ch=='=')
                      {
                          syn=24;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else
                      {
                          syn=23;
                  lin[j]='\0';
                  i--;
                  }
                  }
                  else if(ch==':')
                      {
                          lin[j++]=ch;
                  ch=pro[i++];
                  if (ch=='=')
                      {
                          syn=18;
                  lin[j++]=ch;
                  lin[j]='\0';}
                  else
                      {
                          syn=17;
                  lin[j]='\0';
                  i--;
                  }
                  }
                  else if(ch=='+')
                      {syn=13;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else if(ch=='-')
                      {
                          syn=14;
                  lin[j++]=ch;
                  lin[j]='\0';}
                  else if(ch=='*')
                      {
                          syn=15;
                  lin[j++]=ch;
                  lin[j]='\0';}
                  else if(ch=='/')
                      {
                          syn=16;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else if(ch=='=')
                      {
                          syn=25;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else if(ch==';')
                      {
                          syn=26;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else if(ch=='(')
                              {
                                  syn=27;
                  lin[j++]=ch;
                  lin[j]='\0';
                  }
                  else if(ch==')')
                      {
                          syn=28;
                  lin[j++]=ch;
                  lin[j]='\0';}
                  else if(ch=='#')
                  {
                      syn=0;
                      lin[j++]=ch;
                      lin[j]='\0';
                  }
                  else
                          syn=-1;
                  }
                          return syn;
              }
    3. 判断词的种类
    4. .      运行结果及分析
    5. 四、        实验总结

       

      对于词法分析的输入,有些不懂。通过用字符数组的方式储存来解决。

posted on 2016-10-29 11:02  244谭颖欣  阅读(239)  评论(0编辑  收藏  举报