实验1-3-《编译原理》词法分析程序设计方案.doc
实验1-3 编译原理S语言词法分析程序设计方案一、实验目的了解词法分析程序的两种设计方法之一:根据状态转换图直接编程的方式; 二、实验内容1根据状态转换图直接编程编写一个词法分析程序,它从左到右逐个字符的对源程序进行扫描,产生一个个的单词的二元式,形成二元式(记号)流文件输出。在此,词法分析程序作为单独的一遍,如下图所示。具体任务有:(1)组织源程序的输入(2)拼出单词并查找其类别编号,形成二元式输出,得到单词流文件(3)删除注释、空格和无用符号(4)发现并定位词法错误,需要输出错误的位置在源程序中的第几行。将错误信息输出到屏幕上。(5)对于普通标识符和常量,分别建立标识符表和常量表(使用线性表存储),当遇到一个标识符或常量时,查找标识符表或常量表,若存在,则返回位置,否则返回0并且填写符号表或常量表。标识符表结构:变量名,类型(整型、实型、字符型),分配的数据区地址注:词法分析阶段只填写变量名,其它部分在语法分析、语义分析、代码生成等阶段逐步填入。常量表结构:常量名,常量值三、实验要求1能对任何S语言源程序进行分析在运行词法分析程序时,应该用问答形式输入要被分析的S源语言程序的文件名,然后对该程序完成词法分析任务。2能检查并处理某些词法分析错误词法分析程序能给出的错误信息包括:总的出错个数,每个错误所在的行号,错误的编号及错误信息。本实验要求处理以下两种错误(编号分别为1,2):1:非法字符:单词表中不存在的字符处理为非法字符,处理方式是删除该字符,给出错误信息,“某某字符非法”。2:源程序文件结束而注释未结束。注释格式为:/* */四、保留字和特殊符号表单词代码123456789单词intcharfloatvoidconstforifelsethen单词助记符intcharfloatvoidconstforifelsethen内码值-单词代码101112131415161718单词whileswitchbreakbeginend标识符数字(包括整数和实数)单词助记符whileswitchbreakbeginendid num内码值-在符号表中的位置在常数表中的位置单词代码192021222324252627单词+-*/%()单词助记符+-*/%()内码值-单词代码28282930单词!=<><=>=;单词助记符rlop;内码值!=<><=>=-单词代码373839404142434445单词/=+=-=*=%=|&&!=单词助记符/=+=-=*=%=orandnot=内码值-单词的构词规则:字母=A-Za-z数字=0-9标识符=(字母|_)(字母|数字)*数字=数字(数字)*(.数字+|e)四、S语言表达式和语句说明1算术表达式:+、-、*、/、%2关系运算符:>、>=、<、<=、=、!=3赋值运算符:=,+=、-=、*=、/=、%=4变量说明:类型标识符 变量名表;5类型标识符:int char float6If语句:if 表达式then 语句 else 语句7For语句:for(表达式1;表达式2;表达式3) 语句8While语句:while 表达式 do 语句9S语言程序:由函数构成,函数不能嵌套定义。函数格式为:返回值 函数名(参数) 数据说明 语句五、程序参考结构说明1Initscanner函数:程序初始化:输入并打开源程序文件和目标程序文件,初始化保留字表2Scanner函数:若文件未结束,反复调用lexscan函数识别单词。3Lexscan函数:根据读入的单词的第一个字符确定调用不同的单词识别函数4Isalpha函数:识别保留字和标识符5Isnumber函数:识别整数,如有精力,可加入识别实数部分工功能6Isanotation函数:处理除号/和注释7Isother函数识别其他特殊字符8Output函数:输出单词的二元式到目标文件,输出格式(单词助记符,单词内码值),如(int,-)(rlop,>)9Error函数:输出错误信息到屏幕10除此之外,还可以设置查符号表,填写符号表等函数,学生可自行设计。实验中,可以将某些类型的单词识别过程利用DFA算法实现,DFA算法参考如下:DFA(S=S0,MOVE,F,ALPHABET,ALLS)/*S为状态,初值为DFA的初态,MOVE为状态转换矩阵,F 为终态集,ALPHABET 为字母表,其中的字母顺序与MOVE 中列标题的字母顺序一致。ALLS为状态集*/Char Wordbuffer10=“”/单词缓冲区置空Nextchar=getchar();/读字符i=0;while(nextchar!=NULL)/NULL代表此类单词 if (nextchar!ALPHABET) ERROR(“非法字符”),return(“非法字符”); S=MOVESnextchar /下一状态 if(S=NULL)return(“不接受”);/下一状态为空,不能识别,单词错误 wordbufferi=nextchar ; /保存单词符号 i+; nextchar=getchar();Wordbufferi=0;If(SF)return(wordbuffer); /接受 Else return(“不接受”);六、实验过程说明1每人单独完成。2完成后,由老师验收,并给出成绩。3实验完成后,写出实验报告(要求交打印稿)。报告内容要求如下:完成人:班级、学号、姓名一、实验名称:简化S语言词法分析器二、实验目的:通过手工编写简化C语言词法分析器,熟悉并深入理解编译程序词法分析器的工作原理。三、实验内容:1根据保留字和特殊符号表能区分出源文件中的保留字、普通标识符和特殊符号,并能进行简单的错误处理。2设计词法分析器模块调用结构图和各模块流程图。3程序源代码。4程序的执行结果:输入文件,输出结果文件及屏幕信息。四、实验中出现的问题及解决方法。五、体会、意见或建议。七、测试源程序示例:/aa.cvoid aa( )float rate,circle; rate=3;circle=3.14*rate*rate;输出结果:(1)输出结果文件:(void,-)(id, 0) (,-) (),-) (,- ) ( float,-) (id,1)(2)标识符的符号表: Name type address0aa1rate2circle345(3)常数表: Name value 0313.1423实验地点:教10五楼计算机学院软一机房实验时间:第4周周二3单元、周四1单元、第五周周二3单元八、实验代码#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>#define MAX 10/保留字符号长度#define MAXSIZE 45/保留字和特殊符号表长度#define NUM 30/标识符和常数的个数FILE *in,*out;/ 指向文件的指针char infileMAX;/文件名字char tokenMAX;char WordNUMMAX;/标识符char constsNUMMAX;/常数char lownumNUMMAX="0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19""20","21","22","23","24","25","26","27","28","29" /自定义表int n=1;/行号int num=0; /错误统计数typedef struct Key_word/保留字和特殊符号表结构/int keyNum;char keyWordMAX;char keySignMAX;char keyValueMAX;Key_word;/初始化保留字表Key_word KeyMAXSIZE="int","int","-","char","char","-","float","float","-","void","void","-","const","const","-","for","for","-","if","if","-","else","else","-","then","then","-","while","while","-","switch","switch","-","break","break","-","begin","begin","-","end","end","-","","","","","","","","id","","","num","","+","+","-","-","-","-","*","*","-","/","/","-","%","%","-","(","(","-",")",")","-","","","-","","","-","","","","<","rlop","<",">","rlop",">","<=","rlop","<=",">=","rlop",">=","=","rlop","=","!=","rlop","!=","","","-","","","","/=","/=","-","+=","+=","-","-=","-=","-","*=","*=","-","%=","%=","-","|","or","-","&&","and","-","!","not","-","=","=","-"/打开关闭文件int Initscanner()printf("请输入要输入的文件:n");scanf("%s",infile); if(in=fopen(infile,"r")=NULL)printf("cannot open infile!n");return 0;if(out=fopen("word.txt","a+")=NULL)printf("cannot open outfile!n");return 0;return 0;/写入文件void Output(int n,int m)fputs("(",out);fputs(Keyn.keySign,out);fputs(",",out);if(m=-1)fputs(Keyn.keyValue,out);elsefputs(lownumm,out);/下标fputs(")",out);/Isalpha函数:识别保留字和标识符int Isalpha()int i;for(i=0;i<16;i+)/关键字if(strcmp(token,Keyi.keyWord)=0)Output(i,-1);return 0;/标识符WordNUMMAXfor(i=0;i<NUM;i+)if(Wordi0!=NULL)&&strcmp(token,Wordi)=0)Output(16,i);return 0;if(Wordi0=NULL)strcpy(Wordi,token);Output(16,i);return 0;return 0;int Isnumber()/Isnumber函数:constsNUMMAXint i;for(i=0;i<NUM;i+)if(constsi0!=NULL)&&strcmp(token,constsi)=0)Output(17,i);return 0;if(constsi0=NULL)strcpy(constsi,token);Output(17,i);return 0;return 0;void remove() /扫描指针回退一个字符 fseek(in,-1,SEEK_CUR);/Isanotation函数:处理除号/和注释void Isanotation() /提示第一个字符已经存为/了char ch,pre;ch=getc(in);if(ch='=')/ "/="Output(36,-1);else if(ch='*')/ "/*"ch=getc(in);doif(feof(in)num+;printf("n第(%d)行 : 注释错误: /*后面的字符全部当做注释,缺少结束注释*/n",n);break;pre=ch;ch=getc(in);while(pre!='*'|ch!='/');else/ "/"remove(); /指针回退一个字符 Output(21,-1);/Isother函数识别其他特殊字符void Isother()int i=1;int j=0;char ch;if(token0='<'|token0='>'|token0='!'|token0='%') ch=getc(in);if(ch='=')tokeni+=ch;else remove(); elseif(token0='|') ch=getc(in);if(ch='|')tokeni+=ch;elseremove(); elseif(token0='&') ch=getc(in);if(ch='&')tokeni+=ch;else remove();tokeni='0'for(i=18;i<45;i+)if(strcmp(token,Keyi.keyWord)=0)Output(i,-1);break;if(i=45)num+;printf("n第(%d)行 : 字符错误 : %s字符非法n",n,token);void Scanner()char ch;int i;ch=getc(in);while(!feof(in)while(ch=' '|ch='t'|ch='n') /先统计行数if(ch='n')+n;ch=getc(in);if(ch>='a')&&(ch<='z')|(ch='_')/标识符 isalpha(ch)单词i=1;token0=ch;ch=getc(in);while(ch>='a')&&(ch<='z')|(ch>='0')&&(ch<='9')/word numbertokeni+=ch;ch=getc(in);tokeni='0'Isalpha();else if(ch>='0')&&(ch<='9')/整数i=1;token0=ch;ch=getc(in);while(isdigit(ch)tokeni+=ch;ch=getc(in);tokeni='0'Isnumber();else if(ch='/')/区分 "/" , "/=" , "/*" token0=ch;Isanotation();ch=getc(in);else /特殊字符token0=ch; Isother(); ch=getc(in);void show()int i;char ch;printf("n二元式:n");rewind(out);while(!feof(out)printf("%c",getc(out);printf("n标志符表n");for(i=0;i<NUM;i+)if(Wordi0!=NULL)printf("%d,%sn",i,Wordi);printf("n常数表n");for(i=0;i<NUM;i+)if(constsi0!=NULL)printf("%d,%sn",i,constsi);void main()Initscanner();Scanner();printf("n共有%d个错误n",num);show();fclose(in);fclose(out);九、实验结果输入文件void aa( )beginfloat rate,circle; rate=3;circle=3.14*rate;/*/end输出文件(void,-)(id,0)(,-)(),-)(begin,-)(float,-)(id,1)(id,2)(;,-)(id,1)(rlop,=)(num,0)(;,-)(id,2)(rlop,=)(num,0)(num,1)(*,-)(id,1)(;,-)(end,-)十、实验总结经过此次实验,了解了程序的词法构词规则和词法分析过程。在实验过程中,得到了同学的帮助才得以完成。本实验基本达到了老师的要求,实现了词法分析的基本功能。B14511 2014405A114 王自达