编译原理实验报告和源程序

上传者: wuguimei | 上传时间: 2022-05-28 20:31:26 | 文件大小: 671KB | 文件类型: RAR
附录c 编译程序实验 实验目的:用c语言对一个简单语言的子集编制一个一遍扫描的编译程序,以加深对编译原理的理解,掌握编译程序的实现方法和技术。 语法分析 C2.1 实验目的 编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析. C2.2 实验要求 利用C语言编制递归下降分析程序,并对简单语言进行语法分析. C2.2.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 算法思想 1设置语义过程 (1) emit(char *result,char *arg1,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)主程序示意图如图c.10所示。 (2) 函数lrparser在原来语法分析的基础上插入相应的语义动作:将输入串翻译成四元式序列。在实验中我们只对表达式、赋值语句进行翻译。 语义分析程序的C语言程序框架 int lrparser() { int schain=0; kk=0; if(syn=1) { 读下一个单词符号; schain=yucu; /调用语句串分析函数进行分析/ if(syn=6) { 读下一个单词符号; if(syn=0 && (kk==0)) 输出(“success”); } else { if(kk!=1 ) 输出 ‘缺end’ 错误;kk=1;} else{输出’begin’错误;kk=1;} } return(schain); int yucu() { int schain=0; schain=statement();/调用语句分析函数进行分析/ while(syn=26) {读下一个单词符号; schain=statement(); /调用语句分析函数进行分析/ } return(schain); } int statement() { char tt[8],eplace[8]; int schain=0; {switch(syn) {case 10: strcpy(tt,token); scanner(); if(syn=18) {读下一个单词符号; strcpy(eplace,expression()); emit(tt,eplace,””,””); schain=0; } else {输出’缺少赋值号’的错误;kk=1; } return(schain); break; } } char *expression(void) {char *tp,*ep2,*eplace,*tt; tp=(char *)malloc(12);/分配空间/ ep2=(char *)malloc(12); eplace=(char *)malloc(12); tt =(char )malloc(12); strcpy(eplace,term ());/调用term分析产生表达式计算的第一项eplace/ while(syn=13 or 14) { 操作符 tt= ‘+’或者‘—’; 读下一个单词符号; strcpy(ep2,term());/调用term分析产生表达式计算的第二项ep2/ strcpy(tp,newtemp());/调用newtemp产生临时变量tp存储计算结果/ emit(tp,eplace,tt,ep2);/生成四元式送入四元式表/ strcpy(eplace,tp); } return(eplace); } char *term(void)/仿照函数expression编写/ char *factor(void) {char *fplace; fplace=(char *)malloc(12); strcpy(fplace, “ ”); if(syn=10) {strcpy(fplace,,token);/将标识符token的值赋给fplace/ 读下一个单词符号; } else if(syn=11) {itoa(sum,fplace,10); 读下一个单词符号; } else if (syn=27) {读下一个单词符号; fplace=expression();/调用expression分析返回表达式的值/ if(syn=28) 读下一个单词符号; else{输出‘}’错误;kk=1; } } else{输出‘(’错误;kk=1; } return(fplace); }

文件下载

资源详情

[{"title":"( 57 个子文件 671KB ) 编译原理实验报告和源程序","children":[{"title":"编译原理","children":[{"title":"语法分析","children":[{"title":"2_1","children":[{"title":"2.1 <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"2_1.dsp <span style='color:#111;'> 4.20KB </span>","children":null,"spread":false},{"title":"2.1.dsp <span style='color:#111;'> 3.29KB </span>","children":null,"spread":false},{"title":"2.1.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"2.1.dsw <span style='color:#111;'> 531B </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"vc60.pdb <span style='color:#111;'> 44.00KB </span>","children":null,"spread":false},{"title":"2.1.obj <span style='color:#111;'> 14.76KB </span>","children":null,"spread":false},{"title":"2.1.pdb <span style='color:#111;'> 337.00KB </span>","children":null,"spread":false},{"title":"2_1.exe <span style='color:#111;'> 164.04KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"2_1.pdb <span style='color:#111;'> 337.00KB </span>","children":null,"spread":false},{"title":"2.1.exe <span style='color:#111;'> 164.05KB </span>","children":null,"spread":false},{"title":"2_1.pch <span style='color:#111;'> 209.76KB </span>","children":null,"spread":false},{"title":"2_1.ilk <span style='color:#111;'> 171.59KB </span>","children":null,"spread":false},{"title":"2.1.ilk <span style='color:#111;'> 171.44KB </span>","children":null,"spread":false}],"spread":true},{"title":"2_1.ncb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"2_1.plg <span style='color:#111;'> 1.20KB </span>","children":null,"spread":false},{"title":"2.1.cpp <span style='color:#111;'> 1.59KB </span>","children":null,"spread":false},{"title":"2.1.ncb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"wordscan.h <span style='color:#111;'> 1.98KB </span>","children":null,"spread":false},{"title":"2.1.plg <span style='color:#111;'> 727B </span>","children":null,"spread":false},{"title":"2_1.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"2_1.dsw <span style='color:#111;'> 529B </span>","children":null,"spread":false}],"spread":false},{"title":"语法分析.doc <span style='color:#111;'> 51.50KB </span>","children":null,"spread":false},{"title":"语法分析实验报告.doc <span style='color:#111;'> 77.00KB </span>","children":null,"spread":false}],"spread":true},{"title":"语义分析","children":[{"title":"3_1","children":[{"title":"1.ncb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"1.plg <span style='color:#111;'> 1.58KB </span>","children":null,"spread":false},{"title":"1.cpp <span style='color:#111;'> 4.56KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"vc60.pdb <span style='color:#111;'> 44.00KB </span>","children":null,"spread":false},{"title":"1.exe <span style='color:#111;'> 96.06KB </span>","children":null,"spread":false},{"title":"1.obj <span style='color:#111;'> 18.64KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"1.pch <span style='color:#111;'> 217.77KB </span>","children":null,"spread":false},{"title":"1.ilk <span style='color:#111;'> 50.41KB </span>","children":null,"spread":false},{"title":"1.pdb <span style='color:#111;'> 545.00KB </span>","children":null,"spread":false}],"spread":true},{"title":"1.dsw <span style='color:#111;'> 527B </span>","children":null,"spread":false},{"title":"1.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"1.dsp <span style='color:#111;'> 3.27KB </span>","children":null,"spread":false}],"spread":true},{"title":"实验设计模板.doc <span style='color:#111;'> 81.00KB </span>","children":null,"spread":false},{"title":"Thumbs.db <span style='color:#111;'> 7.00KB </span>","children":null,"spread":false},{"title":"语义分析.doc <span style='color:#111;'> 31.00KB </span>","children":null,"spread":false}],"spread":true},{"title":"词法分析实验","children":[{"title":"~$实验设计模板.doc <span style='color:#111;'> 162B </span>","children":null,"spread":false},{"title":"词法分析.doc <span style='color:#111;'> 36.00KB </span>","children":null,"spread":false},{"title":"1_2","children":[{"title":"1_2.dsp <span style='color:#111;'> 3.29KB </span>","children":null,"spread":false},{"title":"1_2.cpp <span style='color:#111;'> 1.80KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"vc60.pdb <span style='color:#111;'> 44.00KB </span>","children":null,"spread":false},{"title":"1_2.ilk <span style='color:#111;'> 182.36KB </span>","children":null,"spread":false},{"title":"1_2.pdb <span style='color:#111;'> 417.00KB </span>","children":null,"spread":false},{"title":"1_2.obj <span style='color:#111;'> 9.39KB </span>","children":null,"spread":false},{"title":"1_2.pch <span style='color:#111;'> 182.54KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"1_2.exe <span style='color:#111;'> 164.04KB </span>","children":null,"spread":false}],"spread":true},{"title":"1_2.dsw <span style='color:#111;'> 514B </span>","children":null,"spread":false},{"title":"1_2.ncb <span style='color:#111;'> 41.00KB </span>","children":null,"spread":false},{"title":"1_2.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"1_2.plg <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false}],"spread":true},{"title":"词法分析实验报告.doc <span style='color:#111;'> 59.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}]

评论信息

  • aaronli713 :
    最好把三个实验程序分开成不同的文件,便于理解
    2012-05-29
  • carlyzhou1991 :
    有三个实验的集合,但是在Dev里无法正常运行,需要修改!
    2012-05-29

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明