码迷,mamicode.com
首页 > 其他好文 > 详细

编译原理(第三版)词法分析器

时间:2019-04-26 13:30:13      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ror   NPU   typedef   get   exp   put   第三版   amp   word   

/*
    begin x:=9; if x > 0 then x:= 2*x+1/3; end #
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _KEY_WORD_END "waiting for your expanding"
typedef struct
{
    int typenum;
    char *word;
}WORD;
char input[255];
char token[255]="";
int p_input;
int p_token;
char ch;
char* rwtab[] = {"begin","if","then","while","do","end",_KEY_WORD_END};
WORD * scaner();

int main()
{
    int over = 1;
    WORD* oneword = new WORD;
    printf("Enter Your words(end with #):");
    scanf("%[^#]s",input);
    p_input = 0;
    printf("Your words:\n%s\n",input);
    while(over < 1000 &&over !=-1)
    {
        oneword = scaner();
        if(oneword->typenum <1000)
            printf("(%d,%s)\n",oneword->typenum,oneword->word);
        over = oneword->typenum;
    }
    printf("\npress # to exit:");
    scanf("%[^#]s",input);
    return 0;
}
char m_getch()
{
    ch = input[p_input];
    p_input = p_input+1;
    return(ch);
}
void getbc()
{
    while(ch ==  ||ch == 10)
    {
        ch = input[p_input];
        p_input++;
    }
}
void concat()
{
    token[p_token] = ch;
    p_token++;
    token[p_token] = \0;
}
int letter()
{
    if((ch >= a&& ch <= z)|| (ch >=A&&ch <= Z))
        return 1;
    else
        return 0;
}
int digit()
{
    if(ch >= 0&&ch <=9)return 1;
    else return 0;
}
int reserve()
{
    int i = 0;
    while(strcmp(rwtab[i],_KEY_WORD_END))
    {
        if(!strcmp(rwtab[i],token))
            return i+1;
        i++;
    }
    return 10;
}
void retract()
{
    p_input--;
}

char* dtb()
{
    return NULL;
}

WORD* scaner()
{

    WORD* myword = new WORD;
    myword->typenum = 10;
    myword->word = "";
    p_token = 0;
    m_getch();
    getbc();
    if(letter())
    {
        while(letter()||digit())
        {
            concat();
            m_getch();
        }
        retract();
        myword->typenum = reserve();
        myword->word = token;
        return(myword);
    }
    else if(digit())
    {
        while(digit())
        {
            concat();
            m_getch();
        }
        retract();
        myword->typenum = 11;
        myword->word = token;
        return(myword);
    }
    else switch(ch)
    {
        case=:m_getch();
            if(ch == =)
            {
                myword->typenum = 25;
                myword->word = "==";
                return(myword);
            }
            retract();
            myword->typenum = 21;
            myword->word = "=";
            return(myword);
            break;
        case+:
            myword->typenum = 13;
            myword->word = "+";
            return(myword);
            break;
        case-:
            myword->typenum = 14;
            myword->word = "-";
            return(myword);
            break;
        case*:
            myword->typenum = 15;
            myword->word = "*";
            return(myword);
            break;
        case/:
            myword->typenum = 16;
            myword->word = "/";
            return(myword);
            break;
        case(:
            myword->typenum = 27;
            myword->word = "(";
            return(myword);
            break;
        case):
            myword->typenum = 28;
            myword->word = ")";
            return(myword);
            break;
        case[:
            myword->typenum = 28;
            myword->word = "[";
            return(myword);
            break;
        case]:
            myword->typenum = 29;
            myword->word = "]";
            return(myword);
            break;
        case{:
            myword->typenum = 30;
            myword->word = "{";
            return(myword);
            break;
        case}:
            myword->typenum = 31;
            myword->word = "}";
            return(myword);
            break;
        case,:
            myword->typenum = 32;
            myword->word = ",";
            return(myword);
            break;
        case::m_getch();
            if(ch == =)
            {
                myword->typenum = 18;
                myword->word = ":=";
                return(myword);
            }
            retract();
            myword->typenum = 17;
            myword->word = ":";
            return(myword);
            break;
        case;:
            myword->typenum = 26;
            myword->word = ";";
            return(myword);
            break;
        case>:m_getch();
            if(ch == =)
            {
                myword->typenum = 24;
                myword->word = ">=";
                return(myword);
            }
            retract();
            myword->typenum = 23;
            myword->word = ">";
            return(myword);
            break;
        case<:m_getch();
            if(ch == =)
            {
                myword->typenum = 22;
                myword->word = "<=";
                return(myword);
            }
            else if(ch == >)
            {
                myword->typenum = 21;
                myword->word = "<>";
                return(myword);
            }
            retract();
            myword->typenum = 20;
            myword->word = "<";
            return(myword);
            break;
        case!:m_getch();
            if(ch == =)
            {
                myword->typenum = 40;
                myword->word = "!=";
                return(myword);
            }
            retract();
            myword->typenum = -1;
            myword->word = "ERROR";
            return(myword);
            break;
        case#:
            myword->typenum = 0;
            myword->word = "#";
            return(myword);
            break;
        case\0:
            myword->typenum = 1000;
            myword->word = "OVER";
            return(myword);
            break;
        default: myword->typenum = -1;
            myword->word = "ERROR";
            return(myword);
    }
}

 

编译原理(第三版)词法分析器

标签:ror   NPU   typedef   get   exp   put   第三版   amp   word   

原文地址:https://www.cnblogs.com/--lr/p/10773428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!