0916編譯原理第二次作業

53韓麒麟發表於2015-09-21
#include<stdio.h>
#include<string.h>
#define max 100
int n=0,i=0;
char token[max];
char a;
char word[max];

void fuhao();
void shuzi();



main()
{
    char *rwtab[6]={"begin","if","then","while","do","end"};
    int p=0,j=0,k=0,m=0;
    int key=0;
    printf("請輸入源程式(以#鍵結束):");
    do{
        scanf("%c",&a);
        word[p]=a;
        p++;
    }while(a!='#');

    //關鍵字的判斷    
    
    while(word[i]!='#')
    {    
    while((word[i]>='a'&&word[i]<='z')||(word[i]>='A'&&word[i]<='Z'))
    {
        if((word[i]>='a'&&word[i]<='z')||(word[i]>='A'&&word[i]<='Z'))
        {
              token[n]=word[i];
            n++;
            token[n]='\0';
            i++;            
        }

    for(k=0;k<6;k++)
    {
        key=0;
        if(strcmp(rwtab[k],token)==0)
        {
            printf("%s   %d\n",rwtab[k],k+1);
            key=1;
            break;
        }
    }    }
    if(key==0)
    {
            printf("%s   10\n",token);
    }
    key=1;
    i=i+1;
    n=0;
    }
    fuhao();
    shuzi();
}
//特殊符號的判斷
void fuhao()
{
    int p=0,sign=0;
    for(p=0;p<max;p++)
    {
        switch(word[p])
        {
        case '+':
            printf("+    13\n");
            break;
        case '-':
            printf("-    14\n");
            break;
        case '*':
            printf("*    15\n");
            break;
        case '/':
            printf("/    16\n");
            break;
        case ':':
            if(word[p+1]=='=')
            {
                printf(":=   18\n");
                p++;
            }
            else
                printf(":    17\n");
            break;
        case '<':
            if(word[p+1]=='=')
            {
                printf("<=     21\n");
                sign=1;
                p++;
            }
            else if(word[p+1]=='>')
            {
                printf("<>     22\n");
                sign=1;
                p++;
            }
            else if(sign==0)
                printf("<    20\n");
            break;
        case '>':
            if(word[p+1]=='=')
            {
                printf(">=    24\n");
                p++;
            }
            else
                printf(">    23\n");
            break;
        case '=':
            printf("=    25\n");
            break;
        case ';':
            printf(";    26\n");
            break;
        case '(':
            printf("(    27\n");
            break;
        case ')':
            printf(")    28\n");
            break;
        case '#':
            printf("#    29\n");
            break;
        }
    }
}
//數字的判斷
void shuzi()
{
    int p=0,j=0,t=0,key=0;
    char Shu[100];
    while(word[p]!='#')
    {
        key=0;
    while(word[p]>='0'&&word[p]<='9')
    {
        Shu[j]=word[p];
        p++;
        j++;
        key=1;
    }
    if(key==1)
    {
        for(t=0;t<j;t++)
        {
            printf("%c",Shu[t]);
        }
        printf("    11\n");
        j=0;
    }
    p++;
    }

}

 

相關文章