| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 403 人关注过本帖
标题:词法分析器。求大神帮忙修改
只看楼主 加入收藏
higher、
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-26
收藏
 问题点数:0 回复次数:0 
词法分析器。求大神帮忙修改
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define _KEY_WORD_END "waiting for your expanding"        
char input[100];                    //输入缓冲区
char token[100]="";                    //单词缓冲区
int p_input;                        //输入缓冲区指针
int p_token;                        //单词缓冲区指针
char ch;                            //当前读入的字符
char * key[10]={"begin","end","if","then","else","while","write","read","do", "until"};

char m_getch()                    //从缓冲区读入一个字符到ch
{
    ch = input[p_input];
    p_input=p_input+1;;
    return(ch);
}

void getbc()                    //去掉空白符号
{
    while(ch==' '||ch=='\t'||ch=='\n')
    {
        ch=input[p_input];
        p_input=p_input+1;
    }
}

void concat()                    //拼接单词
{
    token[p_input]=ch;
    p_token=p_token+1;
    token[p_token]='\0';
}

int reserve()
{         //关键字判断
    int i=0;
    while(strcmp(key[i],_KEY_WORD_END))
    {
        if(!strcmp(key[i],token))
        {
            return i+1;
        }
        i=i+1;
    }return 10;
}

int letter() {        //判断是否为字母
    if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
        return 1;
    else
        return 0;
}

int digit(){          //判断是否为数字
    if(ch>='0'&&ch<='9') return 1;
    else return 0;
}

scaner()
{
p_token=0;
m_getch=();
getbc();
if(letter())
{
    while(letter||digit())
    {
        concat();
        m_getch();
    }
}
else if (digit())
{
    while(digit())
    {
    concat();
    m_getch();
    }
}
else switch(ch)
       {         
               case'+':printf("+是运算符\n");break;
               case'-' :printf("-是运算符\n");break;
               case'*' :printf("*是运算符\n");break;
               case'=' :printf("=是运算符\n");break;
               case'/' :printf("/是运算符\n");break;
               case'(' :printf("(是届符\n");break;
               case')' :printf(")是届符\n");break;
               case'[' :printf("[是届符\n");break;
               case']' :printf("]是届符\n");break;            
               case';' :printf(";是届符\n");break;
               case'.' :printf(".是届符\n");break;
               case',' :printf(",是届符\n");break;
               case'{' :printf("{是届符\n");break;
               case'}' :printf("}是届符\n");break;
               case':' :{
                        ch=fgetc(in);
                        if(ch=='=') printf(":=是运算符");
                        else
                        {
                            printf("=是运算符");
                            fseek(in,-1L,1);
                        }
                        }break;
                case'>' :{ch=fgetc(in);
                         if(ch=='=') printf(">=是运算符");;
                         if(ch=='>')printf(">>是输入控制符");
                         else {
                            printf(">=是运算符");
                            fseek(in,-1L,1);
                         }
                         }break;
               case'<' :{ch=fgetc(in);
                         if(ch=='=') printf("<=是运算符");;
                         else if(ch=='<') printf("<<是输出控制符");
                         else if(ch=='>') printf("<>是运算符");
                         else{
                             printf("<是运算符");
                            fseek(in,-1L,1);
                         }
                        }break;
              default : printf("无法识别字符");
}
void main()
{
    FILE * in ,* out;
    p_input=0;                           
    char ch;                                    //存入当前读入的字符
    char infile[10],outfile[10];                //存放两个文件名
    printf("请输入读入文件的名字:");
    scanf("%s",infile);
    printf("请输入输出文件的名字:");
    scanf("%s",outfile);
    if ((in=fopen(infile,"r")) == NULL)            //打开输入文件
    {
        printf("无法打开此文件\n");
        exit(0);
    }
    if ((out=fopen(outfile,"w"))== NULL)        //打开输出文件
    {
        printf("无法打开此文件\n");
        exit(0);
    }
    while(!feof(in))                            //如果没有遇到输入文件的结束标志
    {
        int i;
        for(i=0;i<100;i++)
        {
            fscanf(in,%c,&input[i]);
        }
    }
    scaner();
    printf("********************分析如下*********************\n");
    fclose(in);
    fclose(out);
    printf("按任意键结束\n");
}
搜索更多相关主题的帖子: waiting include 缓冲区 write 单词 
2014-05-26 11:34
快速回复:词法分析器。求大神帮忙修改
数据加载中...
 
   



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

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