码迷,mamicode.com
首页 > 其他好文 > 详细

词法分析2

时间:2016-10-07 23:17:21      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>  
#include <string.h>
#define max 100  
char a[max],b[8],ch;  
int syn,p,m,n,sum;  
char word[][6]={"begin","if","then","while","do","end"};   
void mor()
{    
    sum=0;  
    ch=a[p++];  
    m=0;      
    while((ch== )||(ch==\n)||(ch==\t))  
        ch=a[p++];  
    if(((ch<=z)&&(ch>=a))||((ch<=Z)&&(ch>=A)))  
    {
        while(((ch<=z)&&(ch>=a))||((ch<=Z)&&(ch>=A))||((ch>=0)&&(ch<=9)))  
        {  
            b[m++]=ch;  
            ch=a[p++];  
        }    
        p--; 
        syn=10;                    
        for(n=0;n<6;n++)  
           if(strcmp(b,word[n])==0)   
           {
               syn=n+1;  
               break;  
           }  
    }                                   
    else if((ch>=0)&&(ch<=9))  
    {   
       while((ch>=0)&&(ch<=9))  
       {  
           sum=sum*10+ch-0;    
           ch=a[p++];  
       }  
       p--;  
       syn=11;  
    }  
    else                  
    {  
        switch(ch)  
        {  
        case +:  
            syn=13;  
            b[m++]=ch;          
            break;  
        case -:   
            syn=14;
            b[m++]=ch;   
            break; 
        case *:  
            syn=15;  
            b[m++]=ch;  
            break;  
        case /:  
            syn=16;  
            b[m++]=ch;  
            break;  
        case ::  
            b[m++]=ch;  
            ch=a[p++];  
            if(ch===)  
            {  
                syn=18;  
                b[m++]=ch;  
            }  
            else  
            {   
                syn=18;  
                p--;  
            }  
            break; 
        case <:
            b[m++]=ch;  
            ch=a[p++];  
            if(ch===)  
            {   
                syn=21;  
                b[m++]=ch;  
            }  
            else  
            {    
                syn=20;  
                p--;  
            }  
            break;  
        case >:  
            b[m++]=ch;  
            ch=a[p++];  
            if(ch===)  
            {  
                syn=24;  
                b[m++]=ch;  
            }  
            else  
            {   
                syn=23;  
                p--;  
            }  
            break;
        case =:  
            syn=25;  
            b[m++]=ch;  
            break;  
        case ;:  
            syn=26;  
            b[m++]=ch;  
            break;  
        case (:   
            syn=27;  
            b[m++]=ch;  
            break;  
        case ):  
            syn=28;  
            b[m++]=ch;  
            break;  
        case #:   
            syn=0;  
            b[m++]=ch;  
            break; 
        case !: 
            syn=-2;
            break;
        default:  
            syn=-1;  
            break;  
        }  
    }  
    b[m++]=\0;  
}
main()  
{
    p=0;  
    printf("请输入一段程序段,便于词法分析(以!作为结束符):"); 
    do{
        scanf("%c",&a[p++]);    
    }while(a[p-1]!=!);     
    p=0;  
    do{  
        mor();  
        switch(syn)  
        {  
            case 11:  
                printf("( %-5d%10d )\n",syn,sum);  
                break;  
            case -1:  
                printf("你输入一个错的字符\n");    
                return 0;  
                break;
            case -2:
                break;
            default:   
                printf("( %-5d%10s )\n",syn,b);  
                break;  
        }  
    }while(syn!=-2);  
}

 

词法分析2

标签:

原文地址:http://www.cnblogs.com/04JC/p/5936863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!