| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 462 人关注过本帖
标题:C词法分析,求一检索标识符函数,里边函数实现不了
只看楼主 加入收藏
shiyuanren
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-8-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
C词法分析,求一检索标识符函数,里边函数实现不了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char c,a[30],b[20];
int i=0,count0=0,count1=0,count2=0,count3=0,count5=0;
bool flag=true;
FILE *fp1,*fp2;
   char *key[]={" ","auto","bool","break","case","char","complex","const","continue","default","restrict","do","double",
                 "else","enum","extern","float","for","goto","if","imaginary","inline","int","long","register",
                 "return","short","signed","sizeof","static","struct","switch","typedef",
                 "union","unsigned","void","volatile","while","#define","#include"};     /*C语言所有关键字,共37个*/
    char *oper[]={" ","!","%","^","&","*","-","+","=","~","|",".",
                   "<",">","/","?",":","+=","-=","*=","/=","%=","<<==",">>==","|=","->","++",
                  "--","<<",">>","<=",">=","==","!=","&&","||"};/*运算符*/
    char *sep[]={" ","#","(",")","[","]","\'",";",":","\"","{","}",",","\\"," "};/*分隔符*/
/************************
    是否为保留字
************************/
int iskey(char *a){                                       
    for(i=1;i<40;i++)    
        if(strcmp(a,key[i])==0){
            fprintf(fp2,"0\t\t\t%d\t\t\t%s\n",i,a);
            return 1;
        }
        return 0;
}
/**************************
    是否为运算符
***************************/
void isoper(char *a){
    for(i=1;i<35;i++)
        if(strcmp(a,oper[i])==0){
            fprintf(fp2,"2\t\t\t%d\t\t\t%s\n",i,a);
        }
}
/************************
    是否为分隔符
************************/
void issep(char *a){                                       
    for(i=1;i<14;i++)
        if(strcmp(a,sep[i])==0){
        fprintf(fp2,"1\t\t\t%d\t\t\t%s\n",i,a);
        }    
}
/*是否为标识符*/
void issyb(char *a){
    FILE *fp0;    
    int k;
    k=strlen(a);
    if(((a[0]>='A')&&(a[0]<='Z'))||((a[0]>='a')&&(a[0]<='z'))||(a[0]=='_')){
        for(i=1;i<k-1;i++){
           if(((a[i]<48||a[i]>122)||(a[i]>57&&a[i]<65)||(a[i]>90&&a[i]<97))&&(a[i]!='_'))
               flag=false;
               continue;
        }
       if(flag==true){
               count5++;
               fp0=fopen("bzf.txt","a");
                fprintf(fp0,"%s\t\t\t3\t\t\t%d\n",a,count5);
                fclose(fp0);
                fprintf(fp2,"3\t\t\t%d\t\t\t%s\n",count5,a);
       }
    }
}
/**********************
 是否为整型或浮点型常量
***********************/
void isdigit(char *a){
    FILE *fp6;    
    if((a[0]>='0')&&(a[0]<='9')){
        for(i=0;i<100;i++){
            if(a[i]=='.')
                flag=false;
            continue;}
            if(flag==false){                                    /*浮点型常量输出*/
                fp6=fopen("cl.txt","a");
                count1++;
                fprintf(fp6,"%20s\t\t\t4\t1\t\t%d\n",a,count1);
                fclose(fp6);
                fprintf(fp2,"4\t\t\t1\t%d\t\t%s\n",count1,a);
            }
            else{                                            /*整型常量*/
                fp6=fopen("cl.txt","a");
                count0++;
                fprintf(fp6,"%20s\t\t\t4\t0\t\t%d\n",a,count0);
                fclose(fp6);
                fprintf(fp2,"4\t\t\t0\t%d\t\t%s\n",count0,a);
            }
        
        
    }
    
}
/***********************************
    字符、字符串常量判断
************************************/
void ischar(char *a){
    FILE *fp6;
    int k;
    k=strlen(a);
    if(((a[1]>='A')&&(a[1]<='Z'))||((a[1]>='a')&&(a[1]<='z'))||(a[1]=='_')){
        fp6=fopen("cl.txt","a");
        if((a[0]=='\'')&&(a[2]=='\'')){                              /*是字符常量*/
                
                count2++;
             fprintf(fp6,"%20s\t\t\t4\t2\t\t%d\n",a,count2);
                
             fprintf(fp2,"4\t\t\t2\t%d\t\t%s\n",count2,a);
      }
        else
            if((a[0]=='\"')&&(a[k-1]=='\"')){                         /*是字符串常量*/  
                
                count3++;
             fprintf(fp6,"%20s\t\t\t4\t3\t\t%d\n",a,count3);
            
             fprintf(fp2,"4\t\t\t3\t%d\t\t%s\n",count3,a);
        }
        fclose(fp6);
    }
}
/******************************
     分割单词
******************************/
void cut(){
    int j;
    while(!feof(fp1)){
        do{
            a[i++]=fgetc(fp1);
        }while(a[i-1]!='\0'&&a[i-1]!='\n'&&a[i-1]!=' '&&a[i-1]!=EOF&&a[i-1]!='+'&&a[i-1]!='-'&&a[i-1]!='*'&&a[i-1]!='/'
            &&a[i-1]!='%'&&a[i-1]!='='&&a[i-1]!='('&&a[i-1]!=')'&&a[i-1]!='{'&&a[i-1]!='}'&&a[i-1]!='>'&&a[i-1]!='<'&&
            a[i-1]!='|'&&a[i-1]!='&'&&a[i-1]!=';'&&a[i-1]!=','&&a[i-1]!='['&&a[i-1]!=']');
        if(a[i-1]=='\0'|| a[i-1]=='\n'||a[i-1]==' '|| a[i-1]==EOF){
           a[i-1]=0;
           j=iskey(a);
           isoper(a);
           issep(a);
           if(j==0){
             issyb(a);
           }
           j=0;
           isdigit(a);
           ischar(a);
        }
        if(a[i-1]=='+'||a[i-1]=='-'||a[i-1]=='*'||a[i-1]=='/'
            ||a[i-1]=='%'||a[i-1]=='='||a[i-1]=='('||a[i-1]==')'||a[i-1]=='{'||a[i-1]=='}'||a[i-1]=='>'||a[i-1]=='<'
            ||a[i-1]=='|'||a[i-1]=='&'||a[i-1]==';'||a[i-1]==','||a[i-1]=='['||a[i-1]==']') {    
               b[0]=a[i-1];
                
               a[i-1]=0;
               j=iskey(a);
               isoper(a);
               issep(a);
               if(j==0){
                  issyb(a);
               }
               j=0;
               isdigit(a);
               ischar(a);
               if(b[0]=='='||b[0]=='+'||b[0]=='-'||b[0]=='>'||b[0]=='<'||b[0]=='|'||b[0]=='&'){
                  b[1]=fgetc(fp1);
                  if(b[1]=='='||b[1]=='+'||b[1]=='-'||b[1]=='<'||b[1]=='>'||b[1]=='|'||b[1]=='&'){
                     b[2]=0;
                     isoper(b);
                  }
                  else{
                   fseek(fp1,-1,1);
                   b[1]=0;
                   isoper(b);
                  }
               }
               else{
                   b[1]=0;
                   issep(b);
               }

        }
        if(a[i]=='\0'||a[i]=='\n'||a[i]==' '||a[i-1]!=EOF||a[i-1]=='+'||a[i-1]=='-'||a[i-1]=='*'||a[i-1]=='/'
            ||a[i-1]=='%'||a[i-1]=='='||a[i-1]=='('||a[i-1]==')'||a[i-1]=='{'||a[i-1]=='}'||a[i-1]=='>'||a[i-1]=='<'
            ||a[i-1]=='|'||a[i-1]=='&'||a[i-1]==';'||a[i-1]==','||a[i-1]=='['||a[i-1]==']')                   /*有问题*/
            i=0;
        for(j=0;j<=i-1;j++)
            a[j]=0;
    }
}
/*各表初始化程序*/
void init()
{            
   
    FILE *fp;
    int i;
    char c;
    fp=fopen("blz.txt","w");        /*初始化保留字表*/
    for(i=1;i<=39;i++){
        if(i<=9)
          fprintf(fp,"%-20s 0  0%d\n",key[i],i);
        else
          fprintf(fp,"%-20s 0  %2d\n",key[i],i);
    }
    fclose(fp);               
    fp=fopen("fgf.txt","w");        /*初始化限制符表*/
    for(i=1;i<=13;i++){
        if(i<=9)
         fprintf(fp,"%-20s  1  0%d\n",sep[i],i);
        else
          fprintf(fp,"%-20s  1  %2d\n",sep[i],i);
    }
    fclose(fp);               
    fp=fopen("ysf.txt","w");        /*初始化运算符表*/
    for(i=1;i<=35;i++){
        if(i<=9)
          fprintf(fp,"%-20s 2  0%d\n",oper[i],i);
        else
          fprintf(fp,"%-20s 2  %2d\n",oper[i],i);
    }
    fclose(fp);               
      
    c='"';
    fprintf(fp,"%c\n",c);
    fclose(fp);              
    fp=fopen("bzf.txt","w");       /*初始化标识符表*/
    fclose(fp);              
    fp=fopen("cl.txt","w");       /*初始化常数表*/
    fclose(fp);               
    fp=fopen("output.txt","w");  /*初始化输出文件*/
    fclose(fp);              
}
void main()
{     
     init();
     fp1=fopen("input.txt","r");
     fp2=fopen("output.txt","w");
     cut();
     fclose(fp1);
     fclose(fp2);
}

[ 本帖最后由 shiyuanren 于 2009-8-17 18:19 编辑 ]
搜索更多相关主题的帖子: c词法分析 
2009-08-17 12:13
UserYuH
Rank: 12Rank: 12Rank: 12
来 自:毅华
等 级:火箭侠
威 望:8
帖 子:720
专家分:3300
注 册:2009-8-10
收藏
得分:7 
有时自己编的程序有点长了,自己的头都有点麻木了,出了点错,还得把思路从头想一遍,
程序再从头一步步跟踪,要花不少时间,不是自己编的程序看了那更头大,花的时间会更多,
你能写了这么长的程序应该可以跟踪找出错误了,多花点时间自己找吧,等找出错误相信你收获会更多。

努力—前进—变老—退休—入土
2009-08-17 15:40
CCVC果冻爽
Rank: 4
等 级:业余侠客
帖 子:116
专家分:209
注 册:2009-7-31
收藏
得分:7 
基本就错在bool flag=true这一句了。
2009-08-17 16:33
shiyuanren
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-8-16
收藏
得分:0 
回复 3楼 CCVC果冻爽

运行时目标文件会丢一两个词
2009-08-17 17:31
快速回复:C词法分析,求一检索标识符函数,里边函数实现不了
数据加载中...
 
   



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

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