编译原理语义分析实验报告——免费!

上传人:博****1 文档编号:499529848 上传时间:2022-07-14 格式:DOC 页数:11 大小:96.50KB
返回 下载 相关 举报
编译原理语义分析实验报告——免费!_第1页
第1页 / 共11页
编译原理语义分析实验报告——免费!_第2页
第2页 / 共11页
编译原理语义分析实验报告——免费!_第3页
第3页 / 共11页
编译原理语义分析实验报告——免费!_第4页
第4页 / 共11页
编译原理语义分析实验报告——免费!_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《编译原理语义分析实验报告——免费!》由会员分享,可在线阅读,更多相关《编译原理语义分析实验报告——免费!(11页珍藏版)》请在金锄头文库上搜索。

1、语义分析实验报告一、实验目的: 通过上机实习, 加深对语法制导翻译原理的理解, 掌握将语法分析所识别的 语法成分变换为中间代码的语义翻译方法。二、实验要求: 采用递归下降语法制导翻译法, 对算术表达式、 赋值语句进行语义分析并生 成四元式序列。三、算法思想: 1、设置语义过程。(1) emit(char *result,char *ag1,char *op,char *ag2) 该函数的功能是生成一个三地址语句送到四元式表中。 四元式表的结构如下:struct char result8;char ag18;char op8;char ag28; quad20;(2) char *newtemp

2、()该函数回送一个新的临时变量名,临时变量名产生的顺序为T1, T2, char *newtemp(void) char *p;char m8;p=(char *)malloc(8);k+;itoa(k,m,10); strcpy(p+1,m);p0= t; return(p);2、函数 lrparser 在原来语法分析的基础上插入相应的语义动 作:将输入串翻译成四元式序列。 在实验中我们只对表达式、 赋值语 句进行翻译。四、源程序代码:#include#include#include#includestructchar result12;char ag112;char op12;char a

3、g212;quad;char prog80,token12;char ch;int syn,p,m=0,n,sum=0,kk; /p 是缓冲区 prog 的指针, m 是 token 的指针 char *rwtab6=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 stateme

4、nt();int k=0;void emit(char *result,char *ag1,char *op,char *ag2)strcpy(quad.result,result);strcpy(quad.ag1,ag1);strcpy(quad.op,op);strcpy(quad.ag2,ag2);coutquad.result=quad.ag1quad.opquad.ag2endl;char *newtemp()char *p;char m12;p=(char *)malloc(12);k+;itoa(k,m,10);strcpy(p+1,m);p0=t;return (p);void

5、 scaner()for(n=0;n=a&ch=A&ch=0&ch=a&ch=A&ch=Z) tokenm+=ch;ch=progp+; tokenm+=0;p-;syn=10;for(n=0;n=0&ch=0&ch32767)syn=-1;else switch(ch)case)syn=21;tokenm+=ch;else if(ch=) syn=22; tokenm+=ch;elsesyn=23;p-;break;case:m=0;tokenm+=ch;ch=progp+;if(ch=) syn=24; tokenm+=ch;elsesyn=20;p-;break;case:m=0;tok

6、enm+=ch;ch=progp+;if(ch=) syn=18; tokenm+=ch;elsesyn=17;p-; break;case*:syn=13;token0=ch;break;case/:syn=14;token0=ch;break;case+:syn=15;token0=ch;break;case-:syn=16;token0=ch;break;case=:syn=25;token0=ch;break;case;:syn=26;token0=ch;break;case(:syn=27;token0=ch;break;case):syn=28;token0=ch;break;ca

7、se#:syn=0;token0=ch;break;default: syn=-1;break;int lrparser() /cout 调用 lrparserendl;int schain=0;kk=0;if(syn=1)scaner();schain=yucu();if(syn=6)scaner();if(syn=0 & (kk=0)coutsuccess!endl;elseif(kk!=1)cout 缺 end!endl;kk=1;elsecout 缺 begin!endl;kk=1;return(schain);int yucu()/ cout 调用 yucuendl;int scha

8、in=0; schain=statement();while(syn=26)scaner();schain=statement();return(schain);int statement() /cout 调用 statementendl; char *eplace,*tt;eplace=(char *)malloc(12); tt=(char *)malloc(12);int schain=0;switch(syn)case 10: strcpy(tt,token); scaner();if(syn=18)scaner();strcpy(eplace,expression();emit(tt

9、,eplace,);schain=0;elsecout 缺少赋值符 !endl;kk=1;return(schain);break;return(schain);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 分析产生表达式计算的第一项 eplacewhile(syn=15)|(syn=16)if(

10、syn=15)strcpy(tt,+);else strcpy(tt,-);scaner();strcpy(ep2,term();/ 调用 term 分析产生表达式计算的第二项 ep2strcpy(tp,newtemp(); / 调用 newtemp 产生临时变量 tp 存储计算结果 emit(tp,eplace,tt,ep2); / 生成四元式送入四元式表 strcpy(eplace,tp);return(eplace);char *term(void)/ cout 调用 termendl;char *tp,*ep2,*eplace,*tt;tp=(char *)malloc(12);ep2

11、=(char *)malloc(12);eplace=(char *)malloc(12);tt=(char *)malloc(12);strcpy(eplace,factor();while(syn=13)|(syn=14)if(syn=13)strcpy(tt,*);else strcpy(tt,/);scaner();strcpy(ep2,factor();/ 调用 factor 分析产生表达式计算的第二项ep2strcpy(tp,newtemp(); / 调用 newtemp 产生临时变量 tp 存储计算结果 emit(tp,eplace,tt,ep2); / 生成四元式送入四元式表

12、strcpy(eplace,tp);return(eplace);char *factor(void)char *fplace;fplace=(char *)malloc(12);strcpy(fplace,);if(syn=10)strcpy(fplace,token);/将标识符 token 的值赋给 fplacescaner();else if(syn=11)itoa(sum,fplace,10);scaner();else if(syn=27)scaner();fplace=expression();/调用 expression 分析返回表达式的值if(syn=28)scaner();elsecout 缺 )错误 !endl;kk=1;elsecout缺(错误!endl;kk=1;return(fplace);void main(

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号