标题:自己做的词法分析器,求斧正。
只看楼主
Rexfield
Rank: 6Rank: 6
来 自:幻想乡
等 级:侠之大者
威 望:1
帖 子:240
专家分:484
注 册:2010-7-28
结帖率:100%
已结贴  问题点数:20 回复次数:4 
自己做的词法分析器,求斧正。
程序代码:
// --- main.c ---
#include "main.h"

void testlex()
{
    char wds[256],ps,type;
    FILE *fp=fopen("test.lx","rt");
    while(feof(fp)==0)
    {
        type=lex(fp,wds);
        printf("[%c]\t%s\n",type,wds);
    }
}
int main()
{
    #ifdef debug
    testlex();
    #endif
    return 0;
}

// --- Lex.c ---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Lex.h"
#include "Public.h"

char TrueLex(FILE *fp,char *words)
{
    memset(words,'\0',256);
    pt=fgetc(fp);
    pts[0]=pt;
    if(pt==32||pt==9)           // 跳过空白
    {
        while(feof(fp)!=TRUE&&(pt==32||pt==9))
            pt=fgetc(fp);
        ungetc(pt,fp);      // <-
        return isSpa;
    }else if(isalpha(pt)!=FALSE||pt=='_')       // 识别单词
    {
        while(feof(fp)!=TRUE&&(isalnum(pt)!=FALSE||pt=='_'))
        {
            strcat(words,pts);
            pt=fgetc(fp);
            pts[0]=pt;
        }
        ungetc(pt,fp);      // <-
        return isKey;
    }else if(isOperat(pts)==TRUE)        // 识别运算符
    {
        while(feof(fp)==FALSE&&isOperat(pts)==TRUE)
        {
            strcat(words,pts);
            pt=fgetc(fp);
            pts[0]=pt;
        }
        ungetc(pt,fp);      // <-
        return isCal;
    }else if(pt=='\'')      // 识别字符
    {
        pt=fgetc(fp);
        if(fgetc(fp)!='\'')return SynErr;
        sprintf(words,"\'%c\'",pt);
        return isChr;
    }else if(pt=='\"')      // 识别字符串
    {
        do{
            pts[0]=pt;
            strcat(words,pts);
            pt=fgetc(fp);
            if(pt==-1)return SynErr;
        }while(pt!='\"');
        strcat(words,"\"");
        return isStr;
    }else if(pt=='\n')      // 识别换行符
    {
        return isEnt;
    }else if(isdigit(pt)!=0)    // 识别数值
    {
        int isFloat=FALSE;
        while(isdigit(pt)!=0)
        {
            strcat(words,pts);
            pt=fgetc(fp);
            if(pt=='.')         // 浮点
            {
                if(isFloat==FALSE)
                {
                    isFloat=TRUE;
                    strcat(words,".");
                    pt=fgetc(fp);
                }
                else
                    return SynErr;
            }
            pts[0]=pt;
        }
        if(strstr("BbOoDdHh",pts)!=NULL)    // 这是一个进制描述符
        {
            if(isFloat==TRUE)       // 进制描述符和浮点共存
                strcpy(words,strtok(strdup(words),"."));
            if(tolower(pt)=='b')
                return isBin;
            else if(tolower(pt)=='o')
                return isOct;
            else if(tolower(pt)=='d')
                return isDec;
            else if(tolower(pt)=='h')
                return isHex;
        }
        ungetc(pt,fp);      // <-
        if(isFloat==TRUE)
            return isFlo;
        else
            return isInt;
    }else if(isLimit(pt)==TRUE)     // 识别定界符
    {
        strcpy(words,pts);
        return isLim;
    }
    return SynErr;
}

int isLimit(int wd)
{
    if(wd=='('||wd==')'||
        wd=='{'||wd=='}'||
        wd=='['||wd==']'||
        wd==';'||wd==','
        )
        return TRUE;
    else
        return FALSE;
}

int isOperat(char *wds)
{
    if(strcmp(wds,"+")==0||strcmp(wds,"-")==0||strcmp(wds,"*")==0||
       strcmp(wds,"/")==0||strcmp(wds,"%")==0||strcmp(wds,"!")==0||
       strcmp(wds,"<")==0||strcmp(wds,">")==0||strcmp(wds,".")==0||
       strcmp(wds,"=")==0||strcmp(wds,"==")==0||strcmp(wds,"<=")==0||
       strcmp(wds,">=")==0||strcmp(wds,"!=")==0||
       strcmp(wds,"||")==0||strcmp(wds,"&&")==0||strcmp(wds,"++")==0||
       strcmp(wds,"--")==0||strcmp(wds,"->")==0
       )
       return TRUE;
    else
        return FALSE;
}

char lex(FILE *fp,char *words)
{
    type=TrueLex(fp,words);
    if(fgetc(fp)!=-1)
        fseek(fp,-1,SEEK_CUR);
    return type;
}

// --- Lex.h ---

char pt,pts[]={0,0},type;

char TrueLex(FILE *fp,char *words);
int isLimit(int wd);
int isOperat(char *wds);
char lex(FILE *fp,char *words);

// --- Public.h ---

#define TRUE 1
#define FALSE 0

#define SynErr '\0'
#define unknow '\1'

#define isStr 'S'
#define isInt 'I'
#define isFlo 'F'
#define isChr 'C'
#define isKey 'K'
#define isSpa ' '
#define isLim 'L'
#define isCal 'A'
#define isEnt 'n'

#define isDec 'D'
#define isBin 'B'
#define isOct 'O'
#define isHex 'H'
/*

 * 带进制描述符的值只能是Dec类型的

 * 如果描述符和浮点共存,那么舍去浮点数的小数部分

 */
#define debug

// --- main.h ---

#include <stdio.h>
#include <stdlib.h>
#include "Public.h"

extern char lex(FILE *fp,char *words);

自己做的词法分析器,求斧正。
搜索更多相关主题的帖子: 词法 分析器 斧正 
2010-10-03 15:39
Rexfield
Rank: 6Rank: 6
来 自:幻想乡
等 级:侠之大者
威 望:1
帖 子:240
专家分:484
注 册:2010-7-28
得分:0 
程序代码:
// --- test.lx ---
a>=b
c="abc"
d='a'
e=123.2
g=111
h=1010100b
x=D2A4H

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
2010-10-03 15:40
longlong89
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:广州
等 级:小飞侠
威 望:6
帖 子:1043
专家分:2754
注 册:2009-8-18
得分:10 
学习了。

想象力征服世界
2010-10-04 22:22
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
得分:10 
想象力征服世界

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-10-04 22:26
mzg773579598
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-12-18
得分:0 
很棒啊
2010-12-18 21:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321918-1-1.html




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

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