词法分析

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char prog[800],token[8];
char ch;
int syn,p,q,sum;
char *rwtab[6]={"begin","if","then","while","do","end"};
void scaner();
main()
{
    p=0;
    printf("\nplease input the source code:\n");
    do{
        ch=getchar();
        prog[p]=ch;
        p++;
    }while(ch!='#');
    p=0;
    do
    {
        scaner();
        switch(syn)
        {
            case 11:printf("\n(%d,%d)",syn,sum);break;
            case -1:printf("\n(%s,出错)",token);break;
            default:printf("\n(%d,%s)",syn,token);
        }
    }while(syn!=0);
}


void scaner()
{

    int i;
    sum=0;
    for(q=0;q<8;q++)
        token[q]=NULL;
    ch=prog[p];
    q=0;
    while(ch==' '||ch=='\n')
    {
        p++;
        ch=prog[p];    
    }
    if(('a'<=ch)&&(ch<='z')||('A'<=ch)&&(ch<='Z'))
    {
        while(('a'<=ch)&&(ch<='z')||('A'<=ch)&&(ch<='Z')||(ch>='0'&&ch<='9'))
        {
            token[q]=ch;
            p++;
            ch=prog[p];    
            
            q++;
        }
        syn=10;
        for(i=0;i<6;i++)
        {
             if(strcmp(token,rwtab[i])==0)
             {
                 syn=i+1;
                 break;
             }    
        }
    }
    else if(ch>='0'&&ch<='9')
    {
        while((ch>='0')&&(ch<='9'))
        {
            sum=sum*10+ch-'0';
            p++;
            ch=prog[p];
        }
        syn=11;
    }
    else switch(ch)
    {
        case '+':
             syn=13;
             token[q++]=ch;
             p++;
             break;
        case '-':
            syn=14;
             token[q++]=ch;
             p++;
             break;
        case '*':
            syn=15;
             token[q++]=ch;
             p++;
             break;
        case '/':
            syn=16;
             token[q++]=ch;
             p++;
             break;
        case ':':
            syn=17;
             token[q++]=ch;
             p++;
             if(prog[p]=='=')
             {
                 syn=18;
                 ch=prog[p];
                 token[q++]=ch;
                 p++;
             }
             break;
        case '<':
            syn=20;
             token[q++]=ch;
             p++;
             if(prog[p]=='>')
             {
                 syn=21;
                 ch=prog[p];
                 token[q++]=ch;
                 p++;
             }
             if(prog[p]=='=')
             {
                 syn=22;
                 ch=prog[p];
                 token[q++]=ch;
                 p++;
             }
             break;
        case '>':
            syn=23;
             token[q++]=ch;
             p++;
              if(prog[p]=='=')
             {
                 syn=24;
                 ch=prog[p];
                 token[q++]=ch;
                 p++;
             }
             break;
        case '=':
            syn=25;
             token[q++]=ch;
             p++;
             break;
        case ';':
            syn=26;
             token[q++]=ch;
             p++;
             break;
        case '(':
            syn=27;
             token[q++]=ch;
             p++;
             break;
        case ')':
            syn=28;
             token[q++]=ch;
             p++;
             break;
          case '#':
             syn=0;
             token[q++]=ch;
             break;
    }
}

 

 
 

 

posted on 2015-09-21 19:16  Glp_Moliny  阅读(228)  评论(0编辑  收藏  举报

导航