文法

#include<stdio.h>
#define max 200
int i,n,syn;

char pro[max],lin[20];
char *word[6]={"begin","if","then","while","do","end"};
char ch;
scaner()
{
    int j=0;
    for(n=0;n<20;n++)
        lin[n]=NULL;
        ch=pro[i++];
        while(ch==' ')
            ch=pro[i++];
        if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z')||(ch>=48&&ch<=57))
        {
           if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z'))
           {
               lin[j++]=ch;
               ch=pro[i++];
               while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='Z')||(ch>=48&&ch<=57))
               {
                   lin[j++]=ch;
               ch=pro[i++];
               }
                lin[j]='\0';
                i--;
                syn=10;
                for(n=0;n<6;n++)
                    if(strcmp(lin,word[n])==0)
                {
                    syn=n+1;
                    break;
                }
           }
           else
           {
               while(ch>=48&&ch<=57)
               {
                   lin[j++]=ch;
                   ch=pro[i++];
               }
               i--;
               lin[j]='\0';
               syn=11;
           }
        }
        else
        {
            if(ch=='<')
                {lin[j++]=ch;
            ch=pro[i++];
            if (ch=='>')
                {
                    syn=21;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else if(ch=='=')
                {
                    syn=22;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else
                {
                    syn=20;
            lin[j]='\0';
            i--;
            }
                }
            else if(ch=='>')
                { lin[j++]=ch;
            ch=pro[i++];
            if (ch=='=')
                {
                    syn=24;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else
                {
                    syn=23;
            lin[j]='\0';
            i--;
            }
            }
            else if(ch==':')
                {
                    lin[j++]=ch;
            ch=pro[i++];
            if (ch=='=')
                {
                    syn=18;
            lin[j++]=ch;
            lin[j]='\0';}
            else
                {
                    syn=17;
            lin[j]='\0';
            i--;
            }
            }
            else if(ch=='+')
                {syn=13;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else if(ch=='-')
                {
                    syn=14;
            lin[j++]=ch;
            lin[j]='\0';}
            else if(ch=='*')
                {
                    syn=15;
            lin[j++]=ch;
            lin[j]='\0';}
            else if(ch=='/')
                {
                    syn=16;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else if(ch=='=')
                {
                    syn=25;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else if(ch==';')
                {
                    syn=26;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else if(ch=='(')
                        {
                            syn=27;
            lin[j++]=ch;
            lin[j]='\0';
            }
            else if(ch==')')
                {
                    syn=28;
            lin[j++]=ch;
            lin[j]='\0';}
            else if(ch=='#')
            {
                syn=0;
                lin[j++]=ch;
                lin[j]='\0';
            }
            else
                    syn=-1;
            }
                    return syn;
        }

void main()
{
    int i=0 ;
    printf("**************************************************************************\n");
    printf("*********************************种别编码*********************************\n");
    printf("**************************************************************************\n");
    printf("(1,begin)       (2,if)                 (3,then)         (4,while)    (5,do)\n");
    printf("(6,end)         (10,基本标示符)        (11,数字)        (13, )       (14,-)\n");
    printf("(15,*)          (16,/)                 (17,:)           (18,:=)      (20,<)\n");
    printf("(21,<>)         (22,<=)                (23,>)           (24,>=)      (25,=)\n");
    printf("(26,;)          (27,()                 (28,))           (0,#)\n");
    read();
    printf( "读入的字符串如下: \n\n" );
    puts(pro);
    printf( "词法分析结果如下:\n" );
    printf( "\n" );
    do   {
            scaner();
    switch(syn)
    {
        case 11:
            printf ("(%d,%s)\n",syn,lin);
            break;
            case -1:
                printf("错误符号\n");
                break;
                default:
                    printf ("(%d,%s)",syn,lin); }
                    } while (syn!=0);
                    printf( "\n词法分析结束\n" );
                     scanf("%c",&i);
}
posted on 2016-10-29 10:51  244谭颖欣  阅读(101)  评论(0编辑  收藏  举报