编译原理词法分析器(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'); 
实验一:词法分析程序 一、实验目的     通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的类型码及单词符号的自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示) 二、实验要求 用C或C++写一个简单的词法分析程序,程序可以满足下列要求: 1、能分析如下几种简单的语言词法 (1) 标识符: ID=letter(letter|digit)* (2) 关键字(全部小写) main int float double char if then else switch case break continue while do for (3)整型常量:NUM=digit digit* (4)运算符 = + - * / < <= == != > >= ; ( )? : (5)空格由空白、制表符和换行符组成,用以分隔ID、NUM、运算符等,字符分析时被忽略。 2、单词符号和相应的类别码 假定单词符号和相应的类别码如下: 单词符号 种别码 int 1 = 17 float 2 < 20 if 3 <= 21 switch 4 == 22 while 5 != 23 Do 6 > 24 标识符 10 >= 25 整型常量 11 ; 26 + 13 ( 27 - 14 ) 28 * 15 ? 29 / 16 : 30 3、词法分析程序实现的功能 输入:单词序列(以文件形式提供),输出识别的单词的二元组序列到文件和屏幕 输出:二元组构成: (syn,token或sum) 其中: syn 为单词的种别码 token 为存放的单词自身符号串 sum 为整型常数 例: 源程序: int ab; float ef=20; ab=10+ef; 输出: (保留字--1,int) (标识符--10,ab) (分号--26,;) (保留字--2,float) (标识符--10,ef) (等号--17,=) (整数--11,20) (分号--26,;) (标识符--10,ab) (等号--17,=) (整数--11,10) (加号--13,+) (标识符--10,ef) (分号--26,;) 4、自己准备测试数据存放于TestData.txt文件中,测试数据中应覆盖有以上5种数据,测试结果要求以原数据与结果对照的形式输出并保存在Result.txt中,同时要把结果输出到屏幕。 5、提前准备 ① 实验前,先编制好程序,上机时输入并调试程序。 准备好多组测试数据(存放于文件TestData.txt中)。 6、写出实验报告 报告格式:要求有实验名称、实验目的、实验要求、实验内容、实验小结。 其中实验内容包括算法分析、程序流程图及程序代码
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值