C语言编写简单的词法分析器

刚学编译原理,老师让写的一个简单的词法分析器1.0,以后可能有改进

#include
#include
char yytext[256];
int line = 1,row = 0,rows = 0;

int IsLetter(char ch)
{
    if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
        return 1;
    else
        return 0;
}

int IsDigit(char ch)
{
    if(ch >= '0' && ch <='9')
        return 1;
    else
        return 0;
}

int KeyWord()
{
    char data[256];
    FILE *Key;
    Key = fopen("keyword.txt","r");
    if(Key == NULL)
    {
        printf("打开keyword文件失败!\n");
        return -1;
    }
    while(fscanf(Key,"%s ",data)!=EOF)
    {
        if(strcmp(yytext,data) == 0)
        {
            fclose(Key);
            return 1;
        }

    }
    fclose(Key);
    return 0;
}

int Scan(FILE * fp)
{
    char in;
    in = fgetc(fp);
    row ++;
    rows = row;
    while(in == ' ' || in == '\n')
    {
        if(in == '\n')
        {
            line ++;
            row = 0;
        }

        in = fgetc(fp);
        row ++;
        rows = row;
    }

    if(in == EOF)
        return 0;
    if(IsLetter(in) || in == '$')
    {
        int i = 0;
        yytext[i] = in;
        i++;
        in = fgetc(fp);
        row ++;
        while(IsLetter(in) || IsDigit(in) || in == '$')
        {
            yytext[i] = in;
            i ++;
            in = fgetc(fp);
            row ++;
        }
        yytext[i] = '\0';
        i ++;
        ungetc(in,fp);
        row --;
        if(KeyWord(yytext))
            return 1;
        else
            return 2;

    }

    else if(IsDigit(in))
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        in = fgetc(fp);
        row ++;
        while(IsDigit(in))
        {
            yytext[i] = in;
            i ++;
            in = fgetc(fp);
            row ++;
        }
        if(in == '.')
        {
            yytext[i] = in;
            i ++;
            in = fgetc(fp);
            row ++;
            while(IsDigit(in))
            {
                yytext[i] = in;
                i ++;
                in = fgetc(fp);
                row ++;
            }
            yytext[i] = '\0';
            i ++;
            ungetc(in,fp);
            row --;
            return 4;

        }
        yytext[i] = '\0';
        i ++;
        ungetc(in,fp);
        row --;
        return 3;
    }
    else if(in == '\"')
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        in = fgetc(fp);
        row ++;
        while(in != '\"')
        {
            if(in == '\n')
            {
                line ++;
                row = 0;
            }
            yytext[i] = in;
            in = fgetc(fp);
            row ++;
        }
        yytext[i] = '\0';
        i ++;
        ungetc(in,fp);
        row --;
        return 5;
    }
    else if(in == '\'')
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        in = fgetc(fp);
        row ++;
        yytext[i] = in;
        i ++;
        in = fgetc(fp);
        if(in != '\'')
            return -1;
        yytext[i] = in;
        i ++;
        return 6;
    }
    else if(in == '{' || in == '}' || in == '(' || in == ')' || in == ';' || in == ',')
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        return 7;
    }
    else if(in == '=')
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        in = fgetc(fp);
        row ++;
        if(in == '=' || in == '<' || in == '>')
        {
            yytext[i] = in;
            i ++;
            return 8;
        }
        if(in != '=')
        {
            ungetc(in,fp);
            row --;
            return 8;
        }
    }
    else if(in == '+' || in == '-' || in =='*' || in == '/')
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        return 8;
    }
    else
    {
        int i = 0;
        yytext[i] = in;
        i ++;
        return -1;
    }
    return 0;
}



int main()
{
    char c;
    int re,i;
    FILE *yyin;
    yyin = fopen("test.txt","r");
    if(yyin == NULL)
    {
        printf("打开文件失败,test.txt可能不存在\n");
        return 0;
    }
    while((c = fgetc(yyin)) != EOF)
    {
        ungetc(c,yyin);
        re = Scan(yyin);
        if(re != -1 && re != 0)
            printf("%s  类型:%d 位置:Line %2d  Row %2d\n",yytext,re,line,rows);
        else if (re == -1)
            printf("%s  Error\n",yytext);
        else
            printf("finished.\n");
        for(i = 0; yytext[i] != '\0'; i ++)
            yytext[i] = '\0';

    }
    fclose(yyin);
    return 0;
}

你可能感兴趣的:(c语言)