| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:求一词法分析器程序啊,希望有注释的,顺便给下面这个程序注释一下啊,也是 ...
只看楼主 加入收藏
网络编程
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-10-19
结帖率:50%
收藏
已结贴  问题点数:5 回复次数:2 
求一词法分析器程序啊,希望有注释的,顺便给下面这个程序注释一下啊,也是词法分析器的
这是C语言写的此分析器,希望大家给我注释一下啊,谢谢了,呵呵,这个程序要求用户输入时只能输入一行,谁可以把它改成可以输入多行的啊
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int scan(char s[]);
int IsKeyword(char s[]);
int LookUp(char s[]);
int to10(char s[],int n);
#define IDN 0
#define IF 1
#define THEN 2
#define ELSE 3
#define WHILE 4
#define DO 5
#define INT10 6
#define INT8 7
#define INT16 8
#define ADD 9
#define SUB 10
#define MUL 11
#define DIV 12
#define EQ 13
#define GT 14
#define LT 15
#define LP 16
#define RP 17
#define SEMI 18
#define ASG 19
char * Keywords[]={"if","then","else","while","do"};
char MyIDN[100][512];
int symbol;
int id;
char token[1024];
char ch;
char str[1024];
int attr;
int sta;
void main(){
    int i;
    id=0;
    sta=0;
    attr=0;
    printf("\nInput the string:\t");
    gets(str);
    for(i=0;str[i]!='\0';i++){
        symbol=scan(str);
        switch(symbol){
            case IDN:    printf("\nIDN\t%s",&MyIDN[attr]);    break;
            case IF:           printf("\nIF\t_");    break;
            case THEN:         printf("\nTHEN\t_");    break;
            case ELSE:      printf("\nELSE\t_");    break;
            case WHILE:     printf("\nWHILE\t_");    break;
            case DO:    printf("\nDO\t_");       break;
            case INT10:       printf("\nINT10\t%d",attr);    break;
            case INT8:      printf("\nINT8\t%d",attr);    break;
            case INT16:       printf("\nINT16\t%d",attr);    break;
            case ADD:          printf("\nADD\t_"); break;
            case SUB:    printf("\nSUB\t_"); break;
            case MUL:    printf("\nMUL\t_"); break;
            case DIV:    printf("\nDIV\t_"); break;
            case EQ:    printf("\nEQ\t_"); break;
            case GT:    printf("\nGT\t_"); break;
            case LT:    printf("\nLT\t_"); break;
            case LP:    printf("\nLP\t_"); break;
            case RP:    printf("\nRP\t_"); break;
            case SEMI:    printf("\nSEMI\t_"); break;
            case ASG:    printf("\nASG\t_"); break;
            case 111:    break;
            default:    printf("\nInput error~!\n");    break;
        }
        memset(token,0,32);
       }
}
int scan(char s[]){
    int i=0;
    while(s[sta]==' ')    sta++;
    for(;s[sta]!='\0';sta++){
        if(isdigit(s[sta])){
            if((s[sta]-'0')==0){/////
                if((s[sta+1])=='x'){    //
                    token[i++]=s[sta++];
                    token[i++]=s[sta++];
                    if(!(isdigit(s[sta]))&(s[sta]<'a'||s[sta]>'f')){
                        sta--;
                        token[0]='0';
                        return INT10;
                    }
                    else{
                        while(isdigit(s[sta])||(s[sta]>='a'&&s[sta]<='f'))
                        token[i++]=s[sta++];
                        attr=to10(token,16);
                        return INT16;
                    }
                }
                else{
                    if((s[sta+1]>='0')&(s[sta+1]<='7')){//
                        token[i++]=s[sta++];
                        while(isdigit(s[sta]))    token[i++]=s[sta++];
                        attr=to10(token,8);
                        return INT8;
                    }
                    else{
                        token[0]='0';
                        sta++;
                        attr=to10(token,10);
                        return INT10;
                    }
                }
            }
            else{/////
                token[i++]=s[sta++];
                while(isdigit(s[sta]))    token[i++]=s[sta++];
                attr=to10(token,10);
                return INT10;
            }
        }
        if(isalpha(s[sta])){
            token[i++]=s[sta++];



            while(isalpha(s[sta])||isdigit(s[sta]))    token[i++]=s[sta++];
            switch(IsKeyword(token)){
                case 0:    return IF;
                case 1: return THEN;
                case 2:    return ELSE;
                case 3:    return WHILE;
                case 4:    return DO;
                case -1:
                    if((attr=LookUp(token))>=0)    return IDN;
                default:
                    printf("\nInput Error~!");     break;
            }
        }
        if(s[sta]=='+'){    sta++;    return ADD;}
        if(s[sta]=='-'){        sta++;    return SUB;}
        if(s[sta]=='*'){sta++;    return MUL;}
        if(s[sta]=='/'){sta++;    return DIV;}
        if(s[sta]=='='){sta++;    return EQ; }
        if(s[sta]=='>'){sta++;    return GT; }
        if(s[sta]=='<'){sta++;    return LT; }
        if(s[sta]=='('){sta++;    return LP; }
        if(s[sta]==')'){sta++;    return RP; }
        if(s[sta]==';'){sta++;    return SEMI;}
        if(s[sta]==':'){
            if(s[sta+1]=='='){  sta++;return ASG;    }
            else    printf("\nInput Error~!");
        }
    }
    return 111;
}

int IsKeyword(char s[]){
    int i;
    for(i=0;i<5;i++)
        if(!strcmp(s,Keywords[i]))        return i;
    return -1;
}

int LookUp(char s[]){
    int i=0;
    for(i=0;i<=id;i++)
        if(!strcmp(s,MyIDN[i]))        return i;
    strcpy(MyIDN[id++],s);
    return id-1;
}
int to10(char s[],int n){
    int i,no=0;
    switch(n){
        case 10:    for(i=0;s[i]!='\0';i++)    no=no*10+(s[i]-'0');
                return no;
        case 8:        for(i=0;s[i]!='\0';i++)    no=no*8+(s[i]-'0');
                return no;
        case 16:    for(i=2;s[i]!='\0';i++){
                    if(isdigit(s[i]))        no=no*16+(s[i]-'0');
                    else    no=no*16+(s[i]-'a'+10);
                }
                return no;
        default:    break;
    }
    return 0;
}
搜索更多相关主题的帖子: 注释 分析器 词法 
2009-10-25 18:02
ping燕s
Rank: 2
来 自:江西
等 级:论坛游民
帖 子:40
专家分:33
注 册:2009-9-22
收藏
得分:2 
    ===占个位置  等答案~~~
2009-10-26 15:28
Kid_X
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:216
专家分:515
注 册:2007-10-8
收藏
得分:2 
用lex做!
2009-10-27 13:46
快速回复:求一词法分析器程序啊,希望有注释的,顺便给下面这个程序注释一下啊, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035847 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved