| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 751 人关注过本帖
标题:一个词法分析器的问题
取消只看楼主 加入收藏
zjmOO
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-4-13
收藏
 问题点数:0 回复次数:2 
一个词法分析器的问题
#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()函数!!!
搜索更多相关主题的帖子: 词法 分析器 
2008-04-14 16:51
zjmOO
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-4-13
收藏
得分:0 
没有人知道吗!!!
2008-04-15 12:44
zjmOO
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-4-13
收藏
得分:0 
[bo]以下是引用 [un]cobby[/un] 在 2008-4-15 14:07 的发言:[/bo]

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

额。。。这个也不算很长啊,各位哥哥姐姐,帮帮小弟啊!!!
2008-04-16 12:58
快速回复:一个词法分析器的问题
数据加载中...
 
   



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

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