首页 > 代码库 > 2016.9.30词法分析程序

2016.9.30词法分析程序

技术分享

#include <stdio.h>                 #include <string.h>               #include <conio.h>                #include <ctype.h>                 char prog[80]={\0},token[8];                    char ch;int syn,n,sum,m,p;                         char *rwtab[6]={"begin","if","then","while","do","end"};void scaner(){    m=0;    sum=0;    for(n=0;n<8;n++)        token[n]=\0;    ch=prog[p++];    while(ch== )        ch=prog[p++];    if(isalpha(ch))       {        while(isalpha(ch)||isdigit(ch))            {           token[m++]=ch;           ch=prog[p++];        }           token[m++]=\0;           ch=prog[p--];           syn=10;           for(n=0;n<6;n++)            if(strcmp(token,rwtab[n])==0)               {                syn=n+1;                break;            }             }            else        if(isdigit(ch))            {            while(isdigit(ch))                {            sum=sum*10+ch-0;            ch=prog[p++];            }            ch=prog[p--];            syn=11;        }        else            switch(ch)        {                case<:m=0;token[m++]=ch;ch=prog[p++];                        if(ch==>)                        {                            syn=21;                            token[m++]=ch;                        }                        else if(ch===)                        {                            syn=22;                            token[m++]=ch;                        }                            else                            {                                 syn=20;                                 ch=prog[p--];                            }                        break;                case>:m=0;token[m++]=ch;ch=prog[p++];                        if(ch===)                        {                            syn=24;                            token[m++]=ch;                        }                        else                        {                            syn=23;                            ch=prog[p--];                        }                        break;             case::m=0;token[m++]=ch;ch=prog[p++];                     if(ch===)                     {                         syn=18;                         token[m++]=ch;                     }                     else                     {                         syn=17;                         ch=prog[p--];                     }                     break;             case+:syn=13;token[0]=ch;break;             case-:syn=14;token[0]=ch;break;             case*:syn=15;token[0]=ch;break;             case/:syn=16;token[0]=ch;break;             case=:syn=25;token[0]=ch;break;             case;:syn=26;token[0]=ch;break;             case(:syn=27;token[0]=ch;break;             case):syn=28;token[0]=ch;break;             case#:syn=0;token[0]=ch;break;             default:syn=-1;}}main(){     p=0;    printf("\n请输入需要分析的字符串,以#表示结束:");    do     {           ch=getchar();           prog[p++]=ch;     }while(ch!=#);   p=0;   do   {        scaner();        switch(syn)        {            case 11: printf("(%d,%d)\n",syn,sum);break;            case -1: printf("\n 出错;\n");break;            default: printf("(%d,%s)\n",syn,token);        }    }while(syn!=0);    getch();}

技术分享

2016.9.30词法分析程序