C语言词法分析器实验报告
词法分析程序实验报告

词法分析程序实验报告篇一:词法分析器_实验报告词法分析器实验报告实验目的:设计、编制、调试一个词法分析子程序-识别单词,加深对词法分析原理的理解。
实验要求:该程序要实现的是一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分界符五大类。
并依次输出各个单词的内部编码及单词符号自身值。
(一)实验内容(1)功能描述:对给定的程序通过词法分析器弄够识别一个个单词符号,并以二元式(单词种别码,单词符号的属性值)显示。
而本程序则是通过对给定路径的文件的分析后以单词符号和文字提示显示。
(2)程序结构描述:函数调用格式:参数含义:String string;存放读入的字符串 String str; 存放暂时读入的字符串 char ch; 存放读入的字符 int rs 判断读入的文件是否为空 char []data 存放文件中的数据 int m;通过switch用来判断字符类型,函数之间的调用关系图:函数功能:Judgement()判断输入的字符并输出单词符号,返回值为空; getChar() 读取文件的,返回值为空;isLetter(char c) 判断读入的字符是否为字母的,返回值为Boolean类型; switch (m) 判断跳转输出返回值为空;isOperator(char c)判断是否为运算符的,返回值为Boolean类型; isKey(String string)判断是否为关键字的,返回值为Boolean类型; isDigit(char c) 判断读入的字符是否为数字的,返回值为Boolean类型。
(二)实验过程记录:本次实验出错3次,第一次无法输出双运算符,于是采用双重if条件句进行判断,此方法失败,出现了重复输出,继续修改if语句,仍没有成功。
然后就采用了直接方法调用解决此问题。
对于变量的判断,开始忘了考虑字母和数字组成的变量,结果让字母和数字分家了,不过改变if语句的条件,解决了此问题。
c词法分析器实验报告

c词法分析器实验报告篇一:词法分析器设计实验报告计算机与信息学院(信息工程系)编译原理实验报告专业班级课程教学班任课教实验指导教师实验地点XX ~XX学年第一学期实验一词法分析器设计一、实验目的通过本实验的编程实践,使学生了解词法分析的任务,掌握词法分析程序设计的原理和构造方法,使学生对编译的基本概念、原理和方法有完整的和清楚的理解,并能正确地、熟练地运用。
二、实验内容用 VC++/VB/JAVA 语言实现对 C 语言子集的源程序进行词法分析。
通过输入源程序从左到右对字符串进行扫描和分解,依次输出各个单词的内部编码及单词符号自身值;若遇到错误则显示“Error”,然后跳过错误部分继续显示;同时进行标识符登记符号表的管理。
以下是实现词法分析设计的主要工作:(1)从源程序文件中读入字符。
(2 (3 (4(属性值——token 的机内表示)(5)如果发现错误则报告出错 7(6三、实验流程图四、实验步骤12、编制好源程序后,设计若干用例对系统进行全面的上机测试,并通过所设计的词法分析程序;直至能够得到完全满意的结果。
3、书写实验报告;实验报告正文的内容:五、实验结果篇二:C语言词法分析器实验报告计算机科学与工程系编译原理课程设计实验报告姓名:__ ******__ 学号_ *******__ 年级专业及班级___08计算机科学与技术成绩- 1 -- 2 -- 3 -- 4 -- 5 -篇三:词法分析器实验报告实验报告实验题目:词法分析器院系班级:计科系0901班姓名学号: XX210603实验时间:XX-10-21设计。
编制并调试一个词法分析程序,加深对词法分析原理的理解。
实验要求设计出一个简单的词法分析程序,能够识别关键字(包含begin、if、end、 while、else、 then)、标识符、数字及数种符号(+、-、*、/、(、)、:、=、:=、#、>、、=、;)。
返回并打印各类字符所对应的种类编码及该字符所组成的二元组。
(完整版)C语言词法分析器实验报告汇总

计算机科学与工程系编译原理课程设计实验报告扫描数字: 是f 判别为自减符Ch 是否为‘-'判别为”-> ”预读ch预读ch判别为负号 回退3Ch 为数字或‘预读ch扫描引号:扫描单词:实验步骤:1、准备:用TC、VC++等开发工具;2、对本实验的任务进行分析,确定实现功能的函数;3、写好程序,仔细修改函数;4、上机操作:输入源程序,修改、调试,运行。
5、写好试验报告。
实验调试过程及测试结果代^码******************************/#in clude<stdio.h> #in clude<stdlib.h> #in elude <ctype.h> #in clude<stri ng.h> void mai n()FILE *fp,*fp1;int guanjz(char ch1[]);〃关键字和标识符判断int hanjsq=1;〃行计数器,保存行号char ch,infile[15],outfile[15];〃定义输入和输出文件名prin tf("*****************E nter the infilename*****************sca nf("%s",i nfile);// 输入需要扫描的文件名printf( Enter the outfile n ame******************\n");scan f("%s",outfile);// 输入需要另存为的文件名if((fp = fope n(i nfile,"r")) == NULL)// {prin tf("ca nnot ope n file\n");exit(0);}if((fp1 = fope n(outfile,"w")) == NULL)// 打开需要扫描的文件打开需要存入的文件/*******************************{printf("cannot open file'n"); exit(O); }printf("\n******************************************************** printf( "******************************************************* printf("\n******************************************************** printf( "******************************************************* fprin tf(fp1,"*******************************************************fprin tf(fp1, while(!feof(fp)) {ch=fgetc(fp); if(ch==1O)hanjsq++;if(isalpha(ch) || ch=='_')〃 如果第一个字符为字母或下划线则判断为标识符{int i=0; char ch1[30];〃 假疋母个标识付取长为ch1[i++]=ch;〃将ch 保存到ch1[0]中并使i 自加1while(!feof(fp)) {ch=fgetc(fp);if(ch==1O)hanjsq++;〃 如果ch 为换行符,则行计数器自加 1if(isalpha(ch) || isdigit(ch) || ch=='')扫描头文件单词及保留字*********************printf("* 》开始进行词法分析 《*\ n");printf (”行号 字符串 种别码\n");fprin tf(fp1,"行号 字符串 种别码\n"); **\ n");{fseek(fp,-1,1);//fp 回退 1ch1[i]='\0';〃 把结束标志放到 ch1[i]中作为单词结束标志prin tf("li ne %d:%s%d\n",hanjsq,ch1,guanjz(ch1));〃以字符串形式输fprin tf(fp1,"li ne %d: %s%d\n" ,hanjsq,ch1,guanjz(ch1));break; }} if(!isalpha(ch) && !isdigit(ch) && ch!='_' && ch!='.') {//如果ch 不为字母、数字、下划线和点时判断其为标识符ch1[i]='\0'; prin tf("li ne %d: fprin tf(fp1,"li ne %d:%s{//如果ch 为字母、数字或下划线就把ch 放到ch1[i]中并使i 自加1ch1[i++]=ch; }if(ch=='.')〃 如果ch 为小数点则判断是否为头文件{if((ch=fgetc(fp))=='h')〃如果小数点后一位为 h 则判定其为头文件{if(ch==10)hanjsq++; chi[i++]='.'; chi[i++]='h'; ch1[i]='\0';〃 把结束标志放到 chi[i]中作为单词结束标志prin tf("li ne %d: %s83\n",hanjsq,ch1);〃以字符串形式输出 ch1fprin tf(fp1,"li ne %d: %s83\n",hanjsq,ch1);break;else//如果小数点后一位不是h 则判定其为标识符出chi%s%d\n",hanjsq,ch1,guanjz(ch1)); %d\n ”,hanjsq,ch1,guanjz(ch1));if(isdigit(ch) || ch=='-')〃如果ch 为数字或'-'if(isdigit(ch))//如果ch 为数字prin tf("li ne %d:%c",hanjsq,ch);while(!feof(fp))if(isdigit(ch) || ch=='.')prin tf("%c",ch); fprin tf(fp1,"%c",ch);else//否则视为数字结束printf (” fprin tf(fp1,"ch='O :〃 置ch 为0,以免影响下面误判并顺利退出扫描数字 break;break;/************************j 扫描数*************************/fprintf(fp1,"line %d: %c",hanjsq,ch);ch=fgetc(fp);〃预读一位如果 ch 为数字和点则循环输出46\n");46\n");fseek(fp,-1,1);〃回退一位if(ch=='-')〃如果ch 为'-'ch=fgetc(fp);〃 预读一位if (ch=='-')〃 如果ch 还是为'-'则判断为自减符'--' {prin tf("li ne %d: fprintf(fp1,"line %d: }if(isdigit(ch))〃 如果ch 为数字则可能为减号或负号{fseek(fp,-3,1);〃 回退 3 为判断ch=fgetc(fp); if(isdigit(ch))〃如果ch 为数字则判断'-'为减号ch=fgetc(fp); prin tf("li ne %d:%cfprin tf(fp1,"li ne %d: }else //否则判断'-'为负号 {ch=fgetc(fp); prin tf("li ne %d: fprin tf(fp1,"li ne %d: while(!feof(fp)) {ch=fgetc(fp);〃79\n" ,hanjsq,ch);%c 79\n",hanjsq,ch);80\n",hanjsq);80\n",hanjsq);如果ch 为'>',则判断为结构体运算符’->'{prin tf("li ne %d:->fprin tf(fp1,"li ne %d:->81\ n",hanjsq);81\ n",hanjsq);预读一位如果 %c",hanjsq,ch);%c",hanjsq,ch);ch 为数字和点则循环输出if(ch=='>')〃 }if(isdigit(ch) || ch=='.') {prin tf("%c",ch);fprin tf(fp1,"%c",ch);}扫描注释**********************else//否则视为数字结束{printf(”46\n");fprin tf(fp1," 46\n");fseek(fp,-1,1);// 回退1break;}}}if(ch=='/')〃如果ch为'/'则可能为注释{ch=fgetc(fp);〃读下一个字符if(ch==10)hanjsq++;if(ch=='/')〃如果该字符也为'/'则判断为注释一行{while(fgetc(fp)!=10);if(ch==10)hanjsq++;〃直到遇到换行符出现才认为注释结束}if(ch=='*')〃如果该字符为'*'则判断为注释多行{//直到出现’*/' 才认为注释结束while(!feof(fp)) {ch=fgetc(fp); if(ch==10)hanjsq++; if(ch=='*')〃 出现'*'{//且接着出现'/' if((ch=fgetc(fp))=='/')break;while(!feof(fp))else// 否则原样输出'/'prin tf("li ne %d: fprintf(fp1,"line %d:83\n",hanjsq);83\n",hanjsq);fseek(fp,-1,1);〃 回退1break;/***********************扫描引号 ************************/ if(ch==””)〃出现引号int i=0;prin tf("li ne %d:%cfprin tf(fp1,"li ne %d: %c 82\n",hanjsq,ch);82\n",hanjsq,ch);prin tf("li ne %d: ",hanjsq);fprin tf(fp1,"li ne %d:",hanjsq);{//先整体输出引号内所有字符并定为第99类ch=fgetc(fp);i++;〃用于积累回退长度 if(ch==10)hanjsq++; if(ch!=””) {if(ch!=32) {prin tf("%c",ch); fprin tf(fp1,"%c",ch); } }else break;} printf (” fprin tf(fp1," fseek(fp,-i,1);// for(;i>0;i--) {ch=fgetc(fp);if(ch==92)// 如果ch 为'\'则可能为转义字符 {char ch5[13]={"abfntv\\?'\"0"}; // 转义字符集ch=fgetc(fp);// 预读一位 for(int k=0;k<12;k++) {//如果为转义字符则输出if(ch==ch5[k]) {99\n");99\n");回退到引号开始printf(”line %d: \\%c%d\n" ,hanjsq,ch,k+33);fprin tf(fp1," li ne %d: \\%c }}if(ch=='d' && isdigit(fgetc(fp)) && isdigit(fgetc(fp)))〃{fseek(fp,-2,1);prin tf(" line %d: %c%cfprin tf(fp1," line %d: %c%c }if(ch=='x' && isdigit(fgetc(fp)) && isdigit(fgetc(fp)))//{fseek(fp,-2,1);prin tf(" line %d: %c%cfprin tf(fp1," line %d: %c%c }}if(ch=='%')〃如果为'%'则可能为%s%c%d%d\n",hanjsq,ch,k+33);任意字符转换为三位八进制44\n",hanjsq,fgetc(fp),fgetc(fp));44\n “,hanjsq,fgetc(fp),fgetc(fp));任意字符转换为二位十六进制45\n",hanjsq,fgetc(fp),fgetc(fp));45\n",hanjsq,fgetc(fp),fgetc(fp));{ch=fgetc(fp);char bfh[4]={"dcs"};for(i=0;i<3;i++){if(bfh[i]==ch){prin tf(" line %d: %%%c fprin tf(fp1," line %d: %%%c 83\n",hanjsq,ch);83\n",hanjsq,ch);}}}}描其他符^号 ********************/if(!isdigit(ch) && !isalpha(ch) && ch!='_' && ch!=”” && ch!='/')char ch2[14]={"#()[]{}'*:~%A"};〃for(i nt i=0;i<13;i++) {//判断单个符号if(ch==ch2[i])for(i nt j=0;j<8;j++) {//判断双符号if(ch==ch3[j])ch=fgetc(fp);//预读一位//if(ch==10)hanjsq++; if(ch==ch4[j]){//且ch 与ch4第j 个匹配,则表示 ch3[j]与ch4[j]if(ch=='<' && ch3[j]=='<')〃prin tf("li ne %d:%c%c%d\n",hanjsq,ch3[j],ch4[j],i+69);fprin tf(fp1,"li ne %d:%c%c%d\n",hanjsq,ch3[j],ch4[j],i+69);/*********************char ch3[9]={"+?=|&!<>"};// 定义部分单符号或双符号(前半部分)集 char ch4[9]={"+==|&==="};//定义部分双符号(后半部分)定义部分单符号集prin tf("li ne %d: %c%d\n" ,hanjsq,ch,i+48);fprintf(fp1,"line %d:%c%d\n",hanjsq,ch,i+48);{//如果ch 与ch3中第j个字符匹配 连起来为一个双符号判断'<<'符prin tf("li ne %d:fprin tf(fp1,"li ne %d:}if(ch=='>' &&ch3[j]=='>')〃{prin tf("li ne %d:fprin tf(fp1,"li ne %d:}else//否则表示ch3[j]{prin tf("li ne %d:fprin tf(fp1,"li ne %d:fseek(fp,-1,1);}}}}<<<<判断'>>'符>>>>为单符号,不是双符号的%c%c77\n",hanjsq);77\n",hanjsq);78\n",hanjsq);78\n",hanjsq);部分%d\n",hanjsq,ch3[j],j+61);%d\n" ,hanjsq,ch3[j],j+61)****************************************************************************************** printf( **\n");prin tf("* 》词法分析结束prin tf("* 》分析结果保存在文件%$中prin tf("* 》欢迎下次使用,谢谢!《*\n"); 《*\ n",outfile);《*\n");printf( **\n");fprin tf(fp1, **\ n");fprin tf(fp1,"*》词法分析结束《*\n");fprin tf(fp1,"*》欢迎下次使用,谢谢! 《*\n");-21-**\ n");in t guanjz(char ch1[])〃 关键字和标识符判断{char ch2[32][9]={"auto","double","i nt","struct","break","else","lo ng","switch","case","e num","register","typedef","char","extern","return","u nio n","co nst","float","short", "un sig ned","co nti nue","for","sig ned","void","default","goto","sizeof', "volatile","do","while","static","if"};〃for(int i=0;i<32;i++){//逐个比对如果为关键字则返回类别i+1if(!strcmp(ch1,ch2[i]))return i+1;return 47;//否则返回一般标识符类竝船悅Q )動勒卜1 Inc 1: u 4Sline 12 iniclude 4?1 ine 1* <b'fline 1 =. Ili B3lino is >hVlliw 2s U 4Rlima 2s inaludo 47a I M 2? <67 line 2s atAlib.hlinfi 2: > GS line 3; voidline main<17 line 3: <J. inc 3 = > ba line 3: S3 1 inc Ss riLE47 1 Irte 5:lime S ; __ s4?test・曹■:M 曹Entei' t hs outf ile ri 之盯日****^***-¥******<^^ cf F MQI , txt a t ■ w a w at it at ■ at ituif at it at U tguit at ■ • ac n unit ar ・ a t ariMiM at MTM a t :M at ■ u at 心 开史舱行词法分析 <* 讪:"Ik 1: 血1: line 1: Ike]: H UE 2: Iine2: line 2: lines : !k2: line 3: line 3: line 5; line 3: lineS: lint 5: J5QE 5:inclu tstdin hCaiLFIL E丰ft:■:•S3=;阴i 7占fl定义关键字集-22 -:B 豪溥■ ar 河*"百■苜筒n w i {耳鼻p n t 尸广 『卄戸1 nt- _i I n Finp 卄 可・w 耳黑図耳溥■ ar ・鼻*E E 戸F trst«t 具t■ if 耳材imcirHirjtKjnf i fK^nrBl* Th& outf lie 寺暮打補整并*需斗弭書!**祥略再整番 cffXQ-tXt at ir uif at ■ u u KM at ■・ at MENU at ■ U M ~W ;M a t WM a t aa ・:■■■!< IT ar ~K af M E :I m a t at nwu at心开给进行词法分析 3 =!K X KM X MM X X MJ4 K M M X KM X K M X MMX M M M MM M X K M X <M MM MM MMX KKXXMM X MM K MMK K liine line 丄±n& line lino 1 irie linn llnft line liri@ Urie line line 丄inc line linn line line n include stdllo .li includostdlib.lh uo id Piaui PILE 48 4? G7 83 bS 1? 67 68 24 4?印呼 bU 53 4? -1? Line 2C 4Gi Limo 2^鼻llna 26 cnimt 47Line 2& >Line 27 9 JlE & E Line 27 if3Z line 27 C 49 Lime H7 st>£rii^ 47 line 2? C 51 Line 2? 1 47 Line 27 J ^2 Lime 2? 71 Lino 2? ■ «3Jifle 7.7=Livw 2? i 55 Lino 2? > saline 27 55Line 27 > 50 Lino 28 printf 47 1 Ine 2» C 49 Line 20 ii S2Lino 纫 xd\n1血2& + 61 lint 2& T alint 2&■ccmtrlint 26 1 ) 50 line A else6血27 if $IT f X49 1血H Etring Ji 血H [ 51]ine 牙 1 1 rlillE H ] 血H LIM 二line 卵 -1遊H1遊2T)血2TJ 1]inc 2T )line 28 printf 眄 line 28 ■:191U23 ifI LDE 28册-23 -liit 31: liiE 31:y Ike y c-uritrirqiue printFine 3U:<V Igf PF FInCKdXnNdine SI R *\n+ +++c a unt分析结果保总结:lhe加line 29: 1血亚line 3:lii诃;line SO;lbue 50:\be30:line 30:line:fl:Hie 50:luie 馬:血数lhs33!line J3:):rintf(*rfrrfinlMtnHJ\p卄i:wqJIq:4r»-I—ru=rfc-pFlL.■i^u-In—r^vFhdfLJ.rkj"rnuriui指导教师签名:2011年4月12日星期二。
实验一词法分析器实验报告示例

词法分析器实验报告一.需求分析1.C语言关键字的子集,以文件形式保存,待判断的C语言语句以文件形式保存。
2.关键字文件包括标识符、基本字、常数、运算符和界符以及相应的种别码。
3.在计算机终端顺次输出各词法单位的种别码和值。
若为标识符,其值为该标识符在标识符表中的位置;若为常数,其值为该常数在常数表中的位置;其余值为-1。
非法输入单词的种别码为-1,值为-2。
4.“单词”定义:C语言中最小的语法单位。
“标识符”定义:用户自定义的标志符。
5.测试数据:文本文件为unknowedword.text二.概要设计1.单词种别码设计如表一所示:A标识符及关键字如图一所示:图一.标识符状态转换图B实数如图二所示:图二.实数状态转换图C图三.运算符状态转换图D其它与运算符雷同,此处略3. 数据结构know[N] 用来存放构成单词符号的字符串;unknow[N]用来存放待辨别的字符串;chartab[T][N] 用来存放识别出的标识符;keytab[M][N] 用来存放从文件中读入的基本字;consttab[T] 用来存放识别出的实数。
以上均设为全局变量。
4.基本操作Getchar()初始条件:unknow中读入了一串待辨别的字符串。
操作结果:从unknow读入一个字符到ch中,指向unknow的指针加1。
Getbc()初始条件:unknow中读入了一串待辨别的字符串。
操作结果:从unknow中读入不是空格的下一个字符。
Concat()初始条件:know中为字母且ch中为字母,或know中为数字(包括小数点)ch中也为数字。
操作结果:将ch中的字符连接到know中。
Isletter()初始条件:ch中已经读入了一个新的字符。
操作结果:判断ch中的字符是否字母。
Isdigit()初始条件:ch中已经读入了一个新的字符。
操作结果:判断ch中的字符是否数字。
Keyword()初始条件:已判断出know中的字符串为标识符。
C语言词法分析器实验报告

计算机科学与工程系编译原理课程设计实验报告姓名:__ ******__ 学号_ *******__ 年级专业及班级___08计算机科学与技术成绩扫描引号:扫描单词:扫描其他字符:实验环境:需要TC、VC++ 6.0等开发工具作为本次试验的环境。
fprintf(fp1,"*********************************************************\n");}int guanjz(char ch1[])//关键字和标识符判断{char ch2[32][9]={"auto","double","int","struct","break","else","long","switch","case","enum", "register","typedef","char","extern","return","union","const","float","short","unsigned","continue","for","signed","void","default","goto","sizeof","volatile","do","while","static","if"};//定义关键字集for(int i=0;i<32;i++){//逐个比对如果为关键字则返回类别i+1if(!strcmp(ch1,ch2[i]))return i+1;}return 47;//否则返回一般标识符类}总结:指导教师签名:2011年4月12日星期二。
词法分析器实验报告

词法分析器实验报告词法分析器实验报告词法分析器设计一、实验目的:对C语言的一个子集设计并实现一个简单的词法分析器,掌握利用状态转换图设计词法分析器的基本方法。
利用该词法分析器完成对源程序字符串的词法分析。
输出形式是源程序的单词符号二元式的代码,并保存到文件中。
二、实验内容:1. 设计原理词法分析的任务:从左至右逐个字符地对源程序进行扫描,产生一个个单词符号。
理论基础:有限自动机、正规文法、正规式词法分析器(Lexical Analyzer) 又称扫描器(Scanner):执行词法分析的程序2. 词法分析器的功能和输出形式功能:输入源程序、输出单词符号程序语言的单词符号一般分为以下五种:关键字、标识符、常数、运算符,界符3. 输出的单词符号的表示形式:单词种别用整数编码,关键字一字一种,标识符统归为一种,常数一种,各种符号各一种。
4. 词法分析器的结构单词符号5. 状态转换图实现三、程序设计1.总体模块设计/*用来存储目标文件名*/string file_name;/*提取文本文件中的信息。
*/string GetText();/*获得一个单词符号,从位置i开始查找。
并且有一个引用参数j,用来返回这个单词最后一个字符在str的位置。
*/string GetWord(string str,int i,int& j);/*这个函数用来除去字符串中连续的空格和换行int DeleteNull(string str,int i);/*判断i当前所指的字符是否为一个分界符,是的话返回真,反之假*/bool IsBoundary(string str,int i);/*判断i当前所指的字符是否为一个运算符,是的话返回真,反之假*/bool IsOperation(string str,int i);/*此函数将一个pair数组输出到一个文件中*/void OutFile(vector<pair > v);</pair/*此函数接受一个字符串数组,对它进行词法分析,返回一个pair型数组*/vector<pair > analyst(vector vec);</pair/*此函数判断传递的参数是否为关键字,是的话,返回真,反之返回假*/ bool IsKey(string str);2.各模块设计(1).首先根据上面单词符号表及ID和NUM的正规定义式,构造出状态转换图;(2).定义相关的变量和数据结构。
编译原理C语言词法分析报告器

编译原理 C语言词法分析器一、实验题目编制并调试C词法分析程序。
a.txt源代码:•main() {int sum=0 ,it=1;/* Variable declaration*/if (sum==1)it++;elseit=it+2;}•设计其词法分析程序,能识别出所有的关键字、标识符、常数、运算符(包括复合运算符,如++)、界符;能过滤掉源程序中的注释、空格、制表符、换行符;并且能够对一些词法规则的错误进行必要的处理,如:标识符只能由字母、数字和下划线组成,且第一个字符必须为字母或下划线。
实验要求:要给出所分析语言的词法说明,相应的状态转换图,单词的种别编码方案,词法分析程序的主要算法思想等。
二、实验目的1、理解词法分析在编译程序中的作用;2、掌握词法分析程序的实现方法和技术;3、加深对有穷自动机模型的理解。
三、主要函数四、设计1.主函数 void main ( )2. 初始化函数 void load ( )3. 保留字及标识符判断函数 void char_search(char *word)4. 整数类型判断函数 void inta_search(char *word)5. 浮点类型判断函数 void intb_search(char *word)6. 字符串常量判断函数 void cc_search(char *word)7. 字符常量判断函数 void c_search(char *word)同4、5函数图8.主扫描函数 void scan ( )五、关键代码#include <stdio.h>#include <string.h>#include <stdlib.h>char *key0[]={" ","auto","break","case","char","const","continue","default","do","double","else","enum","extern", "float","for","goto","if","int","long","register","return","short","signed","sizeof","static","st ruct","switch","typedef","_Complex","_Imaginary","union","unsigned","void","volatile","while"};/*保留字表*/char *key1[]={" ","(",")","[","]","{","}",",",";","'"};/*分隔符表*/char *key2[]={" ","+","-","*","/","%","<",">","==",">=","<=","!=","!","&&","||","<<",">>","~","|","^","&","=","?: ","->","++","--",".","+=","-=","*=","/="};/*运算符表*/int xx0[35],xx1[10],xx2[31];int temp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;/******* 初始化函数 *******/void load(){int mm;for (mm=0;mm<=34;mm++){xx0[mm]=0;}for (mm=0;mm<=9;mm++){xx1[mm]=0;}for (mm=0;mm<=30;mm++){xx2[mm]=0;}FILE *floading;if ((floading=fopen("key0.txt","w"))==NULL){printf("Error! Can't create file : key0.txt");return;}fclose (floading);/*建立保留字表文件:key0.txt*/if ((floading=fopen("key1.txt","w"))==NULL){printf("Error! Can't create file : key1.txt");return;}/*建立分隔符表文件:key1.txt*/if ((floading=fopen("key2.txt","w"))==NULL){printf("Error! Can't create file : key2.txt");return;}fclose(floading);/*建立运算符表文件:key2.txt*/if ((floading=fopen("key3.txt","w"))==NULL){printf("Error! Can't create file : key3.txt");return;}fclose (floading);/*建立标识符表文件:key3.txt*/if ((floading=fopen("c40.txt","w"))==NULL){printf("Error! Can't create file : c40.txt");return;}fclose (floading);/*建立整数类型常量表文件:c40.txt*/if ((floading=fopen("c41.txt","w"))==NULL){printf("Error! Can't create file : c41.txt");return;}fclose (floading);/*建立浮点类型常量表文件:c41.txt*/if ((floading=fopen("c42.txt","w"))==NULL){printf("Error! Can't create file : c42.txt");return;}fclose (floading);/*建立字符类型常量表文件:c42.txt*/if ((floading=fopen("c43.txt","w"))==NULL){printf("Error! Can't create file : c43.txt");return;}fclose (floading);/*建立字符串类型常量表文件:c43.txt*/if ((floading=fopen("defination.txt","w"))==NULL) {printf("Error! Can't create file : defination.txt");return;}fclose (floading);/*建立注释文件:defination.txt*/if ((floading=fopen("output.txt","w"))==NULL){printf("Error! Can't create file : output.txt");return;}fclose (floading);/*建立内部码文件:output.txt*/if ((floading=fopen("temp_key1","w"))==NULL){printf("Error! Can't create file : temp_key1");return;}fclose (floading);/*建立保留字临时表文件:temp_key1*/if ((floading=fopen("temp_key3","w"))==NULL){printf("Error! Can't create file : temp_key3");return;}fclose (floading);/*建立标识符临时文件:temp_key3*/if ((floading=fopen("temp_c40","w"))==NULL){printf("Error! Can't create file : temp_c40");return;}fclose (floading);/*建立整数类型常量临时文件:temp_c40*/if ((floading=fopen("temp_c41","w"))==NULL){printf("Error! Can't create file : temp_c41");return;}fclose (floading);/*建立浮点类型常量临时文件:temp_c41*/if ((floading=fopen("temp_c42","w"))==NULL){printf("Error! Can't create file : temp_c42");return;}fclose (floading);/*建立字符类型常量临时文件:temp_c42*/if ((floading=fopen("temp_c43","w"))==NULL){printf("Error! Can't create file : temp_c43");return;}fclose (floading);/*建立字符串类型常量临时文件:temp_c43*/}/******* 保留字及标识符判断函数 *******/void char_search(char *word){int m,line=0,csi=0;int value=0;int value2=0;char c,cs[100];FILE *foutput,*finput;for (m=1;m<=34;m++){if (strcmp(word,key0[m])==0){value=1;break;}}if (value==1){if (xx0[m]==0){foutput=fopen("key0.txt","a");fprintf(foutput,"0\t%d\t\t%s\n",m,word);fclose(foutput);xx0[m]=1;}foutput=fopen("output.txt","a");fprintf(foutput,"0\t%d\t\t%s\n",m,word);fclose(foutput);}else{if (temp_key3==0){foutput=fopen("temp_key3","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_key3++;foutput=fopen("key3.txt","a");fprintf(foutput,"3\t1\t\t%s\n",word);fclose(foutput);}finput=fopen("temp_key3","r");c=fgetc(finput);while (c!=EOF){while (c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if ((strcmp(cs,word))==0){value2=1;break;}else{value2=0;c=fgetc(finput);}}fclose(finput);if (value2==1){foutput=fopen("output.txt","a");fprintf(foutput,"3\t%d\t\t%s\n",line,word);fclose(foutput);}else{foutput=fopen("temp_key3","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_key3++;foutput=fopen("output.txt","a");fprintf(foutput,"3\t%d\t\t%s\n",temp_key3,word);fclose(foutput);foutput=fopen("key3.txt","a");fprintf(foutput,"3\t%d\t\t%s\n",temp_key3,word);fclose(foutput);}}}/******* 整数类型判断函数 *******/void inta_search(char *word){FILE *foutput,*finput;char c;char cs[100];int csi=0;int line=0;int value2=0;if (temp_c40==0){foutput=fopen("temp_c40","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c40++;foutput=fopen("c40.txt","a");fprintf(foutput,"4\t0\t1\t%s\n",word);fclose(foutput);}finput=fopen("temp_c40","r");c=fgetc(finput);while (c!=EOF){while (c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if (strcmp(cs,word)==0){value2=1;break;}c=fgetc(finput);}fclose(finput);if (value2==1){foutput=fopen("output.txt","a");fprintf(foutput,"4\t0\t%d\t%s\n",line,word);fclose(foutput);}else{foutput=fopen("temp_c40","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c40++;foutput=fopen("output.txt","a");fprintf(foutput,"4\t0\t%d\t%s\n",temp_c40,word);fclose(foutput);foutput=fopen("c40.txt","a");fprintf(foutput,"4\t0\t%d\t%s\n",temp_c40,word);fclose(foutput);}}/******* 浮点类型判断函数 *******/void intb_search(char *word){FILE *foutput,*finput;char c;char cs[100];int csi=0;int line=0;int value2=0;if (temp_c41==0){foutput=fopen("temp_c41","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c41++;foutput=fopen("c41.txt","a");fprintf(foutput,"4\t1\t1\t%s\n",word);fclose(foutput);}finput=fopen("temp_c41","r");c=fgetc(finput);while (c!=EOF){while (c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if (strcmp(cs,word)==0){value2=1;break;}c=fgetc(finput);}fclose(finput);if (value2==1){foutput=fopen("output.txt","a");fprintf(foutput,"4\t1\t%d\t%s\n",line,word);fclose(foutput);}else{foutput=fopen("temp_c41","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c41++;foutput=fopen("output.txt","a");fprintf(foutput,"4\t1\t%d\t%s\n",temp_c41,word);fclose(foutput);foutput=fopen("c40.txt","a");fprintf(foutput,"4\t1\t%d\t%s\n",temp_c41,word);fclose(foutput);}}/******* 字符串常量判断函数 *******/void cc_search(char *word){FILE *foutput,*finput;char c;char cs[100];int csi=0;int line=0;int value2=0;if (temp_c43==0){foutput=fopen("temp_c43","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c43++;foutput=fopen("c43.txt","a");fprintf(foutput,"4\t3\t1\t%s\n",word);fclose(foutput);}finput=fopen("temp_c43","r");c=fgetc(finput);while (c!=EOF){while (c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if (strcmp(cs,word)==0){value2=1;break;}c=fgetc(finput);}fclose(finput);if (value2==1){foutput=fopen("output.txt","a");fprintf(foutput,"4\t3\t%d\t%s\n",line,word);fclose(foutput);}else{foutput=fopen("temp_c43","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c43++;foutput=fopen("output.txt","a");fprintf(foutput,"4\t3\t%d\t%s\n",temp_c43,word);fclose(foutput);foutput=fopen("c43.txt","a");fprintf(foutput,"4\t3\t%d\t%s\n",temp_c43,word);fclose(foutput);}}/******* 字符常量判断函数 *******/void c_search(char *word){FILE *foutput,*finput;char c;char cs[100];int csi=0;int line=0;int value2=0;if (temp_c42==0){foutput=fopen("temp_c42","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c42++;foutput=fopen("c42.txt","a");fprintf(foutput,"4\t2\t1\t%s\n",word);fclose(foutput);}finput=fopen("temp_c42","r");c=fgetc(finput);while (c!=EOF){while (c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if (strcmp(cs,word)==0){value2=1;break;}c=fgetc(finput);}fclose(finput);if (value2==1){foutput=fopen("output.txt","a");fprintf(foutput,"4\t2\t%d\t%s\n",line,word);fclose(foutput);}else{foutput=fopen("temp_c42","a");fprintf(foutput,"%s\n",word);fclose(foutput);temp_c42++;foutput=fopen("output.txt","a");fprintf(foutput,"4\t2\t%d\t%s\n",temp_c42,word);fclose(foutput);foutput=fopen("c42.txt","a");fprintf(foutput,"4\t2\t%d\t%s\n",temp_c42,word);fclose(foutput);}}/******* 主扫描函数 *******/void scan(){int count;char chin;FILE *fin;FILE *fout;char filename[50];char temp[100];char target[3]="'";printf("请输入文件名:");scanf("%s",filename);if ((fin=fopen(filename,"r"))==NULL){printf("Error! Can't open file : %s\n",filename);return;}chin=fgetc(fin);while (chin!=EOF){/*对文件包含、宏定义进行处理*/if (chin=='#'){while (chin!='>')chin=fgetc(fin);/*chin=fgetc(fin);*/}/*对空格符、水平制表符进行处理*/else if ((chin==' ')||(chin=='\t')){;}/*对回车符进行处理*/else if (chin=='\n'){;}/*对单引号内的字符常量进行处理*/else if (chin==target[0]){if (xx1[9]==0){fout=fopen("key1.txt","a");fprintf(fout,"1\t9\t\t%c\n",target[0]);fclose(fout);xx1[9]=1;}temp[0]=chin;chin=fgetc(fin);temp[1]=chin;chin=fgetc(fin);if (chin!=target[0]){temp[2]=chin;chin=fgetc(fin);temp[3]=chin;temp[4]='\0';}else{temp[2]=chin;temp[3]='\0';}c_search(temp);}/*对双引号内的字符串常量进行处理*/else if (chin=='"'){int i=0;temp[i++]='"';chin=fgetc(fin);while (chin!='"'){temp[i++]=chin;chin=fgetc(fin);}temp[i]='"';temp[i+1]='\0';cc_search(temp);}/*对保留字、标识符进行处理*/else if (((chin>='A')&&(chin<='Z'))||((chin>='a')&&(chin<='z'))||(chin=='_')){int i=0;while(((chin>='A')&&(chin<='Z'))||((chin>='a')&&(chin<='z'))||(chin=='_')||((chin>='0')&&(chin<='9'))) {temp[i++]=chin;chin=fgetc(fin);}temp[i]='\0';char_search(temp);if (chin!=EOF)fseek (fin,-1L,SEEK_CUR);}/*对整型、浮点型数据进行处理*/else if ((chin>='0')&&(chin<='9')){int dotcount=0;int i=0;while (((chin>='0')&&(chin<='9'))||(chin=='.')){if (chin=='.')dotcount++;if (dotcount==2)break;temp[i++]=chin;chin=fgetc(fin);}temp[i]='\0';if (dotcount==1)intb_search(temp);elseinta_search(temp);if (chin!=EOF)fseek (fin,-1L,SEEK_CUR);}/*对注释进行处理*/else if (chin=='/'){chin=fgetc(fin);if (chin=='='){fout=fopen("output.txt","a");fprintf(fout,"2\t30\t\t/=\n");fclose(fout);}else if (chin!='*'){fout=fopen("output.txt","a");fprintf(fout,"2\t4\t\t/\n");fclose(fout);fseek(fin,-1L,SEEK_CUR);}else if (chin=='*'){count=0;chin=fgetc(fin);fout=fopen("defination.txt","a");fprintf(fout,"/*");while (count!=2){count=0;while (chin!='*'){fprintf(fout,"%c",chin);chin=fgetc(fin);}count++;fprintf(fout,"%c",chin);chin=fgetc(fin);if (chin=='/'){count++;fprintf(fout,"%c\n",chin);}else{fprintf(fout,"%c",chin);chin=fgetc(fin);}}}}/*对运算符、分隔符进行处理*/else{int time=0;int firstblood=0;temp[0]=chin;chin=fgetc(fin);if (chin!=EOF){temp[1]=chin;temp[2]='\0';for (time=1;time<=30;time++){if (strcmp(temp,key2[time])==0){firstblood=1;if (xx2[time]==0){fout=fopen("key2.txt","a");fprintf(fout,"2\t%d\t\t%s\n",time,temp);fclose(fout);xx2[time]=1;}fout=fopen("output.txt","a");fprintf(fout,"2\t%d\t\t%s\n",time,temp);fclose(fout);break;}}if (firstblood!=1){fseek(fin,-1L,SEEK_CUR);temp[1]='\0';for (time=1;time<=9;time++){if (strcmp(temp,key1[time])==0){if (xx1[time]==0){fout=fopen("key1.txt","a");fprintf(fout,"1\t%d\t\t%s\n",time,temp);fclose(fout);xx1[time]=1;}fout=fopen("output.txt","a");fprintf(fout,"1\t%d\t\t%s\n",time,temp);fclose(fout);break;}}for (time=1;time<=30;time++){if (strcmp(temp,key2[time])==0){if (xx2[time]==0){fout=fopen("key2.txt","a");fprintf(fout,"2\t%d\t\t%s\n",time,temp);fclose(fout);xx2[time]=1;}fout=fopen("output.txt","a");fprintf(fout,"2\t%d\t\t%s\n",time,temp);fclose(fout);break;}}}}}chin=fgetc(fin);}fout=fopen("output.txt","a");fprintf(fout,"1\t6\t\t}\n");fclose(fout);}/******* Main函数 *******/void main(){FILE *fread;char charin;char command='Q';printf("\n");printf("******************** C语言词法分析工具 ********************\n");printf("* *\n");printf("* *\n");printf("* 命令如下: *\n");printf("* 0 --> 查看保留字表文件 *\n");printf("* 1 --> 查看分隔符表文件 *\n"); printf("* 2 --> 查看运算符表文件 *\n"); printf("* 3 --> 查看标识符表文件 *\n"); printf("* 4 --> 查看整数类型常量表 *\n"); printf("* 5 --> 查看浮点类型常量表 *\n"); printf("* 6 --> 查看字符类型常量表 *\n"); printf("* 7 --> 查看字符串类型常量表 *\n"); printf("* 8 --> 查看注释文件 *\n"); printf("* 9 --> 查看内部码文件 *\n"); printf("* -------------------------- *\n"); printf("* Q --> 退出 *\n"); printf("***************************************************************\n"); printf("\n");load();scan();printf("\n");printf("分析完成!\n");getchar();printf("\n");printf("请输入命令:");command=getchar();while ((command!='Q')&&(command!='q')){switch (command){case '0':{printf("*************************\n");printf("\n");fread=fopen("key0.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '1':{printf("*************************\n");printf("\n");fread=fopen("key1.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '2':{printf("*************************\n");printf("\n");fread=fopen("key2.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '3':{printf("*************************\n");printf("\n");fread=fopen("key3.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '4':{printf("*************************\n");printf("\n");fread=fopen("c40.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '5':{printf("*************************\n");printf("\n");fread=fopen("c41.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '6':{printf("*************************\n");printf("\n");fread=fopen("c42.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '7':{printf("*************************\n");printf("\n");fread=fopen("c43.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '8':{printf("*************************\n");printf("\n");fread=fopen("defination.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}case '9':{printf("*************************\n");printf("\n");fread=fopen("output.txt","r");charin=fgetc(fread);while (charin!=EOF){putchar(charin);charin=fgetc(fread);}printf("\n");printf("*************************\n");printf("\n");printf("请输入命令:");break;}}command=getchar();}}。
词法分析器实验报告

词法分析器实验报告词法分析器实验报告一、引言词法分析器是编译器中的重要组成部分,它负责将源代码分解成一个个的词法单元,为之后的语法分析提供基础。
本实验旨在设计和实现一个简单的词法分析器,以深入理解其工作原理和实现过程。
二、实验目标本实验的目标是设计和实现一个能够对C语言代码进行词法分析的程序。
该程序能够将源代码分解成关键字、标识符、常量、运算符等各种词法单元,并输出其对应的词法类别。
三、实验方法1. 设计词法规则:根据C语言的词法规则,设计相应的正则表达式来描述各种词法单元的模式。
2. 实现词法分析器:利用编程语言(如Python)实现词法分析器,将源代码作为输入,根据词法规则将其分解成各种词法单元,并输出其类别。
3. 测试和调试:编写测试用例,对词法分析器进行测试和调试,确保其能够正确地识别和输出各种词法单元。
四、实验过程1. 设计词法规则:根据C语言的词法规则,我们需要设计正则表达式来描述各种词法单元的模式。
例如,关键字可以使用'|'操作符将所有关键字列举出来,标识符可以使用[a-zA-Z_][a-zA-Z0-9_]*的模式来匹配,常量可以使用[0-9]+的模式来匹配等等。
2. 实现词法分析器:我们选择使用Python来实现词法分析器。
首先,我们需要读取源代码文件,并将其按行分解。
然后,针对每一行的代码,我们使用正则表达式进行匹配,以识别各种词法单元。
最后,我们将识别出的词法单元输出到一个结果文件中。
3. 测试和调试:我们编写了一系列的测试用例,包括各种不同的C语言代码片段,以测试词法分析器的正确性和鲁棒性。
通过逐个测试用例的运行结果,我们可以发现和解决词法分析器中的问题,并进行相应的调试。
五、实验结果经过多次测试和调试,我们的词法分析器能够正确地将C语言代码分解成各种词法单元,并输出其对应的类别。
例如,对于输入的代码片段:```cint main() {int a = 10;printf("Hello, world!\n");return 0;}```我们的词法分析器将输出以下结果:```关键字:int标识符:main运算符:(运算符:)运算符:{关键字:int标识符:a运算符:=常量:10运算符:;标识符:printf运算符:(常量:"Hello, world!\n"运算符:)运算符:;关键字:return常量:0运算符:;```可以看到,词法分析器能够正确地将代码分解成各种词法单元,并输出其对应的类别。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
计算机科学与工程系
编译原理课程设计实验报告姓名:__ ******__ 学号_ *******__ 年级专业及班级___08计算机科学与技术成绩
扫描引号:
扫描单词:
扫描其他字符:
实验环境:需要TC、VC++ 6.0等开发工具作为本次试验的环境。
fprintf(fp1,"*********************************************************\n");
}
int guanjz(char ch1[])//关键字和标识符判断
{
char ch2[32][9]={"auto","double","int","struct","break","else","long","switch","case","enum", "register","typedef","char","extern","return","union","const","float","short",
"unsigned","continue","for","signed","void","default","goto","sizeof",
"volatile","do","while","static","if"};//定义关键字集
for(int i=0;i<32;i++)
{//逐个比对如果为关键字则返回类别i+1
if(!strcmp(ch1,ch2[i]))return i+1;
}
return 47;//否则返回一般标识符类
}
总结:
指导教师签名:
2011年4月12日星期二。