编译原理词法分析器c.docx

上传人:b****2 文档编号:18053744 上传时间:2023-08-07 格式:DOCX 页数:16 大小:19.93KB
下载 相关 举报
编译原理词法分析器c.docx_第1页
第1页 / 共16页
编译原理词法分析器c.docx_第2页
第2页 / 共16页
编译原理词法分析器c.docx_第3页
第3页 / 共16页
编译原理词法分析器c.docx_第4页
第4页 / 共16页
编译原理词法分析器c.docx_第5页
第5页 / 共16页
编译原理词法分析器c.docx_第6页
第6页 / 共16页
编译原理词法分析器c.docx_第7页
第7页 / 共16页
编译原理词法分析器c.docx_第8页
第8页 / 共16页
编译原理词法分析器c.docx_第9页
第9页 / 共16页
编译原理词法分析器c.docx_第10页
第10页 / 共16页
编译原理词法分析器c.docx_第11页
第11页 / 共16页
编译原理词法分析器c.docx_第12页
第12页 / 共16页
编译原理词法分析器c.docx_第13页
第13页 / 共16页
编译原理词法分析器c.docx_第14页
第14页 / 共16页
编译原理词法分析器c.docx_第15页
第15页 / 共16页
编译原理词法分析器c.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

编译原理词法分析器c.docx

《编译原理词法分析器c.docx》由会员分享,可在线阅读,更多相关《编译原理词法分析器c.docx(16页珍藏版)》请在冰点文库上搜索。

编译原理词法分析器c.docx

编译原理词法分析器c

编译原理词法分析器c++源程序

#include

#include

#include

#include

#include

#include

#include/*头文件*/

  voidinit();

     char*DchangeB(char*buf);

  intsearch(char*buf,inttype,intcommand);

     voidintdeal(char*buffer);

     voidchardeal(char*buffer);

  voiderrordeal(charerror,intlineno);

  voidscanner();

voidinit()

{     char*key[]={"","auto","break","case","char","const","continue","default","do","double",

                "else","enum","extern","float","for","goto","if","int","long","register",

                "return","short","signed","sizeof","static","struct","switch","typedef",

                "union","unsigned","void","volatile","while"};    /*C语言所有关键字/

   char*limit[]={"","(",")","[","]","->",".","!

","++","--","&","~",

                  "*","/","%","+","-","<<",">>","<","<=",">",">=","==","!

=","&&","||",

                 "=","+=","-=","*=","/=",",",";","{","}","#","_","''''"};/*运算、限界符*/

 fstreamoutfile;

   inti,j;

 char*c;

 outfile.open("key.txt",ios:

:

out);

 for(i=0;i<32;i++)

  outfile<

 outfile.close();

 outfile.open("Limit.txt",ios:

:

out);

 for(j=0;j<38;j++)

  outfile<

 c="";

 outfile<

 outfile.close();

 outfile.open("bsf.txt",ios:

:

out);

 outfile.close();

 outfile.open("cs.txt",ios:

:

out);

 outfile.close();

 outfile.open("output.txt",ios:

:

out);

 outfile.close();

}

char*DchangeB(char*buf)

{

      

   inttemp[20];

 char*binary;

 intvalue=0,i=0,j;

 for(i=0;buf[i]!

=''''\0'''';i++)

  value=value*10+(buf[i]-48);      /*将字符转化为十进制数*/

   if(value==0)

   {

  binary=newchar[2];

  binary[0]=''''0'''';

  binary[1]=''''\0'''';

  return(binary);

 }

 i=0;

 while(value!

=0)

   {

  temp[i++]=value%2;

  value/=2;

 }

 temp[i]=''''\0'''';

 binary=newchar[i+1];

 for(j=0;j<=i-1;j++)

      binary[j]=(char)(temp[i-j-1]+48);

   binary[i]=''''\0'''';

   return(binary); /*十进制转化为二进制*/

}

intsearch(char*buf,inttype,intcommand)

{intnumber=0;

   fstreamoutfile;

   charch;

   chartemp[30];

   inti=0;

   switch(type)

   {

 case1:

outfile.open("key.txt",ios:

:

in);break;

   case2:

outfile.open("bsf.txt",ios:

:

in);break;

   case3:

outfile.open("cs.txt",ios:

:

in);break;

   case4:

outfile.open("limit.txt",ios:

:

in);break;

   }

 outfile.get(ch);

 while(ch!

=EOF){

    while(ch!

=''''\n'''')

       {

           temp[i++]=ch;

           outfile.get(ch);

       }

       temp[i]=''''\0'''';

       i=0;

       number++;

       if(strcmp(temp,buf)==0)

       { 

     outfile.close();

   returnnumber;       /*若找到,返回在相应表中的序号*/

       }

       else

         outfile.get(ch);

 }                          //结束外层while循环

    if(command==1)

    {  

     outfile.close();

  return0;               /*找不到,当只需查表,返回0,否则还需造表*/

    

 }

switch(type)

   {

 case1:

outfile.open("key.txt",ios:

:

in);break;

   case2:

outfile.open("bsf.txt",ios:

:

in);break;

   case3:

outfile.open("cs.txt",ios:

:

in);break;

   case4:

outfile.open("limit.txt",ios:

:

in);break;

   }

outfile<

outfile.close();

 returnnumber+1;

}

voidintdeal(char*buffer){

 fstreamoutfile;

   intresult;

   result=search(buffer,1,1);          /*先查关键字表*/

   outfile.open("output.txt",ios:

:

app);

   if(result!

=0)

      outfile<

   else

   {

       result=search(buffer,2,2);      /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/

       outfile<

   }                                /*写入输出文件*/

   outfile.close();

}

voidchardeal(char*buffer)

{   fstreamoutfile;

   intresult;

   result=search(buffer,1,1);          /*先查关键字表*/

 outfile.open("output.txt",ios:

:

app);

   if(result!

=0)

      outfile<

   else

   {

       result=search(buffer,2,2);      /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/

      outfile<

   }                                /*写入输出文件*/

   outfile.close();

}

voiderrordeal(charerror,intlineno)

{ cout<<"\nerror:

"<

}

voidscanner()

{  fstream infile,outfile;

   charfilename[20];

   charch;

 interr=0;

   inti=0,line=1;

   intcount,result,errorno=0;

 chararray[30];

   char*word;

 printf("\npleaseinputthefilescannername:

");

   scanf("%s",filename);

   err=1;

   infile.open(filename,ios:

:

nocreate|ios:

:

in);

   while(!

infile)

   {

       cout<<"cannotopenfile"<

       printf("pleaseinput thefile nameagain:

\n");

  scanf("%s",filename);

       infile.open(filename,ios:

:

nocreate|ios:

:

in);

       err++;

  if(err==3)

  {cout<<"SORROYYOUCAN''''TVUEWTHEPRGARME\n";

       cout<<"TANKEYOUVIEW"<

  exit(0);}

   }

   infile.get(ch);

   while(ch!

=EOF)

   {                /*按字符依次扫描源程序,直至结束*/

       i=0;

       if(((ch>=''''A'''')&&(ch<=''''Z''''))||((ch>=''''a'''')&&(ch<=''''z''''))||(ch==''''_''''))

       {          /*以字母开头*/

           while(((ch>=''''A'''')&&(ch<=''''Z''''))||((ch>=''''a'''')&&(ch<=''''z''''))||(ch==''''_'''')||((ch>=''''0'''')&&(ch<=''''9'''')))

           {

               array[i++]=ch;

               infile.get(ch);

           }

           word=newchar[i+1];

   memcpy(word,array,i);

   word[i]=''''\0'''';

           intdeal(word);

           if(ch!

=EOF)

    infile.seekg(-1,ios:

:

cur);

       }

       elseif(ch>=''''0''''&&ch<=''''9'''')

       {         /*以数字开头*/

           while(ch>=''''0''''&&ch<=''''9'''')

           {

               array[i++]=ch;

               infile.get(ch);

           }

           word=newchar[i+1];

   memcpy(word,array,i);

   word[i]=''''\0'''';

           intdeal(word);

           if(ch!

=EOF)

    infile.seekg(-1,ios:

:

cur);

       }

       elseif((ch=='''''''')||(ch==''''\t''''))

   ;          /*消除空格符和水平制表符*/

  elseif(ch==''''\n'''')

   line++;          /*消除回车并记录行数*/

  elseif(ch==''''/'''')

       {                /*消除注释*/

   infile.get(ch);

   if(ch==''''='''')

   {             /*判断是否为‘/=’符号*/

    outfile.open("output.txt",ios:

:

noreplace|ios:

:

app);

    outfile<<"/=\t\t\t4\t\t\t32\n";

               outfile.close();

   }

   elseif(ch!

=''''*'''')

           {             /*若为除号,写入输出文件*/

               outfile.open("output.txt",ios:

:

noreplace|ios:

:

app);

    outfile<<"/\t\t\t4\t\t\t13\n";

               outfile.close();

    outfile.seekg(-1,ios:

:

cur);

   }

   elseif(ch==''''*'''')

   {             /*若为注释的开始,消除包含在里面的所有字符*/

    count=0;

   infile.get(ch);

    while(count!

=2)

               {         /*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/

     count=0;

     while(ch!

=''''*'''')

      infile.get(ch);

     count++;

     infile.get(ch);

     if(ch==''''/'''')

      count++;

     else

      infile.get(ch);

    }

   }

  }

  elseif(ch==''''"'''')

  {                /*消除包含在双引号中的字符串常量*/

   outfile.open("output.txt",ios:

:

noreplace|ios:

:

app);

     outfile<

   outfile.close();

   while(ch!

=''''"'''')

   infile.get(ch);

           infile<

   infile.close();

  }

  else

       {        /*首字符为其它字符,即运算限界符或非法字符*/

           array[0]=ch;

           infile.get(ch);      /*再读入下一个字符,判断是否为双字符运算、限界符*/

           if(ch!

=EOF)

           {          /*若该字符非文件结束符*/

               array[1]=ch;

    word=newchar[3];

    memcpy(word,array,2);

    word[2]=''''\0'''';

               result=search(word,4,1);     /*先检索是否为双字符运算、限界符*/

    if(result==0)

    {                          /*若不是*/

                   word=newchar[2];

     memcpy(word,array,1);

     word[1]=''''\0'''';

     result=search(word,4,1);     /*检索是否为单字符运算、限界符*/

     if(result==0)

                   {                          /*若还不是,则为非法字符*/

      errordeal(array[0],line);

      errorno++;

      infile.seekg(-1,ios:

:

cur);

                   }

                   else

     {    /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/

      outfile.open("output.txt",ios:

:

noreplace|ios:

:

app);

      outfile<

      outfile.close();

      infile.seekg(-1,ios:

:

cur);

     }

    }

               else

    {            /*若为双字符运算、限界符,写入输出文件*/

     outfile.open("output.txt",ios:

:

noreplace|ios:

:

app);

       outfile<

     outfile.close();

    }

           }

           else

   {              /*若读入的下一个字符为文件结束符*/

    word=newchar[2];

    memcpy(word,array,1);

    word[1]=''''\0'''';

    result=search(word,4,1);      /*只考虑是否为单字符运算、限界符*/

    if(result==0)               /*若不是,转出错处理*/

     errordeal(array[0],line);

    else

    {                           /*若是,写输出文件*/

     outfile.open("output.txt",ios:

:

noreplace|ios:

:

app);

       outfile<

     outfile.close();

    }

   }

       }

       infile.get(ch);

   }

   infile.close();

cout<<"\nThereare"<

}

voidmain()

{  charyn;

 

 do{

   init();/*初始化*/

   scanner();/*扫描源程序*/

 printf("AreYoucontinue(y/n)\n");//判断是否继续?

 yn=getch();

 }while(yn==''''y''''||yn==''''Y'''');

}

本人做的简单的词法分析器(编译原理作业)

                                      

这个是我在cfree3.5下编译后的文件

命令行下运行运行后有说明还有源代码

打包下载

#include

#include

#include

/*-----------------------------------

如花2005.10.1词法分析器

(很多没判断,

题目没提示所以不加比如说小数问题等)

简单的编译原理作业

*/

voidmain()

{

FILE*in,*out;

chararr[255];/*定义缓冲区*/

charch;

inti=0;

/*infile.txt是输入程序,结尾加@易于判断*/

if((in=fopen("infile.txt","r"))==NULL)

{

printf("cannotopenfile\n");

exit(0);

}

if((out=fopen("outfile.txt","w"))==NULL)

{

printf("cannoutopenoutfile\n");

exit

(1);

}

while(ch!

='@')/*文件尾加@作判断用*/

{

ch=fgetc(in);

while(ch=='')/

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2