编译原理实验1-词法分析

#include
#include
#include
#define KEYWORDEND "waiting for your expanding"

typedef struct
{
    int typenum;
    char *word;
} WORD;

char input[255];
char token[255]="";
int pinput;
int ptoken;
char ch;
char *rwtab[]= {"begin","if","then","while","do","end",KEYWORDEND};

WORD *scaner();

int main()
{
    int over=1;
    WORD *oneword=new WORD;
    printf ("enter your words(end with #):");
    scanf("%[^#]s",input);
    pinput=0;
    printf ("your words:\n%s\n",input);
    while(over<1000 && over!=-1)
    {
        oneword=scaner();
        if (oneword->typenum<1000)
            printf ("(%d,%s)",oneword->typenum,oneword->word);
        over=oneword->typenum;
    }
    printf("\npress # to exit:");
    scanf("%[^#]s",input);
    return 0;
}

char mgetch()           //读取一个字符到ch
{
    ch=input[pinput];
    pinput=pinput+1;
    return(ch);
}

void getbc()            //去掉空白符号
{
    while(ch==' ' || ch==10)
    {
        ch=input[pinput];
        pinput=pinput+1;
    }
}

void concat()
{
    token[ptoken]=ch;
    ptoken=ptoken+1;
    token[ptoken]='\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],KEYWORDEND))
    {
        if(!strcmp(rwtab[i],token))
        {
            return i+1;
        }
        i=i+1;
    }
    return 10;
}

void retract()
{
    pinput=pinput-1;
}

char *dtb()
{
    return NULL;
}

WORD *scaner()
{
    WORD *myword=new WORD;
    myword->typenum=10;
    myword->word="";
    ptoken=0;
    mgetch();
    getbc();
    if (letter())
    {
        while(letter() || digit())
        {
            concat();
            mgetch();
        }
        retract();
        myword->typenum=reserve();
        myword->word=token;
        return(myword);
    }
    else if(digit())
    {
        while(digit())
        {
            concat();
            mgetch();
        }
        retract();
        myword->typenum=11;
        myword->word=token;
        return(myword);
    }
    else switch(ch)
        {
        case'=':
            mgetch();
            if (ch=='=')
            {
                myword->typenum=39;
                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':':
            mgetch();
            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'>':
            mgetch();
            if (ch=='=')
            {
                myword->typenum=37;
                myword->word=">=";
                return(myword);
            }
            retract();
            myword->typenum=35;
            myword->word=">";
            return(myword);
            break;
        case'<':
            mgetch();
            if (ch=='=')
            {
                myword->typenum=38;
                myword->word="<=";
                return(myword);
            }
            if (ch=='>')
            {
                myword->typenum=21;
                myword->word="<=";
                return(myword);
            }
            retract();
            myword->typenum=20;
            myword->word="<";
            return(myword);
            break;
        case'!':
            mgetch();
            if (ch=='=')
            {
                myword->typenum=40;
                myword->word="!=";
                return(myword);
            }
            retract();
            myword->typenum=-1;
            myword->word="ERROR";
            return(myword);
            break;
        case'\0':
            myword->typenum=1000;
            myword->word="OVER";
            return(myword);
            break;
        default:
            myword->typenum=-1;
            myword->word="ERROR";
            return(myword);
        }
}

你可能感兴趣的:(【课程学习】)