实验一、词法分析程序实验

专业:商软   姓名:郑润凯  学号:201506110018

一、实验目的 

编制一个词法分析程序 

二、实验内容和要求

实验内容:输入一段字符串,从字符串表示的源程序中识别出具有独立意义的单词符号,根据输入的字符串,按照种别码分类识别出对应的单词符号。

 实验要求:输入:源程序字符串;

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

待分析语言的词法规则。 

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

1.      源程序名:cifa.c

可执行程序名:cifa.exe

  1. 2.      原理分析及流程图

对构成源程序的字符流进行扫描然后根据构词规则识别单词(也称单词符号或符号)

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

  #include<stdio.h>

#include<string.h>

void Fenxi(char c,char b);

void word(char a[]);

void number(char a[]);

int i;  //定义全局变量i

int s=1; //用来记录是否存在非法字符

main(){

    char a[50];

    printf("请输入源程序:");

    gets(a);

    printf("您要分析的源程序为:");

    printf("%s",a);

    printf("\n");

    for(i=0;(a[i]!='\0')&&(i<50)&&s==1;i++)

    {

        if((a[i]>='a' && a[i]<='z')||(a[i]>='A' && a[i]<='Z'))

            word(a);

        else if(a[i]>='0' && a[i]<='9')

            number(a);

        else

            Fenxi(a[i],a[i+1]);

    }

    printf("\n");

}

 

void number(char a[])  //对数字字符进行扫描分析

{

    char b[50];

    int m,k=0,t;

    m=i;

    while(a[m]>='0' && a[m]<='9')  

    {

        b[k]=a[m];   //用数组b存放数组a中的数字

        k++;

        m++;

    }

    i=m-1;

    printf("(11,");

    for(t=0;t<k;t++)

            printf("%c",b[t]);

    printf(")");

    printf("\n");

}

 

void word(char a[])   //对字母字符进行扫描分析,并识别保留字

{

    int k=0,m,flag=0,t;

    char b[50];

    char *key[6]={"begin","if","then","while","do","end"};

    m=i;

    while((a[m]>='a'&&a[m]<='z')||(a[m]>='A'&&a[m]<='Z'))  //用数组b存放数组a中的字母

    {

        b[k]=a[m];   

        k++;

        m++;

        b[k]='\0';

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

    }

    i=m-1;

    for(t=0;t<6;t++)

    {

        if(strcmp(b,key[t])==0)  //将数组b与关键字进行比较

        {

            printf("(%d,%s)",t+1,key[t]);  //输出关键字

            flag=1;

            printf("\n");

        }

    }

    if(flag==0)

    {

        printf("(10,%s)",b);   //输出标识符

        printf("\n");

    }

}

void Fenxi(char c,char b)  //对特殊字符进行扫描分析

{

    switch(c){

        case ' ':

            break;

        case '+':

            printf("(13,+)\n");

            break;

        case '-':

            printf("(14,-)\n");

            break;

        case '*':

            printf("(15,*)\n");

            break;

        case '/':

            printf("(16,/)\n");

            break;

        case ':':

            if(b=='=')

            {

                i++;

                printf("(18,:=)\n");

            }

            else

                printf("(17,:)\n");

            break;

        case '<':

            if(b=='>')

            {

                i++;

                printf("(21,<>)\n");

            }

            else if(b=='=')

            {

                i++;

                printf("(22,<=)\n");

            }

            else

                printf("(20,<)\n");

            break;

        case '>':

            if(b=='=')

            {

                printf("(24,>=)\n");

                i++;

            }

            else

                printf("(23,>)\n");

            break;

        case '=':

            printf("(25,=)\n");

            break;

        case ';':

            printf("(26,;)\n");

            break;

        case '(':

            printf("(27,()\n");

            break;

        case ')':

            printf("(28,))\n");

            break;

        case '#':

            printf("(0,#)\n");

            break;

        default:

            {

            printf("\n存在字符 '%c',无法继续识别!\n",c);

            s=0;    //用s=0记录存在非法字符

            break;

            }

    }

}

4. 运行结果及分析 

符合实验内容以及要求,能识别出输入字符串所对应的单词符号。 

四、实验总结 

刚开始入门,基础薄弱,对于程序不知道如何下手, 没有思路,后查询并参考相关的资料以及相应的代码后,慢慢理解并加以分析,最终生成实验报告。

 posted on 2016-10-12 19:08  Runka  阅读(224)  评论(0编辑  收藏  举报