一、实验目的 通过上机实习,加深对语法制导翻译原理的理解,掌握将语法分析所识别的语法成分变换为中间代码的语义翻译方法。 二、实验要求 采用递归下降语法制导翻译法,对算术表达式、赋值语句进行语义分析并生成四元式序列。 三、实验的结果验证 1.输入是语法分析后提供的正确的单词串,输出为三地址指令形式的四元式序列。 给出语句串: begin_a:=2+3*4; _x:=(a+b)/c_end# 输出如下三地址指令: (1) t1=3*4 (2) t2=2+t1 (3) a=t2 (4) t3=a+b (5) t4=t3/c (6) x=t4 2.自己任意给出一语句串,输出四元式序列。 要求:将上述2个语句串的执行结果显示保留在实验报告中(拷屏)。 四、算法思想 1、设置语义过程。 (1)emit(char *result,char *ag1,char *op,char *ag2) 该函数的功能是生成一个三地址语句送到四元式表中。 四元式表的结构如下: struct{char result[8]; char ag1[8]; char op[8]; char ag2[8]; }quad[20]; (2) char *newtemp() 该函数回送一个新的临时变量名,临时变量名产生的顺序为T1,T2,… char *newtemp(void) {char *p; char m[8]; p=(char *)malloc(8); k++; itoa(k,m,10); strcpy(p+1,m); p[0]=’t’; return(p); } 2、函数lrparser 在原来语法分析的基础上插入相应的语义动作:将输入串翻译成四元式序列。在实验中我们只对表达式、赋值语句进行翻译。 五、语义分析程序的C程序。 #include #include #include #include struct {char result[12]; char ag1[12]; char op[12]; char ag2[12]; }quad; char prog[80],token[12]; char ch; int syn,p,m=0,n,sum=0,kk; //p是缓冲区prog的指针,m是token的指针 char *rwtab[6]={"begin","if","then","while","do","end"}; void scaner(); char *factor(void); char *term(void); char *expression(void); int yucu(); void emit(char *result,char *ag1,char *op,char *ag2); char *newtemp(); int statement(); int k=0; void emit(char *result,char *ag1,char *op,char *ag2) { strcpy(quad.result,result); strcpy(参数设置); strcpy(quad.op,op); strcpy(quad.ag2,ag2); cout<
1
编译原理实验,超完整版本,一整套五个实验
2021-11-16 15:43:10 787KB 编译原理 实验报告 试验程序
1
这是鄙人完成老师的一个实验作业,写的还不错已比较详细,且代码中有大量注释帮助理解。使用时打开vc,在文件里选择打开工作区间,打开文件里的test2.dsw即可使用。里面的大量过程结果被鄙人注释掉了,打开1.cpp里的注释即可看到过程的结果。
1
本课程中所实现的程序为普通C或C++程序,在Windows环境下,属于控制台应用程序。
1
编译原理实验.rar Flex自动工具词法分析器设计步骤.pdf Tiny语言词法分析器设计过程.pdf Tiny语言代码生成器设计过程.pdf Tiny语言语法分析器设计过程.pdf Tiny语言语义分析器设计过程.pdf Yacc语法分析器设计步骤.pdf 编译原理实验指导书.pdf
2021-11-11 11:47:15 8.56MB 编译原理实验
1
实验一:C++源代码单词扫描程序(词法分析) 功能: (1)C++源代码扫描程序识别C++记号。 C++语言包含了几种类型的记号:标识符,关键字,数(包括整数、浮点数),字符串、注释、特殊符号(分界符)和运算符号等。 (2)打开一个C++源文件,打印出所有以上的记号。 (3)要求应用程序应为Windows界面。 (4)选作部分:为了提高C++源程序的可读性,C++程序在书写过程中加入了空行、空格、缩进、注释等。假设你想牺牲可读性,以节省磁盘空间,那么你可以存贮一个删除了所有不必要空格和注释的C++源程序的压缩文本。因此,程序中还应该有这样的压缩功能。 (5)选作部分:进一步思考或实现——如何进一步实现减小源文件大小的压缩功能。 (6)应该书写完善的软件文档。
2021-11-09 22:34:41 12KB 编译原理 实验一 词法分析 C++
1
编译原理实验,词法分析,LL1自顶向下的递归分析,LL1文法自动构造预测分析表、消除左递归、提取公共左因子以及预测分析,功能比较完善,有什么bug欢迎指正,Main文件中有几个测试案例,里面打开的文件都是工程文件夹的中的用txt形式保存的文法。几个具体的实验代码在哪个位置可能有点乱....自己找吧
2021-11-07 21:07:51 60.81MB 编译原理
1
编译原理实验 C语言 预测分析方法 预测分析 预测分析
2021-11-06 16:50:06 9KB 编译原理 C语言 预测分析
1
实验二:表达式计算器的语法、语义分析器的设计与实现(输出四元式的中间结果和最终的运算结果)。 在实验一词法分析的基础上,以词法分析输出结果(单词串或者成为多元式序列)作为该语法语义分析器的输入,最后输出中间代码四元式序列,并计算出表达式最后的结果。(共8个上机学时,时间不够的请自己课下找时间补完) 实现方法上,建议大家采用算符优先分析法或者LR分析方法,进行语法制导翻译。先根据上述文法中的“表达式定义”构造算符优先关系表或者LR分析表进行语法分析。 检查要求: e) 启动程序后,先输出作者姓名、班级、学号(可用汉语、英语或拼音)。 f) 请求输入测试程序名,键入程序名后自动开始编译。 g) 输出四元式中间代码(样式见样板输出3)。 h) 能发现程序的语法错误并输出出错信息(样式见样板输出4)。
2021-11-06 15:00:31 31KB 语义分析器
1
Compiler Construction Experiment 1 Implementing a Scanner for TINY+ You are to write a lexical analyzer/scanner for the language TINY+. Goals 1The input of the scanner is a source code file and the output of the scanner is a stream of tokens. 2Your scanner should go for longest possible match i.e. a string ‘:=’ is to be identified as ‘ass-symbol’ rather than ‘:’ and ‘=’. 3Token is represented as (Kind, Value). We use the following symbols to denote different kinds of tokens KEY denotes reserved words SYM denotes special symbols ID denotes identifiers NUM denotes numeric constants STR denotes string constants 4Check lexical errors: giving meaning error messages and the lines where errors occur. The kinds of lexical errors are: Illegal character, that is, scanner may recognize a character that is not in the alphabet of TINY+, such as $ is an illegal character The right bracket of a STRING is lost, such as ' scanner The right delimiter of a comment is lost, such as: {this is an example Requirements 1Write your program in C or C++ 2This experiment must be finished in 4 periods. You will submit a report and the source code Example output for some TINY+ programs Test1 or and int bool char while do if then else end repeat until read write , ; := + - * / ( ) = a2c 123 'EFG' The scanner should give the outputs: (KEY, or) (KEY, and) (KEY, int) (KEY, bool) (KEY, char) (KEY, while) (KEY, do) (KEY, if) (KEY, then) (KEY, else) (KEY, end) (KEY, repeat) (KEY, until) (KEY, read) (KEY, write) (SYM, ,) (SYM, ;) (SYM, :=) (SYM, +) (SYM, -) (SYM, *) (SYM, /) (SYM, ( ) (SYM, )) (SYM, ) (SYM, =) (ID, a2c) (NUM, 123) (STR, EFG) Test2 {this is an example} int A,B; bool C1, C2, C3; char D; D:= 'scanner'; while A<=B do A:=A*2 end The scanner should give the outputs: (KEY, int) (ID, A) (SYM, ,) (ID, B) (SYM, ;) (KEY, bool) (ID, C1) (SYM, ,) (ID, C2) (SYM, ,) (ID, C3
1