一个小语言的词法分析程序原理及其实现(2)

类别:VC语言 点击:0 评论:0 推荐:

(接上篇)根据这个表来构造程序,程序的核心是下面的这个函数,

/********************************************************************

以下为主分析函数

从输入文件里面读,把分析结果写到输出文件中

参数:fpin :输入文件指针  fpout: 输出文件指针

********************************************************************/

void parse(FILE* fpin,FILE* fpout)

{

       char arr[MAXBUF];//读出的最长的字符串不超过MAXBUF,MAXBUF定义为255,够长了我想

int i=0;//分析含字母的字符串用

       int j=0;//分析纯数字的字符串用

    

 

       while(1)

       {

       fscanf(fpin,"%c",&ch);//从输入文件中读入一个字符

 

              if( ch==' '|| ch =='\t')//过滤掉空格和tab

                     ;

              else if( ch=='\n')//回车换行符,为下面进行错误判断

                     lineno++;

              else if( IsDigit(ch))//读入的是数字

              {

                     while(IsDigit(ch))

                     {

                             

                            arr[j] = ch;

                            j++;

                            fscanf(fpin,"%c",&ch);

                           

                     }

                    

                  fseek(fpin,-1L,SEEK_CUR);//文件指针后退一个字节

 

                     char* temp1 =(char*)malloc(j+1);/

                     memcpy(temp1,arr,j);

                     temp1[j] ='\0';//把数组里面的内容拷贝到连外一个数组里面,因为我定义的

                     //arr为255个字节,实际上写不到那么多,所以只拷贝实际上有数据的

 

                     j=0;//恢复初始状态,以备下次使用

                     fprintf(fpout,"%s\t\t%d\n",temp1,2);//常数

             

                     free(temp1);//释放内存

                 

                    

              }

 

              else if(IsAlpha(ch))//是字母开头的

              {

             

                     while(IsAlpha(ch) || IsDigit(ch))

                     {

                            arr[i] =ch;

                            i++;

                            fscanf(fpin,"%c",&ch);

                           

                     }

                     fseek(fpin,-1L,SEEK_CUR);

                    

                     char* temp = (char*)malloc(i+1) ;

                     memcpy(temp,arr,i);

                     temp[i] ='\0';

 

                     i=0;

                     /*基本思想同处理数字的*/

             

          if(FindOK(temp))//FindOK函数在关键字表中查找和temp字符串相同的,找到就返回类别编号

                {

                      

                      fprintf(fpout,"%s\t\t%d \n",temp,FindOK(temp));

                      

                }

                else

                {

                       fprintf(fpout,"%s\t\t%d\n",temp,1);//标示符号

                      

                }

                free(temp);

              }

      

              //以下为2字节的运算符号

              else if( ch==':')//符号“:=”

              {

                     fscanf(fpin,"%c",&ch);

                     if(ch=='=')

                            fprintf(fpout,"%s\t\t%d\n",":=",20);

                    

              else       

                     fprintf(fpout,"error in compileing %d lines unknown character %c \n",lineno,ch);//出错了

              }

 

              else if(ch=='>')//符号 “> “ 和”>=”

              { 

                     fscanf(fpin,"%c",&ch);

                     if(ch=='=')

                     fprintf(fpout,"%s\t\t%d\n",">=",16);

                     else

                     fprintf(fpout,">\t\t15\n");

              }

              else if( ch=='<') //符号 “< “ 和”<=”

 

              {

                     fscanf(fpin,"%c",&ch);

                     if(ch=='=')

                     {fprintf(fpout,"<=\t\t18\n");}

                     else if( ch=='>')

                     {fprintf(fpout,"<>\t\t19");}

                     else

                     {fprintf(fpout,"<\t\t19\n");}

              }

      

 

              else {

            //以下为一个字节的运算符号

              if(ch=='-') {fprintf(fpout,"%s\t\t%d\n",'-',10);continue;}//在文件中输出为“-   10”

              if(ch==';') {fprintf(fpout,";\t\t21\n");continue;}

              if(ch=='+') {fprintf(fpout,"+\t\t9\n");continue;}

              if(ch=='*') {fprintf(fpout,"*\t\t11\n");continue;}

              if(ch=='/') {fprintf(fpout,"/ \t\t12\n");continue;}

              if(ch=='(') {fprintf(fpout,"(\t\t13\n");continue;}

              if(ch==')') {fprintf(fpout,")\t\t14\n");continue;}

              if(ch=='.') {fprintf(fpout,".\t\t22\n");continue;}

              if(ch==',') {fprintf(fpout,",\t\t23\n");continue;}

              if(ch=='#') break;//分析结束

              else fprintf(fpout,"error in compiling %d lines unknown character %c \n",lineno,ch);//出错了,输出出错信息

              }

             

       }

      

 

}

以上代码在VISUAL C++6.0 +WIN2000下编译通过.

下面我们用该程序对下面的代码进行词法分析,该代码保存为test.txt,假设编译后的应用程序为lex.exe,

program test

 begin

       3j := 200 ;

if(j > 100) then      

  j := 300&&&&

else

  j := 107870

gyt768%^

 

end. #

在命令行下输入源文件名,test.txt.以及输出文件名:output.txt可以看到下面的结果.

 

program         6

test         1

begin              7

3            2

j             1

:=           20

200         2

;             21

if            3

(             13

j             1

>            15

100         2

)             14

then        4

j             1

:=           20

300         2

error in compiling 4 lines unknown character &

error in compiling 4 lines unknown character &

error in compiling 4 lines unknown character &

error in compiling 4 lines unknown character &

else         5

j             1

:=           20

107870           2

gyt768           1

error in compiling 7 lines unknown character %

error in compiling 7 lines unknown character ^

end         8

.             22

该函数是词法分析程序的核心,它可以识别出源程序中的编写错误,以及找出关键字,变量等.具体请看源代码,注释很详细,但是肯定有不足的地方,请大家不吝赐教。有什么问题,可以给我发邮件。。我的email:[email protected],

 

本文地址:http://com.8s8s.com/it/it1113.htm