编译原理词法分析器(C源代码)

  1. #include<iostream.h> 
  2. #include<fstream.h> 
  3. #include<stdlib.h> 
  4. #include<stdio.h> 
  5. #include<string.h> 
  6. #include<conio.h> 
  7. #include<process.h> /*头文件*/ 
  8. void init(); 
  9. char *DchangeB(char *buf); 
  10. int search(char *buf,int type,int command); 
  11. void intdeal(char *buffer); 
  12. void chardeal(char *buffer); 
  13. void errordeal(char error,int lineno); 
  14. void scanner(); 
  15. void init() 
  16. {
  17.     char *key[]=
  18.     {
  19.         "","auto","break","case","char","const","continue","default","do","double"
  20.             "else","enum","extern","float","for","goto","if","int","long","register"
  21.             "return","short","signed","sizeof","static","struct","switch","typedef"
  22.             "union","unsigned","void","volatile","while"
  23.     }; 
  24.     /*C语言所有关键字/ 
  25.     char *limit[]={" ","(",")","[","]","->",".","!","++","--","&","~", 
  26.     "*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||", 
  27.     "=","+=","-=","*=","/=",",",";","{","}","#","_","'"};/*运算、限界符*/ 
  28.     fstream outfile; 
  29.     int i,j; 
  30.     char *c; 
  31.     outfile.open("key.txt",ios::out); 
  32.     for(i=0;i<32;i++) 
  33.         outfile<<key[i]<<endl; 
  34.     outfile.close(); 
  35.     outfile.open("Limit.txt",ios::out); 
  36.     for(j=0;j<38;j++) 
  37.         outfile<<limit[j]<<endl; 
  38.     c=""
  39.     outfile<<c; 
  40.     outfile.close(); 
  41.     outfile.open("bsf.txt",ios::out); 
  42.     outfile.close(); 
  43.     outfile.open("cs.txt",ios::out); 
  44.     outfile.close(); 
  45.     outfile.open("output.txt",ios::out); 
  46.     outfile.close(); 
  47. char *DchangeB(char *buf) 
  48. {
  49.     int temp[20]; 
  50.     char *binary; 
  51.     int value=0,i=0,j; 
  52.     for(i=0;buf[i]!='/0';i++) 
  53.         value=value*10+(buf[i]-48); /*将字符转化为十进制数*/ 
  54.     if(value==0) 
  55.     { 
  56.         binary=new char[2]; 
  57.         binary[0]='0'
  58.         binary[1]='/0'
  59.         return(binary); 
  60.     } 
  61.     i=0; 
  62.     while(value!=0) 
  63.     { 
  64.         temp[i++]=value%2; 
  65.         value/=2; 
  66.     } 
  67.     temp[i]='/0'
  68.     binary=new char[i+1]; 
  69.     for(j=0;j<=i-1;j++) 
  70.         binary[j]=(char)(temp[i-j-1]+48); 
  71.     binary[i]='/0'
  72.     return(binary); /*十进制转化为二进制*/ 
  73. int search(char *buf,int type,int command) 
  74. {
  75.     int number=0; 
  76.     fstream outfile; 
  77.     char ch; 
  78.     char temp[30]; 
  79.     int i=0; 
  80.     switch(type) 
  81.     { 
  82.     case 1: outfile.open("key.txt",ios::in);break
  83.     case 2: outfile.open("bsf.txt",ios::in);break
  84.     case 3: outfile.open("cs.txt",ios::in);break
  85.     case 4: outfile.open("limit.txt",ios::in);break
  86.     } 
  87.     outfile.get(ch); 
  88.     while(ch!=EOF){ 
  89.         while(ch!='/n'
  90.         { 
  91.             temp[i++]=ch; 
  92.             outfile.get(ch); 
  93.         } 
  94.         temp[i]='/0'
  95.         i=0; 
  96.         number++; 
  97.         if(strcmp(temp,buf)==0) 
  98.         { 
  99.             outfile.close(); 
  100.             return number; /*若找到,返回在相应表中的序号*/ 
  101.         } 
  102.         else 
  103.             outfile.get(ch); 
  104.     } //结束外层while循环 
  105.     if(command==1) 
  106.     { 
  107.         outfile.close( ); 
  108.         return 0; /*找不到,当只需查表,返回0,否则还需造表*/ 
  109.     } 
  110.     switch(type) 
  111.     { 
  112.     case 1: outfile.open("key.txt",ios::in);break
  113.     case 2: outfile.open("bsf.txt",ios::in);break
  114.     case 3: outfile.open("cs.txt",ios::in);break
  115.     case 4: outfile.open("limit.txt",ios::in);break
  116.     } 
  117.     outfile<<buf; 
  118.     outfile.close(); 
  119.     return number+1; 
  120. void intdeal(char *buffer)
  121. {
  122.     fstream outfile; 
  123.     int result; 
  124.     result=search(buffer,1,1); /*先查关键字表*/ 
  125.     outfile.open("output.txt",ios::app); 
  126.     if(result!=0) 
  127.         outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/ 
  128.     else 
  129.     { 
  130.         result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/ 
  131.         outfile<<buffer<<result<<endl; 
  132.     } /*写入输出文件*/ 
  133.     outfile.close(); 
  134. void chardeal(char *buffer) 
  135.     fstream outfile; 
  136.     int result; 
  137.     result=search(buffer,1,1); /*先查关键字表*/ 
  138.     outfile.open("output.txt",ios::app); 
  139.     if(result!=0) 
  140.         outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/ 
  141.     else 
  142.     { 
  143.         result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/ 
  144.         outfile<<buffer<<result<<endl; 
  145.     } /*写入输出文件*/ 
  146.     outfile.close(); 
  147. void errordeal(char error,int lineno) 
  148.     cout<<"/nerror: "<<error<<" ,line"<<lineno; 
  149. void scanner() 
  150.     fstream infile,outfile; 
  151.     char filename[20]; 
  152.     char ch; 
  153.     int err=0; 
  154.     int i=0,line=1; 
  155.     int count,result,errorno=0; 
  156.     char array[30]; 
  157.     char *word; 
  158.     printf("/n please input the file scanner name:"); 
  159.     scanf("%s",filename); 
  160.     err=1; 
  161.     infile.open(filename,ios::nocreate|ios::in); 
  162.     while(! infile) 
  163.     { 
  164.         cout<<"cannot open file"<<endl; 
  165.         printf("please input the file name again:/n"); 
  166.         scanf("%s",filename); 
  167.         infile.open(filename,ios::nocreate|ios::in); 
  168.         err++; 
  169.         if(err==3) 
  170.         {cout<<"SORROY YOU CAN'T VUEW THE PRGARME/n"
  171.         cout<<"TANKE YOU VIEW"<<endl; 
  172.         exit(0);} 
  173.     } 
  174.     infile.get(ch); 
  175.     while(ch!=EOF) 
  176.     { /*按字符依次扫描源程序,直至结束*/ 
  177.         i=0; 
  178.         if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')) 
  179.         { /*以字母开头*/ 
  180.             while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9'))) 
  181.             { 
  182.                 array[i++]=ch; 
  183.                 infile.get(ch); 
  184.             } 
  185.             word=new char[i+1]; 
  186.             memcpy(word,array,i); 
  187.             word[i]='/0'
  188.             intdeal(word); 
  189.             if(ch!=EOF) 
  190.                 infile.seekg(-1,ios::cur); 
  191.         } 
  192.         else if(ch>='0'&&ch<='9'
  193.         { /*以数字开头*/ 
  194.             while(ch>='0'&&ch<='9'
  195.             { 
  196.                 array[i++]=ch; 
  197.                 infile.get(ch); 
  198.             } 
  199.             word=new char[i+1]; 
  200.             memcpy(word,array,i); 
  201.             word[i]='/0'
  202.             intdeal(word); 
  203.             if(ch!=EOF) 
  204.                 infile.seekg(-1,ios::cur); 
  205.         } 
  206.         else if((ch==' ')||(ch=='/t')) 
  207.             ; /*消除空格符和水平制表符*/ 
  208.         else if(ch=='/n'
  209.             line++; /*消除回车并记录行数*/ 
  210.         else if(ch=='/'
  211.         { /*消除注释*/ 
  212.             infile.get(ch); 
  213.             if(ch=='='
  214.             { /*判断是否为‘/=’符号*/ 
  215.                 outfile.open("output.txt",ios::noreplace|ios::app); 
  216.                 outfile<<"/=/t/t/t4/t/t/t32/n"
  217.                 outfile.close(); 
  218.             } 
  219.             else if(ch!='*'
  220.             { /*若为除号,写入输出文件*/ 
  221.                 outfile.open("output.txt",ios::noreplace|ios::app); 
  222.                 outfile<<"//t/t/t4/t/t/t13/n"
  223.                 outfile.close(); 
  224.                 outfile.seekg(-1,ios::cur); 
  225.             } 
  226.             else if(ch=='*'
  227.             { /*若为注释的开始,消除包含在里面的所有字符*/ 
  228.                 count=0; 
  229.                 infile.get(ch); 
  230.                 while(count!=2) 
  231.                 { /*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/ 
  232.                     count=0; 
  233.                     while(ch!='*'
  234.                         infile.get(ch); 
  235.                     count++; 
  236.                     infile.get(ch); 
  237.                     if(ch=='/'
  238.                         count++; 
  239.                     else 
  240.                         infile.get(ch); 
  241.                 } 
  242.             } 
  243.         } 
  244.         else if(ch=='"'
  245.         { /*消除包含在双引号中的字符串常量*/ 
  246.             outfile.open("output.txt",ios::noreplace|ios::app); 
  247.             outfile<<ch<<"/t/t/t4/t/t/t37/n"
  248.             outfile.close(); 
  249.             while(ch!='"'
  250.                 infile.get(ch); 
  251.             infile<<ch<<"/t/t/t4/t/t/t37/n"
  252.             infile.close(); 
  253.         } 
  254.         else 
  255.         { /*首字符为其它字符,即运算限界符或非法字符*/ 
  256.             array[0]=ch; 
  257.             infile.get(ch); /*再读入下一个字符,判断是否为双字符运算、限界符*/ 
  258.             if(ch!=EOF) 
  259.             { /*若该字符非文件结束符*/ 
  260.                 array[1]=ch; 
  261.                 word=new char[3]; 
  262.                 memcpy(word,array,2); 
  263.                 word[2]='/0'
  264.                 result=search(word,4,1); /*先检索是否为双字符运算、限界符*/ 
  265.                 if(result==0) 
  266.                 { /*若不是*/ 
  267.                     word=new char[2]; 
  268.                     memcpy(word,array,1); 
  269.                     word[1]='/0'
  270.                     result=search(word,4,1); /*检索是否为单字符运算、限界符*/ 
  271.                     if(result==0) 
  272.                     { /*若还不是,则为非法字符*/ 
  273.                         errordeal(array[0],line); 
  274.                         errorno++; 
  275.                         infile.seekg(-1,ios::cur); 
  276.                     } 
  277.                     else 
  278.                     { /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/ 
  279.                         outfile.open("output.txt",ios::noreplace|ios::app); 
  280.                         outfile<<word<<"/t/t/t4/t/t/t"<<result<<"/t"<<endl; 
  281.                         outfile.close(); 
  282.                         infile.seekg(-1,ios::cur); 
  283.                     } 
  284.                 } 
  285.                 else 
  286.                 { /*若为双字符运算、限界符,写入输出文件*/ 
  287.                     outfile.open("output.txt",ios::noreplace|ios::app); 
  288.                     outfile<<word<<"/t/t/t4/t/t/t"<<result<<endl;; 
  289.                     outfile.close( ); 
  290.                 } 
  291.             } 
  292.             else 
  293.             { /*若读入的下一个字符为文件结束符*/ 
  294.                 word=new char[2]; 
  295.                 memcpy(word,array,1); 
  296.                 word[1]='/0'
  297.                 result=search(word,4,1); /*只考虑是否为单字符运算、限界符*/ 
  298.                 if(result==0) /*若不是,转出错处理*/ 
  299.                     errordeal(array[0],line); 
  300.                 else 
  301.                 { /*若是,写输出文件*/ 
  302.                     outfile.open("output.txt",ios::noreplace|ios::app); 
  303.                     outfile<<word<<"/t/t/t4/t/t/t"<<result<<"/t"<<endl; 
  304.                     outfile.close(); 
  305.                 } 
  306.             } 
  307.         } 
  308.         infile.get(ch); 
  309.     } 
  310.     infile.close(); 
  311.     cout<<"/nThere are "<<errorno<<" error(s)./n"/*报告错误字符个数*/ 
  312. void main() 
  313.     char yn; 
  314.     do
  315.         init(); /*初始化*/ 
  316.         scanner();/*扫描源程序*/ 
  317.         printf("Are You continue(y/n)/n"); //判断是否继续? 
  318.         yn=getch(); 
  319.     }while(yn=='y'||yn=='Y'); 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值