zoukankan      html  css  js  c++  java
  • 实验二

    一、        实验目的

             编制一个词法分析程序

    二、        实验内容和要求

    1. 输入:源程序字符串
    2. 输出:二元组(种别,单词本身)
    3. 待分析语言的词法规则

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

    1. 1.      源程序名:压缩包文件(rarzip)中源程序名 cifafenxi.c  cifafenxi.exe
    1. 2.      原理分析及流程图

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

        do

        {

               scanf("%s ",input);

               i=0;

               while(input[i] !='')

               {

                      //printf("char=%c ",input[i]);

                      if(input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '=')       //对字符串中的表达式做分词,并记录相应运算符 

                      {

                             //printf(" hello ");

                             p=(NODE*)malloc(sizeof(NODE));

                             p->next=NULL;

                             p->string=(char*)malloc(sizeof(char)*2);

                             p->string[0]='';

                             strncpy(p->string,&input[i],1);

                             p->next=yunsuanfu->next;

                             yunsuanfu->next=p;

                      //     printf("yunsuanfu is %c ",input[i]);

                      }

                      else if(ispunct(input[i]) && input[i] != '~')   //对字符串中出现界符的地方做分词,并记录相应界符

                      {

                             //ch=(char*)malloc(sizeof(NODE));

                             p=(NODE*)malloc(sizeof(NODE));

                             p->next=NULL;

                             p->string=(char*)malloc(sizeof(char)*2);

                             p->string[0]='';

                             strncpy(p->string,&input[i],1);

                             p->next=jiefu->next;

                             jiefu->next=p;

                      }

                      i++;

               }

               ch=strtok(input,delim);

               do

               {

                      if(input[0] =='~')

                             break;

                      q=head;

                      p=(NODE*)malloc(sizeof(NODE));

                      p->next=NULL;

                      p->string=(char*)malloc(sizeof(input));

                      p->string[0]='';

                      strcat(p->string,ch);           //连接前后字符串,合成新的字符串

                      while(q->next !=NULL)

                             q=q->next;

                      p->next=q->next;

                      q->next=p;

               }while(ch=strtok(NULL,delim));         

        }while(input[0] !='~');

                      if(strspn(p->string,biaowords))              //使用strspn()来对字符串中是否含有biaowords数组中的字符,若有则为标识符

                      {

                                    q->string=p->string;

                                    q->next=biaoshi->next;

                                    biaoshi->next=q;                            

                      }

     

    1. 4.      运行结果及分析

    一般必须配运行结果截图,结果是否符合预期及其分析。

       (截图需根据实际,截取有代表性的测试例子)

    四、        实验总结

    这个实验一开始不是很能理解,感觉非常难,问了一些会的同学,参考了网上的一些资料,才逐步有了一些了解。这个实验需要为不同的关键词分配不同的种别码,再逐步输出,我的代码相比别人还很拙略,还要更加努力。

  • 相关阅读:
    程序设计实习课(0)资源链接
    解决clion2016.3不能支持搜狗输入法的问题
    四元数运动学笔记(5)IMU驱动的运动误差方程
    四元数运动学笔记(4)旋转的雅克比矩阵
    四元数运动学笔记(3)四元数和旋转相关的约定表述
    四元数运动学笔记(2)旋转向量,旋转矩阵和四元数的关系
    单应矩阵,基本矩阵,本质矩阵
    ROS标定IDS相机
    四元数运动学笔记(1)旋转的表示
    IMU Noise Model
  • 原文地址:https://www.cnblogs.com/zd983886992/p/6030333.html
Copyright © 2011-2022 走看看