词法分析器代码 编译原理C语言

词法分析程序

通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。

编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。(遇到错误时可显示“Error”,然后跳过错部分继续显示)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define keywordSum 14
char *keyword[keywordSum]={
    
    "int","char","long","void","bool", "if","else","do","while","for","then","main","return","include"};

//判断是字母
bool isalpha(char ch)
{
    
    
    if (ch>='a' && ch<='z' || ch>='A' && ch<='Z')
        return true;
    else
        return false;
}
//判断是数字
bool isalnum(char ch)
{
    
    
    if(ch>='0' && ch<='9')
        return true;
    else
        return false;
}
//判断是“+”
bool is1(char ch)
{
    
    
    if(ch=='+')
        return true;
    else
        return false;
}
//判断是“-”
bool is2(char ch)
{
    
    
    if(ch=='-')
        return true;
    else
        return false;
}
bool is3(char ch)
{
    
    
    if(ch=='*')
        return true;
    else
        return false;
}
bool is4(char ch)
{
    
    
    if(ch=='/')
        return true;
    else
        return false;
}
bool is5(char ch)
{
    
    
    if(ch=='=')
        return true;
    else
        return false;
}
bool is6(char ch)
{
    
    
    if(ch=='(')
        return true;
    else
        return false;
}
bool is7(char ch)
{
    
    
    if(ch==')')
        return true;
    else
        return false;
}
bool is8(char ch)
{
    
    
    if(ch==';')
        return true;
    else
        return false;
}
bool is9(char ch)
{
    
    
    if( ch=='{' )
        return true;
    else
        return false;
}
bool is10(char ch)
{
    
    
    if(ch=='}')
        return true;
    else
        return false;
}
bool is11(char ch)
{
    
    
    if(ch=='<')
        return true;
    else
        return false;
}
bool is12(char ch)
{
    
    
    if(ch=='>')
        return true;
    else
        return false;
}
bool is13(char ch)
{
    
    
    if(ch=='#')
        return true;
    else
        return false;
}
bool is14(char ch)
{
    
    
    if(ch==',')
        return true;
    else
        return false;
}
bool is15(char ch)
{
    
    
    if(ch=='.')
        return true;
    else
        return false;
}
bool is16(char ch)
{
    
    
    if(ch=='&')
        return true;

    else
        return false;
}
bool is17(char ch)
{
    
    
    if(ch=='|')
        return true;
    else
        return false;
}
bool is18(char ch)
{
    
    
    if(ch=='!')
        return true;
    else
        return false;
}
bool is28(char ch)
{
    
    
    if(ch=='_')
        return true;
    else
        return false;
}
//出错处理
void error()
{
    
    

}
int main()
{
    
    
    FILE *fp,*fg,*re,*te;
//源文件输入输出,分析文件上午读,写
    char ch,token[40];
    int es=0,j,n;
//写源程序
    printf("输入源程序(输入$结束):\n");
    if((fp=fopen("D://MyOffice//code blocks//dem.txt","w"))==NULL)
    {
    
    
        printf("Failure to open demo.txt!\n");
        return 0;
    }
    ch=getchar();

    while(ch!='$')
        {
    
    
            fputc(ch,fp);
            ch=getchar();
        }
    fclose(fp);
//读文件中的代码
    if((re=fopen("D://MyOffice//code blocks//dem.txt","r"))==NULL)
    {
    
    
        printf("Failure to open demo.txt!\n");
        return 0;
    }
//往文件中写入分析字符数字表
    if((te=fopen("D://MyOffice//code blocks//dem.txt","a"))==NULL)
    {
    
    
        printf("Failure to open demo.txt!\n");
        return 0;
    }
//分析的程序
    ch=getc(re);
    while(ch!=EOF)
    {
    
    
        while(ch==' '||ch=='\n'||ch=='\t')
            ch=getc(re);
        if(ch==EOF)
            break;
        if(isalpha(ch))
        {
    
    
            token[0]=ch;
            j=1;
            ch=getc(re);
            while(isalpha(ch)||ch=='_'||isalnum(ch))
            {
    
    
                token[j++]=ch;
                ch=getc(re);
            }
            token[j]='\0';
            n=0;
            while((n<keywordSum)&&strcmp(token,keyword[n])!=0)
                n++;

            if(n>=keywordSum)
                fprintf(te,"\n( 28 , %s )",token);
            else
                fprintf(te,"\n( %d , %s )",n+30,token);


        }
        else if(is28(ch))//标识符
        {
    
    
            token[0]=ch;
            j=1;
            ch=getc(re);
            while(isalpha(ch)||ch=='_'||isalnum(ch))
            {
    
    
                token[j++]=ch;
                ch=getc(re);
            }
            token[j]='\0';
                fprintf(te,"\n( 28 , %s )",token);
        }
        else if(isalnum(ch))//数字开头
        {
    
    
            token[0]=ch;
            j=1;
            ch=getc(re);
            while(isalnum(ch))
            {
    
    
                token[j++]=ch;
                ch=getc(re);
            }
            token[j]='\0';
            fprintf(te,"\n( 29 , %s )",token);
        }
        else if(is1(ch))
        {
    
    
            fprintf(te,"\n( 1, %c )",ch);
            ch=getc(re);
        }
        else if(is2(ch))
        {
    
    
            fprintf(te,"\n( 2, %c )",ch);
            ch=getc(re);
        }

        else if(is3(ch))
        {
    
    
            if(getc(re)!='/')

                fprintf(te,"\n( 3, %c )",ch);
            ch=getc(re);
        }
        else if(is4(ch))
        {
    
    
            ch=getc(re);
            if(ch=='/')
            {
    
    
                while(ch!='\n')//注释
                {
    
    
                 ch=getc(re);
                }
                

            }
            else if(ch=='*')
            {
    
    
                do{
    
    

                     ch=getc(re);
                }while(ch!='*');
                ch=getc(re);

                fprintf(te,"\n( 21 , /* )");

            }
            else
                fprintf(te,"\n( 4, / )");



            ch=getc(re);
        }
        else if(is5(ch))
        {
    
    
            fprintf(te,"\n( 5, %c )",ch);
            ch=getc(re);
        }
        else if(is6(ch))
        {
    
    
            fprintf(te,"\n( 6, %c )",ch);
            ch=getc(re);
        }
        else if(is7(ch))
        {
    
    
            fprintf(te,"\n( 7, %c )",ch);
            ch=getc(re);
        }
        else if(is8(ch))
        {
    
    
            fprintf(te,"\n( 8, %c )",ch);
            ch=getc(re);
        }
        else if(is9(ch))
        {
    
    
            fprintf(te,"\n( 9, %c )",ch);
            ch=getc(re);
        }
        else if(is10(ch))
        {
    
    
            fprintf(te,"\n( 10, %c )",ch);
            ch=getc(re);
        }
        else if(is11(ch))
        {
    
    
            fprintf(te,"\n( 11, %c )",ch);
            ch=getc(re);
        }
        else if(is12(ch))
        {
    
    
            fprintf(te,"\n( 12, %c )",ch);
            ch=getc(re);
        }

        else if(is13(ch))
        {
    
    
            fprintf(te,"\n( 13, %c )",ch);
            ch=getc(re);
        }
        else if(is14(ch))
        {
    
    
            fprintf(te,"\n( 14, %c )",ch);
            ch=getc(re);
        }
        else if(is15(ch))
        {
    
    
            fprintf(te,"\n( 15, %c )",ch);
            ch=getc(re);
        }

        else if(is16(ch))
        {
    
    
            ch=getc(re);
            if(is16(ch))
                fprintf(te,"\n( 16 , && )");
            else
            {
    
    
                fprintf(te,"\n( 19 , & )");
            }
            ch=getc(re);
        }
        else if(is17(ch))
        {
    
    
            ch=getc(re);
            if(is17(ch))
                fprintf(te,"\n( 17 , || )");
            ch=getc(re);
        }
        else if(is18(ch))
        {
    
    
            fprintf(te,"\n( 18, %c )",ch);
            ch=getc(re);
        }

        else
        {
    
    
            fprintf(te,"\n( error , %c )",ch);
            ch=getc(re);
        }


    }
    fclose(re);
    fclose(te);
    printf("----词法分析结果如下所示----\n");

    if((fg=fopen("D://MyOffice//code blocks//dem.txt","rb"))==NULL)
    {
    
    
        printf("Failure to open demo.txt!\n");
        return 0;
    }

    while((ch=fgetc(fg))!=EOF)
    {
    
    
        putchar(ch);
    }
    fclose(fg);
    return 0;
}

单词符号表
在这里插入图片描述
运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45784099/article/details/112976896