实验一  词法分析程序045

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验一  词法分析程序045相关的知识,希望对你有一定的参考价值。

实验一  词法分析程序实验

专业 计算机科学与技术   姓名 邹汉辉  学号 201508030045

一、实验目的

1. 编制一个词法分析程序

二、 实验内容和要求

1. 输入:源程序字符串

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

3. 待分析语言的词法规则

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

1.  源程序名:压缩包文件(rarzip)中源程序名 cifafenxi.c

可执行程序名:cifafenxi.exe

2.  原理分析及流程图

技术分享

 

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

   #include<stdio.h>

#include<string.h>

int ccc(char a[100],int i)

{

    switch(a[i])

    {

        case ‘+‘:

            {

                printf("(13+)");

                return(1);    

            }

        

        case ‘-‘:

            {

                printf("(14-)");

                return(1);

            }

        case ‘*‘:

            {

                printf("(15*)");

                return(1);

            }

        case ‘/‘:

            {

                printf("(16/)");

                return(1);

            }

        case ‘:‘:

            {

                if(a[i+1]==‘=‘)

                {

                    printf("(18:=)");

                    return(2);

                }

                else

                    printf("(17:)");

                return(1);

 

            }

        case ‘<‘:

            {

                if(a[i+1]==‘>‘)

                {

                    printf("(21<>)");

                    return(2);

                }

                else if(a[i+1]==‘=‘)

                {

                    printf("(22<=)");

                    return(2);

                }

                else

                    printf("(20<");

                return(1);

 

            }

        case ‘>‘:

            {

                if(a[i+1]==‘=‘)

                {

                    printf("(24>=)");

                    return(2);

                }

                else

                    printf("(23>");

                return(1);

 

            }

        case ‘=‘:

            {

                printf("(25=)");

                return(1);

            }

        case ‘;‘:

            {

                printf("(26;)");

                return(1);

            }

        case ‘(‘:

            {

                printf("(27()");

                return(1);

            }

        case ‘)‘:

            {

                printf("(28)");

                return(1);

            }

        default:

            return(0);

            break;

 

    }

}

void number(char a[10])

{

    int i=0;

    char text[10];

    while(a[i]!=‘\\0‘)

    {

        text[i]=a[i];

        i++;

    }

    text[i]=‘\\0‘;

    printf("(11,%s)",text);

}

void fing(char a[10])

{

    int i=0;

    char text[10];

    while(a[i]!=‘\\0‘)

    {

        text[i]=a[i];

        i++;

    }

    text[i]=‘\\0‘;

 

    if( strcmp(text,"begin")==0)

    {

        printf("(1,begin)");

    }

    else if( strcmp(text,"if")==0)

    {

        printf("(2,if)");

    }

    else if( strcmp(text,"then")==0)

    {

        printf("(3,then)");

    }

    else if( strcmp(text,"while")==0)

    {

        printf("(4,while)");

    }

    else if( strcmp(text,"do")==0)

    {

        printf("(5,do)");

    }

    else if( strcmp(text,"end")==0)

    {

        printf("(6,end)");

    }

    else

    {

        printf("(10,%s)",text);

    }

}

 

main(){

    int i=0,j=-1,l=0,x=0;

    char a[100],text[10];

    printf("输入源程序,#号结束");

    gets(a);

    while(1)

    {

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

        {

            while(a[i]>=‘0‘&&a[i]<=‘9‘)

                i++;

 

            l=0;

        

            for(++j;j<i;j++)

            {

                text[l]=a[j];

                l++;

            }

    

            text[l]=‘\\0‘;

 

            number(text);

            i--;

        }

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

        {l=0;

        

            for(i;a[i]>=‘a‘ && a[i]<=‘z‘ ||a[i]>=‘A‘&&a[i]<=‘Z‘;i++)

            {

                text[l]=a[i];

                l++;

            }

            i--;

    

            text[l]=‘\\0‘;

            fing (text);

            

        }

        else

        {

            if(a[i]==‘#‘)

            {

                printf("(0,#)");

                break;

            }

            

            x=ccc(a,i);

            if(x==1)

            {

                j=i;

            }

            else if(x==2)

            {

                i++;

                j=i;

            }

        }

        i++;

        

    }

 

}

4.  运行结果及分析

技术分享

 

 

实验结果与预期基本相符

四、 实验总结

这次试验对我来说难度很大,查阅了大量的资料和别人的实验报告,才一步步完成,以后的理论课和上机课还要多多努力。

 

以上是关于实验一  词法分析程序045的主要内容,如果未能解决你的问题,请参考以下文章

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

词法分析

实验词法分析器实验

词法分析的大嘴

实验一 词法分析程序实验

实验一 词法分析程序实验