北京科技大学编译原理实验报告

合集下载

编译原理 实验报告

编译原理 实验报告

编译原理实验报告编译原理实验报告引言编译原理是计算机科学中的重要课程,它研究的是如何将高级语言程序转化为机器语言程序的过程。

在本次实验中,我们学习了编译原理的基本概念和技术,并通过实践来加深对这些概念和技术的理解。

本报告将对我们在实验中遇到的问题、解决方案以及实验结果进行总结和分析。

实验目的本次实验的主要目的是设计并实现一个简单的编译器,能够将类C语言的源代码翻译成目标代码。

通过这个实验,我们可以更好地理解编译器的工作原理,掌握编译器设计的基本方法和技术。

实验过程在实验中,我们首先对给定的类C语言的语法进行了分析,并根据语法规则设计了相应的语法分析器。

然后,我们使用了自顶向下的递归下降分析法来实现语法分析器。

在实现语法分析器的过程中,我们遇到了一些问题,例如如何处理语法规则中的左递归、如何处理语法规则中的优先级和结合性等。

通过仔细研究相关的文献和资料,我们成功地解决了这些问题,并完成了语法分析器的设计和实现。

接下来,我们对语法分析器进行了测试,并对测试结果进行了分析。

通过测试,我们发现语法分析器在处理简单的源代码时能够正确地识别出语法错误,并给出相应的错误提示。

然而,在处理复杂的源代码时,语法分析器可能会出现一些错误,例如无法正确地处理嵌套的语法结构、无法正确地处理运算符的优先级和结合性等。

为了解决这些问题,我们对语法分析器进行了改进,并进行了多次测试,最终得到了令人满意的结果。

实验结果通过本次实验,我们成功地设计并实现了一个简单的编译器,能够将类C语言的源代码翻译成目标代码。

在实验中,我们对编译器的工作原理有了更深入的了解,掌握了编译器设计的基本方法和技术。

同时,我们也发现了一些问题,并通过不断地改进和测试,最终得到了令人满意的结果。

结论编译原理是一门重要的计算机科学课程,它研究的是如何将高级语言程序转化为机器语言程序的过程。

通过本次实验,我们对编译原理的基本概念和技术有了更深入的了解,并通过实践来加深了对这些概念和技术的理解。

编译原理实验报告

编译原理实验报告

编译原理实验报告一、实验目的和要求本次实验旨在对PL_0语言进行功能扩充,添加新的语法特性,进一步提高编译器的功能和实用性。

具体要求如下:1.扩展PL_0语言的语法规则,添加新的语法特性;2.实现对新语法的词法分析和语法分析功能;3.对扩展语法规则进行语义分析,并生成中间代码;4.验证扩展功能的正确性。

二、实验内容1.扩展语法规则本次实验选择扩展PL_0语言的语句部分,添加新的控制语句,switch语句。

其语法规则如下:<switch_stmt> -> SWITCH <expression> CASE <case_list><default_stmt> ENDSWITCH<case_list> -> <case_stmt> , <case_stmt> <case_list><case_stmt> -> CASE <constant> : <statement><default_stmt> -> DEFAULT : <statement> ,ε2.词法分析和语法分析根据扩展的语法规则,需要对新的关键字和符号进行词法分析,识别出符号类型和记号类型。

然后进行语法分析,建立语法树。

3.语义分析在语义分析阶段,首先对switch语句的表达式进行求值,判断其类型是否为整型。

然后对case语句和default语句中的常量进行求值,判断是否与表达式的值相等。

最后将语句部分生成中间代码。

4.中间代码生成根据语法树和语义分析的结果,生成对应的中间代码。

例如,生成switch语句的跳转表,根据表达式的值选择相应的跳转目标。

5.验证功能的正确性设计一些测试用例,验证新语法的正确性和扩展功能的实用性。

三、实验步骤与结果1.扩展语法规则,更新PL_0语法分析器的词法规则和语法规则。

编译原理实验报告

编译原理实验报告

编译原理实验报告实验⼀词法分析⼀、实验⽬的设计、编制并调试⼀个词法分析程序,加深对词法分析原理的理解。

⼆、实验要求2.1 待分析的简单的词法(1)关键字:begin if then while do end所有的关键字都是⼩写。

(2)运算符和界符:= + - * / < <= <> > >= = ; ( ) #(3)其他单词是标识符(ID)和整型常数(SUM),通过以下正规式定义:ID = letter (letter | digit)*NUM = digit digit*(4)空格有空⽩、制表符和换⾏符组成。

空格⼀般⽤来分隔ID、SUM、运算符、界符和关键字,词法分析阶段通常被忽略。

2.2 各种单词符号对应的种别码:表2.1 各种单词符号对应的种别码单词符号种别码单词符号种别码bgin 1 :17If 2 := 18Then 3 < 20wile 4 <> 21do 5 <= 22end 6 > 23lettet(letter|digit)* 10 >= 24 dight dight* 11 = 25 + 13 ;26—14 ( 27* 15 ) 28/ 16 # 02.3 词法分析程序的功能:输⼊:所给⽂法的源程序字符串。

输出:⼆元组(syn,token或sum)构成的序列。

其中:syn为单词种别码;token为存放的单词⾃⾝字符串;(1,begin)(10,x)(18,:=)(11,9)(26,;)(2,if)……三、词法分析程序的算法思想:算法的基本任务是从字符串表⽰的源程序中识别出具有独⽴意义的单词符号,其基本思想是根据扫描到单词符号的第⼀个字符的种类,拼出相应的单词符号。

3.1 主程序⽰意图:主程序⽰意图如图3-1所⽰。

其中初始包括以下两个⽅⾯:⑴关键字表的初值。

关键字作为特殊标识符处理,把它们预先安排在⼀张表格中(称为关键字表),当扫描程序识别出标识符时,查关键字表。

编译原理实验报告

编译原理实验报告

编译原理实验报告一、实验目的本次编译原理实验的主要目的是通过实践加深对编译原理中词法分析、语法分析、语义分析和代码生成等关键环节的理解,并提高实际动手能力和问题解决能力。

二、实验环境本次实验使用的编程语言为 C/C++,开发工具为 Visual Studio 2019,操作系统为 Windows 10。

三、实验内容(一)词法分析器的设计与实现词法分析是编译过程的第一个阶段,其任务是从输入的源程序中识别出一个个具有独立意义的单词符号。

在本次实验中,我们使用有限自动机的理论来设计词法分析器。

首先,我们定义了单词的种类,包括关键字、标识符、常量、运算符和分隔符等。

然后,根据这些定义,构建了相应的状态转换图,并将其转换为程序代码。

在实现过程中,我们使用了字符扫描和状态转移的方法,逐步读取输入的字符,判断其所属的单词类型,并将其输出。

(二)语法分析器的设计与实现语法分析是编译过程的核心环节之一,其任务是在词法分析的基础上,根据给定的语法规则,判断输入的单词序列是否构成一个合法的句子。

在本次实验中,我们采用了自顶向下的递归下降分析法来实现语法分析器。

首先,我们根据给定的语法规则,编写了相应的递归函数。

每个函数对应一种语法结构,通过对输入单词的判断和递归调用,来确定语法的正确性。

在实现过程中,我们遇到了一些语法歧义的问题,通过仔细分析语法规则和调整函数的实现逻辑,最终解决了这些问题。

(三)语义分析与中间代码生成语义分析的任务是对语法分析所产生的语法树进行语义检查,并生成中间代码。

在本次实验中,我们使用了四元式作为中间代码的表示形式。

在语义分析过程中,我们检查了变量的定义和使用是否合法,类型是否匹配等问题。

同时,根据语法树的结构,生成相应的四元式中间代码。

(四)代码优化代码优化的目的是提高生成代码的质量和效率。

在本次实验中,我们实现了一些基本的代码优化算法,如常量折叠、公共子表达式消除等。

通过对中间代码进行分析和转换,减少了代码的冗余和计算量,提高了代码的执行效率。

实验报告编译原理

实验报告编译原理

北京科技大学计算机与通信工程学院实验报告实验名称:_________编译原理实验报告_______学生姓名:_________ ________________专业:_________ _________________班级:_________ _________________学号:_________________________指导教师:_________ _________________实验成绩:________________________________实验地点:________________________________实验时间:____2015___年___07__月_______日一、实验目的与实验要求1、实验目的通过一个词法分析程序将一段给出的C语言代码的词法部分分析出来;通过一个语法分析程序将一段给出的C语言代码的语法分析出来。

2、实验要求二、实验设备(环境)及要求Win7家庭普通版;Visual Studio 2013;三、实验内容与步骤1、实验1(1)实验内容对于给定一段代码,通过词法分析程序将程序中的各个类型的字表分析,包括保留字、分隔符表、运算符表、标识符、整型数、浮点类型数、字符类型、字符串类型等表文件。

(2)主要步骤通过读入程序的文本文件,然后将分析的结果一次保存在本地文本文件。

然后通过指定的命令来将分析的结果读取出来。

附录:源码#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", "struct", "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("******************** 词法分析 ********************\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();}}2、实验2(1)实验内容输入单词串,以”#”结束,如果是文法正确的句子,则输出成功信息,打印“success”,否则输出”error”例如:输入: begin a:=9;b:=0 end #输出: success输入: begin a=9 end #输出: error(2)主要步骤附录:源码#include<stdio.h>#include<string.h>#include<ctype.h>#include<stdlib.h>char GetChar(char *input, int *index, int length);int ClearBlank(char *input, int(*index), int length);int reserve(char *s);void lrparser(char *input, int inputLength, int *index);void yucu(char *input, int inputLength, int *index);void factor(char *input, int inputLength, int *index);void statement(char *input, int inputLength, int *index);void expression(char *input, int inputLength, int *index);void term(char *input, int inputLength, int *index);char *retab[6] = { "begin", "if", "then", "while", "do", "end" };//关键字int syn = 0;int myIsAlpha(char ch){if (islower(ch) == 2 || isupper(ch) == 1){return 1;}else{return 0;}}void scaner(char *input, int inputLength, int *index){char s[256] = ""; //保存当前的字符char ch = GetChar(input, index, inputLength);int nowPosition = 0;int j = 0;if (myIsAlpha(ch) == 1) //如果是字母{while(((ch >= '0'&& ch <= '9') || (myIsAlpha(ch) == 1)) && *index<= inputLength) {s[nowPosition] = ch; //添加到当前字符串中nowPosition++;ch = GetChar(input, index, inputLength);}if ((ch <'0' || ch>'9') && (myIsAlpha(ch) == 0))//进行回退操作,并输出结果{s[nowPosition] = '\0';//添加结束标志j = reserve(s);if (j == 0){syn = 10;}else{syn = j;}(*index)--;return;}else//超过范围{s[nowPosition++] = ch;s[nowPosition] = '\0';//添加结束标志j = reserve(s);if (j == 0){syn = 10;}else{syn = j;}getchar();exit(0);return;}}elseif (ch >= '0' && ch <= '9') //如果是数字{while (ch >= '0' && ch <= '9'&& *index <= inputLength) {s[nowPosition] = ch; //添加到当前字符串中nowPosition++;ch = GetChar(input, index, inputLength);}if (ch<'0' || ch>'9')//进行回退操作{(*index)--;syn = 11;return;}else//超过范围时{s[nowPosition] = ch;syn = 11;return;}}else{switch (ch){case'+':{syn = 13;return;}case'-':{syn = 14;return;}case'*':{syn = 15;return;}case'/':{syn = 16;return;}case'<':{ch = GetChar(input, index, inputLength);if (ch == '='){syn = 22;return;}elseif (ch == '>'){syn = 21;return;}else{syn = 20;if (*index>inputLength){return;}else{(*index)--;return;}}case'>':{ch = GetChar(input, index, inputLength);if (ch == '='){syn = 24;return;}else{syn = 23;if (*index>inputLength){return;}else{(*index)--;return;}}}case':':{ch = GetChar(input, index, inputLength);if (ch == '='){syn = 18;return;}else{if (*index>inputLength){return;}else(*index)--;return;}}}case'=':{syn = 25;return;}case';':{syn = 26;return;}case'(':{syn = 27;return;}case')':{syn = 28;return;}case'#':{syn = 0;return;}case' ':{syn = -1;return;}default:{printf("(非法符号)");。

编译原理实验报告

编译原理实验报告

编译原理实验报告一、实验概述本次实验旨在设计并实现一个简单的词法分析器,即实现编译器的第一个阶段,词法分析。

词法分析器将一段源程序代码作为输入,将其划分为一个个的词法单元,并将其作为输出。

二、实验过程1.设计词法规则根据编程语言的规范和所需实现的功能,设计词法规则,以明确规定如何将源程序代码分解为一系列的词法单元。

2.实现词法分析器采用合适的编程语言,根据所设计的词法规则,实现词法分析器。

词法分析器的主要任务是读入源程序代码,并将其根据词法规则进行分解,生成对应的词法单元。

3.测试词法分析器设计测试用例,用于检验词法分析器的正确性和性能。

测试用例应包含各种情况下的源程序代码。

4.分析和修正错误根据测试过程中发现的问题,分析产生错误的原因,并进行修正。

重复测试和修正的过程,直到词法分析器能够正确处理所有测试用例。

三、实验结果我们设计了一个简单的词法分析器,并进行了测试。

测试用例涵盖了各种情况下的源程序代码,包括正确的代码和错误的代码。

经过测试,词法分析器能够正确处理所有的测试用例。

词法分析器将源程序代码分解为一系列的词法单元,每个词法单元包含了单词的种类和对应的值。

通过对词法单元的分析,可以进一步进行语法分析和语义分析,从而完成编译过程。

四、实验总结通过本次实验,我深入了解了编译原理的词法分析阶段。

词法分析是编译器的第一个重要阶段,它将源程序代码分解为一个个的词法单元,为后续的语法分析和语义分析提供基础。

在实现词法分析器的过程中,我学会了如何根据词法规则设计词法分析器的算法,并使用编程语言实现词法分析器。

通过测试和修正,我掌握了调试和错误修复的技巧。

本次实验的经验对我今后的编程工作有很大帮助。

编译原理是计算机科学与技术专业的核心课程之一,通过实践能够更好地理解和掌握其中的概念和技术。

我相信通过进一步的学习和实践,我能够在编译原理领域取得更大的成果。

编译原理实验报告

编译原理实验报告

编译原理实验报告一、实验目的编译原理是计算机科学中的重要学科,它涉及到将高级编程语言转换为计算机能够理解和执行的机器语言。

本次实验的目的是通过实际操作和编程实践,深入理解编译原理中的词法分析、语法分析、语义分析以及中间代码生成等关键环节,提高我们对编译过程的认识和编程能力。

二、实验环境本次实验使用的编程语言为C++,开发环境为Visual Studio 2019。

此外,还使用了一些相关的编译工具和调试工具,如 GDB 等。

三、实验内容(一)词法分析器的实现词法分析是编译过程的第一步,其任务是将输入的源程序分解为一个个单词符号。

在本次实验中,我们使用有限自动机的理论来设计和实现词法分析器。

首先,定义了各种单词符号的类别,如标识符、关键字、常量、运算符等。

然后,根据这些类别设计了相应的状态转换图,并将其转换为代码实现。

在实现过程中,使用了正则表达式来匹配输入字符串中的单词符号。

对于标识符和常量等需要进一步处理的单词符号,使用了相应的规则进行解析和转换。

(二)语法分析器的实现语法分析是编译过程的核心环节之一,其任务是根据给定的语法规则,分析输入的单词符号序列是否符合语法结构。

在本次实验中,我们使用了递归下降的语法分析方法。

首先,根据实验要求定义了语法规则,并将其转换为相应的递归函数。

在递归函数中,通过对输入单词符号的判断和处理,逐步分析语法结构。

为了处理语法错误,在分析过程中添加了错误检测和处理机制。

当遇到不符合语法规则的输入时,能够输出相应的错误信息,并尝试进行恢复。

(三)语义分析及中间代码生成语义分析的目的是对语法分析得到的语法树进行语义检查和语义处理,生成中间代码。

在本次实验中,我们使用了三地址码作为中间代码的表示形式。

在语义分析过程中,对变量的定义和使用、表达式的计算、控制流语句等进行了语义检查和处理。

对于符合语义规则的语法结构,生成相应的三地址码指令。

四、实验步骤(一)词法分析器的实现步骤1、定义单词符号的类别和对应的正则表达式。

编译原理实验报告

编译原理实验报告

编译原理实验报告编译原理实验报告一、实验目的1. 了解编译器的基本原理和工作过程;2. 掌握编译器设计和实现的基本方法和技巧;3. 通过设计和实现一个简单的编译器,加深对编程语言和计算机系统的理解和认识。

二、实验原理编译器是将高级语言程序翻译成机器语言程序的一种软件工具。

它由编译程序、汇编程序、链接程序等几个阶段组成。

本次实验主要涉及到的是编译程序的设计和实现。

编译程序的基本原理是将高级语言程序转换为中间代码,再将中间代码转换为目标代码。

整个过程可以分为词法分析、语法分析、语义分析、代码生成和代码优化几个阶段。

三、实验内容本次实验的设计目标是实现一个简单的四则运算表达式的编译器。

1. 词法分析根据规定的语法规则,编写正则表达式将输入的字符串进行词法分析,将输入的四则运算表达式划分成若干个单词(Token),例如:运算符、操作数等。

2. 语法分析根据定义的语法规则,编写语法分析程序,将词法分析得到的Token序列还原成语法结构,构建抽象语法树(AST)。

3. 语义分析对AST进行遍历,进行语义分析,判断表达式是否符合语法规则,检查语义错误并给出相应的提示。

4. 代码生成根据AST生成目标代码,目标代码可以是汇编代码或者机器码。

四、实验过程和结果1. 首先,根据输入的表达式,进行词法分析。

根据所定义的正则表达式,将输入的字符串划分成Token序列。

例如:输入表达式“2+3”,经过词法分析得到的Token序列为["2", "+", "3"]。

2. 然后,根据语法规则,进行语法分析。

根据输入的Token序列,构建抽象语法树。

3. 接着,对抽象语法树进行语义分析。

检查表达式是否符合语法规则,给出相应的提示。

4. 最后,根据抽象语法树生成目标代码。

根据目标代码的要求,生成汇编代码或者机器码。

五、实验总结通过本次实验,我对编译器的工作原理有了更深入的认识,掌握了编译器设计和实现的基本方法和技巧。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

编译原理实验报告学院: 计算机与通信工程学院专业: 计算机科学与技术班级:学号:姓名:实验成绩:词法分析一、实验目的设计、编制并调试一个词法分析程序,加深对词法分析原理的理解。

二、实验要求2.1 待分析的简单的词法(1)关键字:begin if then while do end所有的关键字都是小写。

(2)运算符和界符:= + - * / < <= <> > >= = ; ( ) #(3)其他单词是标识符(ID)和整型常数(SUM),通过以下正规式定义:ID = letter (letter | digit)*NUM = digit digit*(4)空格有空白、制表符和换行符组成。

空格一般用来分隔ID、SUM、运算符、界符和关键字,词法分析阶段通常被忽略。

2.2 各种单词符号对应的种别码:输入:所给文法的源程序字符串。

输出:二元组(syn,token或sum)构成的序列。

其中:syn为单词种别码;token为存放的单词自身字符串;sum为整型常数。

例如:对源程序begin x:=9: if x>9 then x:=2*x+1/3; end #的源文件,经过词法分析后输出如下序列:(1,begin)(10,x)(18,:=)(11,9)(26,;)(2,if)……三、词法分析程序的算法思想:算法的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。

3.1 主程序示意图:3.2词法分析程序流程图:四、词法分析程序的C++语言程序源代码:#include"stdio.h" #include"stdlib.h" #include"string.h"#define _KEY_WORD_END "waiting for your expanding" typedef struct开始 变量初始化是否文件结束?返回拼数Syn=11返回拼字符串是否是关键字?Syn 为对应关键字的单词种别码Syn=10给不同的符号相同的 Syn 值报错是否数字字母是否运算符, 界符等其他{int typenum;char * word;}WORD;char input[255];char token[255]="";int p_input;int p_token;char ch;char * rwtab[]={"begin","if","then","while","do","end",_KEY_WORD_END}; WORD * scaner();int main(){int over=1;WORD* oneword=new WORD;printf(" 输入源程序(以#结束):");scanf("%[^#]s",input);p_input=0;printf("Your words:\n%s\n",input);while(over<1000&&over!=-1){oneword=scaner();if(oneword->typenum<1000)printf("(%d,%s)\n",oneword->typenum,oneword->word);over=oneword->typenum;}printf("\npress # to exit:");scanf("%[^#]s",input);return 0;}char m_getch(){ch=input[p_input];p_input=p_input+1;return(ch);}void getbc(){while(ch==' '||ch==10){ch=input[p_input];p_input=p_input+1;}}void concat(){token[p_token]=ch;p_token=p_token+1;token[p_token]='\0';}int letter(){if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')return 1;elsereturn 0;}int digit(){if(ch>='0'&&ch<='9')return 1;elsereturn 0;}int reserve(){int i=0;while(strcmp(rwtab[i],_KEY_WORD_END)){if(!strcmp(rwtab[i],token)){return i+1;}i=i+1;}return 10;}void retract(){p_input=p_input-1;}char * dtp(){return NULL;}WORD *scaner(){WORD *myword=new WORD;myword->typenum=10;myword->word=" ";p_token=0;m_getch();getbc();if(letter()){while(letter()||digit()){concat();m_getch();}retract();myword->typenum=reserve();myword->word=token;return(myword);}else if(digit()){while(digit()){concat();m_getch();}retract();myword->typenum=20;myword->word=token;return(myword);}else switch(ch){case'=': m_getch();if(ch=='='){myword->typenum=39;myword->word="==";return(myword);}retract();myword->typenum=21;myword->word="=";break;case'+': myword->typenum=22;myword->word="+";return(myword);break;case'-': myword->typenum=23;myword->word="-";return(myword);break;case'*': myword->typenum=24;myword->word="*";return(myword);break;case'/': myword->typenum=25;myword->word="/";return(myword);break;case'(': myword->typenum=26;myword->word="(";return(myword);break;case')': myword->typenum=27;myword->word=")";return(myword);break;case'[': myword->typenum=28;myword->word="[";return(myword);break;case']': myword->typenum=29;myword->word="]";return(myword);break;case'{': myword->typenum=30;myword->word="{";return(myword);break;case'}': myword->typenum=31;myword->word="}";return(myword);break;case',': myword->typenum=32;myword->word=",";break;case':': myword->typenum=33;myword->word=":";return(myword);break;case';': myword->typenum=34;myword->word=";";return(myword);break;case'>': m_getch();if(ch=='='){myword->typenum=37;myword->word=">=";return(myword);}retract();myword->typenum=35;myword->word=">";return(myword);break;case'<': m_getch();if(ch=='='){myword->typenum=38;myword->word="<=";return(myword);}retract();myword->typenum=36;myword->word="<";return(myword);break;case'!': m_getch();if(ch=='='){myword->typenum=40;myword->word="!=";return(myword);}retract();myword->typenum=-1;myword->word="ERROR";break;case'\0': myword->typenum=1000;myword->word="OVER";return(myword);break;default: myword->typenum=-1;myword->word="ERROR";return(myword);}}五、结果分析:输入begin x:=9: if x>9 then x:=2*x+1/3; end # 后如图所示:六、总结:词法分析的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。

相关文档
最新文档