当前位置: 代码迷 >> C语言 >> 一个词法分析器的问题
  详细解决方案

一个词法分析器的问题

热度:340   发布时间:2008-04-14 16:51:45.0
一个词法分析器的问题
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define N sizeof(struct keyword)
char token[20];
struct keyword
{
    char str[20];
    struct keyword *next;
};
struct keyword *creatkt(void)
{
    struct keyword *p1,*p2,*head;
    head=p1=p2=(struct keyword *)malloc(N);
    printf("请输入关键字:");
    scanf("%s",&p2->str);
    while(strcmp(p2->str,"end"))
    {
        p2=(struct keyword *)malloc(N);
        p1->next=p2;
        p1=p2;
        scanf("%s",&p2->str);
    }
    p2->next=NULL;
    return head;
}
int lookup(struct keyword *p)
{
    int flag=0;
    while(p)
    {
        if(strcmp(p->str,token))
        {
            p=p->next;
        }
        else
        {
            flag=1;
            p=NULL;
        }
    }
    return flag;
}
void scanner(FILE *fp1,FILE *fp2,struct keyword *p)
{
    int i,c;
    char ch;
    char s[20]={"标识符"},t[20]={"关键字"},u[20]={"数字"},v[20]={"符号"},w[20]={"出错"};
    ch=fgetc(fp1);
    if('a'<ch<'z'||'A'<ch<'Z')
    {
        
        token[0]=ch;
        ch=fgetc(fp1);
        i=1;
        while('a'<ch<'z'||'A'<ch<'Z'||'0'<ch<'9')
        {
            token[i]=ch;
            i++;
            ch=fgetc(fp1);
        }
        token[i]='\0';
        fseek(fp1,-1,1);
        c=lookup(p);
        if(c==0)
        {
                    fprintf(fp2,"%s,%s\n",s,token);
        }
        else
        {
            fprintf(fp2,"%s,%s\n",t,token);
        }
    }
    else if('0'<ch<'9')
        {
            token[0]=ch;
            ch=fgetc(fp1);
            i=1;
            while('0'<ch<'9')
            {
                token[i]=ch;
                i++;
                ch=fgetc(fp1);
            }
            token[i]='\0';
            fseek(fp1,-1,1);
            fprintf(fp2,"%s,%s\n",u,token);
        }
    else
            switch(ch)
        {
            case'<':ch=fgetc(fp1);
                if(ch=='=')
                {
                    fprintf(fp2,"%s,%s\n",v,token);
                }
                else if(ch=='>')
                {
                    fprintf(fp2,"%s,%s\n",v,token);
                }
                else
                {
                    fseek(fp1,-1,1);
                    fprintf(fp2,"%s,%s\n",v,token);
                }
                break;
            case'=':
                       fprintf(fp2,"%s,%s\n",v,token);
                    
                break;
            case'>':ch=fgetc(fp1);
                if(ch=='=')
                {
                    fprintf(fp2,"%s,%s\n",v,token);
                }
                else
                {
                    fseek(fp1,-1,1);
                    fprintf(fp2,"%s,%s\n",v,token);
                }
                break;
            default:
                 fprintf(fp2,"%s,%s\n",w,token);
                 break;
    }
    fclose(fp2);
}
void main()
{
    FILE *fp1,*fp2;
    struct keyword *p;
    p=(struct keyword *)malloc(N);
    p=creatkt();
    if((fp1=fopen("d:\\zjm1.txt","rt"))==NULL)
    {
        printf("\n不能打开此文件,按任意键退出!");
        getchar();
    }
    if((fp1=fopen("d:\\zjm.txt","wt"))==NULL)
    {
        printf("\n不能打开此文件,按任意键退出!");
        getchar();
    }
    while(!feof(fp1))
    scanner(fp1,fp2,p);
    fclose(fp1);
    fclose(fp2);
}

这是一个简单词法分析器的程序,想问问大家为什么编译时总提示错误进不了sanner()函数!!!
搜索更多相关的解决方案: 词法  分析器  

----------------解决方案--------------------------------------------------------
没有人知道吗!!!
----------------解决方案--------------------------------------------------------
就是知道也懒得看这么长的代码呵
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]cobby[/un] 在 2008-4-15 14:07 的发言:[/bo]

就是知道也懒得看这么长的代码呵

额。。。这个也不算很长啊,各位哥哥姐姐,帮帮小弟啊!!!
----------------解决方案--------------------------------------------------------
  相关解决方案