ImageVerifierCode 换一换
格式:DOCX , 页数:26 ,大小:71.99KB ,
资源ID:3489457      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3489457.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(编译原算符优先分析实验报告.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

编译原算符优先分析实验报告.docx

1、编译原算符优先分析实验报告学号 E10714103 专业 计算机科学与技术 姓名 万学进实验日期2010-5-25 教师签字 成绩实 验 报 告【实验名称】 算符优先文法分析【实验目的】掌握算符优先分析法的原理,利用算符优先分析法将赋值语句进行语法分析,翻译成等价的四元式表示。【实验内容】1.算术表达式的文法可以是: (1)S-#E#(2)E-E+T(3)E-T(4)T-T*F(5)T-F(6)F-PF(7)F-P(8)P-(E)(9)P-i2.根据算符优先分析法,将表达式进行语法分析,判断一个表达式是否正确。 【设计思想】(1)定义部分:定义常量、变量、数据结构。(2)初始化:设立算符优先关

2、系表、初始化变量空间(包括堆栈、结构体、数组、临时变量等);(3)控制部分:从键盘输入一个表达式符号串;(4)利用算符优先文法分析算法进行表达式处理:根据优先关系表对表达式符号串进行堆栈(或其他)操作,输出分析结果,如果遇到错误则显示错误信息。【流程图】【源代码】#include#include#includechar Grammar2010;char VN10,VT10;char BoolArray1010;char FirstBoolArray1010;char LastBoolArray1010;char RelationShip1010;#define StackSize 100;in

3、t vtNum,vnNum;int grammarNum;int scount=0;int VNum20;int GF210;typedef struct char vt; char vn;array;typedef struct array *base; array *top; int stacksize;SqStack;typedef struct char s20; int step; char curInVt;CharType;typedef struct CharType *base; CharType *top;Stack;typedef struct int x; int y;P

4、osition;SqStack S;Stack CS;SqStack InitStack() int i,j; S.base=(array *)malloc(100*sizeof(array); if(!S.base)exit(1); S.top=S.base; S.stacksize=StackSize; array temp; printf(初始化栈:n); for(i=1;i=vnNum;i+) for(j=1;j=vtNum;j+) if(BoolArrayij=1) temp.vt=BoolArray0j; temp.vn=BoolArrayi0; *S.top=temp; S.to

5、p+; printf(%c,%cn,temp.vn,temp.vt); return S;int vNumCount() for(int i=0;i) fp=tp; continue; if(singleChar=|) Grammari+10=Grammari0; Grammarij=0; i+; j=1; continue; Grammarij=singleChar; if(singleChar=A&singleChar=Z) count=0; while(VNcount!=singleChar&VNcount!=0) count+; if(VNcount=0) VNcount=single

6、Char; vnNum=count+1; else count=0; while(VTcount!=singleChar&VTcount!=0) count+; if(VTcount=0) VTcount=singleChar; vtNum=count+1; j+; printf(输入的文法:n); for(k=0;k); printf(%c,Grammarkj); j+; printf(n); printf(vnNum:%dn,vnNum); printf(vtNum:%dn,vtNum); printf(%dnVN:,i); count=0; while(VNcount!=0) BoolA

7、rraycount+10=VNcount; LastBoolArraycount+10=VNcount; printf(%d%c ,count+1,BoolArraycount+10); count+; printf(nVT:); count=0; while(VTcount!=0) BoolArray0count+1=VTcount; LastBoolArray0count+1=VTcount; printf(%d%c ,count+1,BoolArray0count+1); count+; printf(n); fclose(fp); return i;int print() for(in

8、t i=1;i=vnNum;i+) for(int j=1;j=vtNum;j+) if(BoolArrayij=1) printf(%c %cn,BoolArrayi0,BoolArray0j); return 0;int printlast() for(int i=1;i=vnNum;i+) for(int j=1;j=vtNum;j+) if(LastBoolArrayij=1) printf(%c %cn,LastBoolArrayi0,LastBoolArray0j); return 0;int BoolArrayInitial(char vn,char vt) int row=1,

9、column=1; while(BoolArrayrow0!=vn) row+; while(BoolArray0column!=vt) column+; if(BoolArrayrowcolumn=1) return 0; BoolArrayrowcolumn=1; printf(%d %dn,row,column); return 0;int LastBoolArrayInitial(char vn,char vt) int row=1,column=1; while(LastBoolArrayrow0!=vn) row+; while(LastBoolArray0column!=vt)

10、column+; if(LastBoolArrayrowcolumn=1) return 0; LastBoolArrayrowcolumn=1; printf(%d %dn,row,column); return 0;int FirstInitial(int grammarNum) int i; for(i=0;i=grammarNum;i+) if(Grammari1Z) BoolArrayInitial(Grammari0,Grammari1); continue; if(Grammari1=A&Grammari1=Z&Grammari2!=0) if(Grammari2Z) BoolA

11、rrayInitial(Grammari0,Grammari2); printf(文法表中各行文法字符数:); vNumCount(); return 0;int LastInitial(int grammarNum) int i,count; for(i=0;i=grammarNum;i+) count=VNumi-1; if(GrammaricountZ) LastBoolArrayInitial(Grammari0,Grammaricount); continue; if(count=A&Grammaricount=Z&Grammaricount-1!=0) if(Grammaricou

12、nt-1Z) LastBoolArrayInitial(Grammari0,Grammaricount-1); printlast(); return 0;Position GetPos(array temp) for(int i=1;i=vnNum;i+) if(BoolArrayi0=temp.vn) break; for(int j=1;j=vtNum;j+) if(BoolArray0j=temp.vt) break; Position pos; pos.x=i; pos.y=j; return pos;int PrintStack() array *p=S.top,scan; sco

13、unt+; printf(scount %d:n,scount); while(p!=S.base) scan=*p; printf(%c %cn,scan.vn,scan.vt); p-; return 0;int Insert(array pushTemp) Position pos; array scan; pos=GetPos(pushTemp); if(BoolArraypos.xpos.y=0) BoolArraypos.xpos.y=1; array *p=S.base; while(p!=S.top) scan=*p; if(scan.vn=pushTemp.vn & scan

14、.vt=pushTemp.vt) break; p+; if(p=S.top) S.top+; *S.top=pushTemp; PrintStack(); return 1;int PushPop() int i; array temp,pushTemp; while(S.top!=S.base) temp=*S.top; S.top-; for(i=0;i=grammarNum;i+) if(Grammari1=temp.vn) pushTemp.vn=Grammari0; pushTemp.vt=temp.vt; Insert(pushTemp); print(); return 0;i

15、nt LastPushPop() int i,count; array temp,pushTemp; while(S.top!=S.base) temp=*S.top; S.top-; for(i=0;i=grammarNum;i+) count=VNumi-1; if(Grammaricount=temp.vn) pushTemp.vn=Grammari0; pushTemp.vt=temp.vt; Insert(pushTemp); print(); return 0;int ResetStack() int i,j; array temp; while(S.top!=S.base) S.

16、top-; printf(n); for(i=1;i=vnNum;i+) for(j=1;j=A&Grammarij=Z&(Grammarij+1Z) return 0; else return 1;Position GetOpPos(char vni,char vnj) for(int i=1;i=vtNum;i+) if(RelationShipi0=vni) break; for(int j=1;j=vtNum;j+) if(RelationShip0j=vnj) break; Position pos; pos.x=i; pos.y=j; return pos;int GetVtPos

17、(char vt) for(int i=1;i=vtNum;i+) if(LastBoolArrayi0=vt) break; return i;int RelEqual() Position pos; int j; for(int i=0;i=grammarNum;i+) j=1; while(Grammarij!=0) if(Grammarij+2=0)break; if(GrammarijZ)&(Grammarij+2Z) pos=GetOpPos(Grammarij,Grammarij+2); RelationShippos.xpos.y=;/2表示= j+; return 0;int

18、 RelShBlanket() int row; Position pos; int i,j; for(i=1;i=vtNum;i+) RelationShipi0=BoolArray0i; RelationShip0i=BoolArray0i; for(i=0;i=grammarNum;i+) j=1; while(Grammarij!=0) if(Judge(i,j)=-1)break; if(Judge(i,j)=0) row=GetVtPos(Grammarij); for(int ii=1;ii;/3表示大于 if(Judge(i,j)=1) row=GetVtPos(Grammar

19、ij+1); for(int ii=1;ii=9;ii+) if(FirstBoolArrayrowii=1) pos=GetOpPos(Grammarij,FirstBoolArray0ii); RelationShippos.xpos.y=;/1表示小于 j+; RelEqual(); printf(表达式文法算符优先关系表:n); RelationShip00= ; for(i=0;i=vtNum;i+) for(int j=0;j=vtNum;j+) if(RelationShipij=0) RelationShipij=0; printf(%4c ,RelationShipij); printf(n); return 0;int ChangeValue() int change=0; int i,j; for(i=1;i=vtNum;i+) for(j=1;j) if(GF0i=GF1j) GF0i=GF1j+1; change=1; if(RelationShipij=GF1j) GF1j=GF0i+1; change=1; if(RelationShipij=) if(GF0i!=GF1j) if(GF0iGF1j) GF1j=GF0i; change=1; if(GF0iGF1j) GF0i=GF1j; change=1; if(change=1)

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1