• 实验报告一 词法分析程序


    实验一、词法分析实验

    商业软件工程   刘锦泉  201506110139

    一、        实验目的

           编制一个词法分析程序。

    二、        实验内容和要求

    输入:源程序字符串

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

    待分析语言的词法规则

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

    1.     源程序名:词法分析.c

    可执行程序名:词法分析.exe

    2.     原理分析及流程图

    待分析的简单语言的词法

    (1)   关键字:

    begin  if  then  while  do  end

    (2)   运算符和界符:

    :  =  +  -  *  /  <   >  <=  < >  >=  ;  ( )  #

    (3)各种单词符号对应的种别码

    单词符号

    种别码

    单词符号

    种别码

    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

    3.     主要程序段及其解释:

    1、构建主程序:
    void
    main() { int i=-1; printf("please input sentence, end of '#' ! "); do{ i++; scanf("%c",&ch[i]); }while(ch[i]!='#'); scanner(); }

    2、编写扫描子程序:

    void scanner()
    { int i=0,j=0,k=0,t,l=0,c=0,f=0,n=0,m=0,q=0;
    char cc[100]="";
    char cf[100]="";
      while(ch[i]!='#')
      {
      while (ch[i]!='#' && ch[i]!=' ' && ch[i]!='
    ' && ch[i]!='    ')
          i++;
     for(;j<i;j++)
     {
        if((ch[j]>='a'&&ch[j]<='z') || (ch[j]>='A'&&ch[j]<='Z'))
        {
            tempc[c]=ch[j];
            c++;
        }
     
      else if(ch[j]>='0'&& ch[j]<='9')
      {
        tempn[n]=ch[j];
          n++;
      }
     
      else
      {
        tempf[f]=ch[j];
        f++;
      }
     }
        if(c>0)
        {
        if(strcmp(tempc,r1)==0)
        t=1;
       else if(strcmp(tempc,r2)==0)
        t=2;
      else if(strcmp(tempc,r3)==0)
        t=3;
       else if(strcmp(tempc,r4)==0)
        t=4;
        else if(strcmp(tempc,r5)==0)
        t=5;
       else if(strcmp(tempc,r6)==0)
        t=6;
        if(t>0)
          Swit(t);
        else
            for(;k<c;k++)
            printf("%c
    ",tempc[k]);
        }
         if(n>0)
         {
              for(;q<n;q++)
            printf("%d
    ",tempn[q]-48);
         }
         if(f>0)
         {
            for(;l<f;l++)
           {
            switch(tempf[l])
            {
            case '+': m=13;break;
            case '-': m=14;break;
            case '*': m=15;break;
            case '/': m=16;break;
            case ':':
                if(tempf[l+1]=='=')
                {
                    m=18;
                    l++;
                }
                else m=17;
                break;
            case '<':
                if(tempf[l+1]=='=')
                {
                     m=21;
                     l++;
                }
                else if (tempf[l+1]=='>')
                {
                    m=22;
                    l++;
                }
                else m=20;
                break;
            case '>':
                if(tempf[l+1]=='=')
                {
                    m=24;
                    l++;
                }
                else m=23;
                break;
            case '=': m=25;break;
            case ';': m=26;break;
            case '(': m=27;break;
            case ')': m=28;break;
            default :printf("%c
    ",tempf[l]);
            }
            Swit(m);
             getchar();
           }
         }
        l++;
        t=0;
        if(c>0)
          for(;c>0;c--)
          tempc[c]=cc[0];
         if(ch[i]!='#')
            i++;
      }
    }
    

    4.     运行结果及分析

    四、        实验总结

    心得体会:

    一、对实验原理有更深的理解

    二、对词法分析在实践中的应用有深刻的理解。在实践的基础上,把所学过的知识应用于实际应用,更深刻的理解了词法分析以及编译原理的实际应用

    三、通过本次编译原理实践,激发了我学习的积极性。         

     

  • 相关阅读:
    vue-路由传参
    ES6模板字符串
    es6中Set和Map数据结构
    本周面试题
    var、let和const定义变量的特点
    修改this的指向
    Echarts图表插件
    ES6学习
    swiper插件学习
    每日刷题4
  • 原文地址:https://www.cnblogs.com/LauSir139/p/5943822.html
Copyright © 2020-2023  润新知