回 帖 发 新 帖 刷新版面

主题:词法分析器的核心代码,绝大可行!

analyse(FILE *fpin, FILE *fpout)
{
    char arr[MAXBUF];
    int i=0;
    int j=0;
    
    while((ch=fgetc(fpin))!=EOF)          //读入字符判断,空格、字母、数字、界符
    {
        if(ch==' '||ch=='\t')
        {
            
        }
        else if(ch=='\n')                //如果是换行符,则行号加1
        {  
            lineno++;
        }
        else if(isdigit(ch))            //如果是数字
        { 
            while(isdigit(ch))            //判断和读取数字
            { 
                arr[j]=ch;
                j++;
                ch=fgetc(fpin);
            } 
            arr[j]='\0';
            j=0;
            fseek(fpin,-1L,SEEK_CUR);
            fprintf(fpout,"%s\t%d\n",arr,2) ;
        }
        else if (isalpha(ch))        //如果是字母
        {
            while(isalpha(ch)||isdigit(ch))
            {
                arr[j]=ch;
                j++;
                ch=fgetc(fpin);
            }
            fseek(fpin,-1L,SEEK_CUR);
            arr[j]='\0';
            j=0;
            if (search(arr))    //如果是关键字
            {
                fprintf(fpout,"%s\t%d\n",arr,search
(arr));
            }
            else  
                fprintf(fpout,"%s\t%d\n",arr,1);    //普通标志符
        }
        }

回复列表 (共4个回复)

沙发

else if(ch==':')  
        {
            ch=fgetc(fpin);
            if(ch=='=') 
            {
                fprintf(fpout,"%s\t%d\n",":=",29);    //如果是 :=
            }
            else
            {
                fprintf(fpout,"%s\t%d\n",":",30);   //如果是 :
                fseek(fpin,-1L,SEEK_CUR);
            }
        }
        else if (ch=='>')
        {
            ch=fgetc(fpin);
            if(ch=='=')    //如果是 >=
            {
                fprintf(fpout,"%s\t%d\n",">=",32);
            }
            else
            {
                fprintf(fpout,"%s\t%d\n",">",31);  //如果是 >
                fseek(fpin,-1L,SEEK_CUR);
            }
        }
        else if(ch=='<')
        { 
            ch=fgetc(fpin);
            if(ch=='>')
            {
                fprintf(fpout,"%s\t%d\n","<>",35);  // 如果是 <>
            }
            else if(ch=='=')
            {
                fprintf(fpout,"%s\t%d\n","<=",34);   //如果是 <=
            }
            else 
            {
                fprintf(fpout,"%s\t%d\n","<",33);   //如果是 <
                fseek(fpin,-1L,SEEK_CUR);
            }
        }
        else if(ch=='/')
        { 
            ch=fgetc(fpin);
            if(ch=='*')
            {
                ch=fgetc(fpin);

板凳

s:
                while(ch!='*')
                {
                    ch=fgetc(fpin);
                }
                while(ch=='*')
                {
                    ch=fgetc(fpin);
                    while(ch!='/')
                    {
                        goto s;            //如果是注释 /*  */
                    }
                }
            }
            else if(ch=='/')
            {
                ch=fgetc(fpin);
                while(ch!='\n')
                {
                    ch=fgetc(fpin);      //如果是注释 //
                }
            }
            else 
            {  
                fprintf(fpout,"%s\t%d\n","/",24);
                fseek(fpin,-1L,SEEK_CUR);
            }
        }
        else if(ch=='+')
        {
            fprintf(fpout,"%s\t%d\n","+",21);
        }
        else if(ch=='-')
        {
            fprintf(fpout,"%s\t%d\n","-",22);
        }
        else if(ch=='*')
        {
            fprintf(fpout,"%s\t%d\n","*",23);
        }
        else if(ch=='(')
        {
            fprintf(fpout,"%s\t%d\n","(",25);
        }
        else if(ch==')')
        {
            fprintf(fpout,"%s\t%d\n",")",26);
        }
        else if(ch=='[')
        {
            fprintf(fpout,"%s\t%d\n","[",27);
        }
        else if(ch==']')
        {
            fprintf(fpout,"%s\t%d\n","]",28);
        }
        else if(ch=='.')
        {
            fprintf(fpout,"%s\t%d\n",".",39);
        }
        else if(ch==';')
        {
            fprintf(fpout,"%s\t%d\n",";",36);
        }
        else if(ch=='=')
        {
            fprintf(fpout,"%s\t%d\n","=",38);
        }
        else if(ch==',')
        {
            fprintf(fpout,"%s\t%d\n",",",40);
        }
        else fprintf(fpout,"无法识别的字符 %c\n",ch)  ;
   }

3 楼


HAO

4 楼

[quote]
HAO[/quote]
[em3]

我来回复

您尚未登录,请登录后再回复。点此登录或注册