




免费预览已结束,剩余10页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
合肥工业大学计算机与信息学院课程设计课 程:编译原理专业班级:计算机科学与技术09-2班学 号:姓 名: 一:设计题目 题目: lr分析器总控程序的实现设计内容及要求:对p.101中的文法,按图5.5lr分析表构造lr分析器。要求程序按p.102例5.7那样,对于输入串i*i+i,输出lr分析器的工作过程。改进:可以自行输入输入串,能按重新按钮重新开始,外加了一个计时器。二:设计目的 进一步了解了lr分析器的整个工作过程,将lr分析器以图形界面的形式展现了出来,有利加深了对lr分析过程的掌握。三:实验原理本程序是用windows编的,大体的思想是这样的:通过建立一个符号栈,状态栈,输入栈(结构体类型定义的)分别用来存放符号,状态,输入串,将lr分析表构造成为一个二维数组table139,其中0-11表示状态结点,2126表示规约标号,-1表示error(出错),12表示acc(接受),在acation函数里面通过i等于多少来判断到底是规约还是移进。在main_oncommand()函数中通过switch.case.来判断每次及下一步所要执行的操作。运行的时候,首先要输入一个以#结尾的输入串,然后单击开始下一步.,如果规约成功,则弹出一个规约成功的对话框,否则弹出一个规约失败的对话框。当然在运行的过程中如果出现什么差错,都可以单击重新开始按钮重新输入输入串重新开始。四:实验代码#include stdafx.h#include #include #include resource.h#include maindlg.hchar *str26=e-e+t,e-t,t-t*f,t-f,f-(e),f-i;int flag=0;#define max 20typedef structint stack1max;int top1;status;typedef structchar stack2max;int top2;symbol_instr;char index_char9=i,+,*,(,),#,e,t,f;/为二维数数组的纵坐标/lr分析表/0-11表示状态结点,2126表示规约标号,/-1表示error(出错),12表示acc(接受)int table139 = 5,-1,-1, 4,-1,-1, 1, 2, 3,-1, 6,-1,-1,-1,12,-1,-1,-1,-1,22, 7,-1,22,22,-1,-1,-1,-1,24,24,-1,24,24,-1,-1,-1, 5,-1,-1, 4,-1,-1, 8, 2, 3,-1,26,26,-1,26,26,-1,-1,-1, 5,-1,-1, 4,-1,-1,-1, 9, 3, 5,-1,-1, 4,-1,-1,-1,-1,10,-1, 6,-1,-1,11,-1,-1,-1,-1,-1,21, 7,-1,21,21,-1,-1,-1,-1,23,23,-1,23,23,-1,-1,-1,-1,25,25,-1,25,25,-1,-1,-1;/规约规则struct rulechar x;int y;r6=e,3,e,1,t,3,t,1,f,3,f,1; /后面的代表a中的长度bool winapi main_proc(hwnd hwnd, uint umsg, wparam wparam, lparam lparam) switch(umsg) handle_msg(hwnd, wm_initdialog, main_oninitdialog); handle_msg(hwnd, wm_command, main_oncommand);handle_msg(hwnd,wm_close, main_onclose); return false;void init_stack1(hwnd hwnd,status *p)if( !p)messagebox(hwnd,text(出错),text(警告),mb_ok|mb_iconhand);p-top1 = -1;void push1(hwnd hwnd,status *&p,int x)if(p-top1 top1+;p-stack1p-top1 = x;else messagebox(hwnd,text(出错),text(警告),mb_ok|mb_iconhand);int pop1(hwnd hwnd,status *p)int x;if(p-top1 != 0)x = p-stack1p-top1;p-top1-;return x;else messagebox(hwnd,text(状态栈为空),text(警告),mb_ok|mb_iconhand);/printf(n状态栈1空!n);return 0;void out_stack(hwnd hwnd,status *p)if(p-top1 0)messagebox(null,text(状态栈为空),text(警告),mb_ok|mb_iconhand);/printf(n状态栈3空!n);int i;tchar str256;for(i=0;itop1;i+)wsprintf(&stri,%d,p-stack1i);setdlgitemtext(hwnd,idc_edit1,str);int get_top1(hwnd hwnd,status *p)int x;if(p-top1 != -1)x = p-stack1p-top1;return x;else messagebox(hwnd,text(状态栈为空),text(警告),mb_ok|mb_iconhand);/printf(n状态栈2空!n);return 0;void init_stack2(hwnd hwnd,symbol_instr *p)if( !p)messagebox(hwnd,text(出错),text(警告),mb_ok|mb_iconhand);p-top2= -1;void push2(hwnd hwnd,symbol_instr *p,char x)if(p-top2 top2+;p-stack2p-top2 = x;p-stack2p-top2+1=0;else messagebox(hwnd,text(出错),text(警告),mb_ok|mb_iconhand);char pop2(hwnd hwnd,symbol_instr *p)char x;if(p-top2 != -1)x = p-stack2p-top2;p-top2-;p-stack2p-top2+1=0;return x;else messagebox(hwnd,text(符号栈为空),text(警告),mb_ok|mb_iconhand);/printf(n符号栈1空!n);return 0;void out_stack1(hwnd hwnd,symbol_instr *p)if(p-top2 stack2);void out_stack2(hwnd hwnd,symbol_instr *p)if(p-top2 stack2);char get_top2(hwnd hwnd,symbol_instr *p)char x;if(p-top2 != -1)x = p-stack2p-top2;return x;else messagebox(hwnd,text(符号栈为空),text(警告),mb_ok|mb_iconhand);/printf(n符号栈2空!n);return 0;void print(hwnd hwnd,status *status_p,symbol_instr *symbol_p,symbol_instr *instr_p)out_stack(hwnd,status_p); /输出状态栈的内容out_stack1(hwnd,symbol_p); /输出符号栈的内容out_stack2(hwnd,instr_p); /输出输入串int get_index_char(char i)for(int j=0;j=0 & i=21 & i=26)x = ri-21.y;for(j=0;jx;j+)pop1(hwnd,status_p);pop2(hwnd,symbol_p);push2(hwnd,instr_p,ri-21.x);setdlgitemtext(hwnd,idc_edit4,str2i-21);void callback timerproc(hwnd hwnd,uint message,uint itimerid,dword dwtime)systemtime stlocal;tchar buf256;getlocaltime(&stlocal);wsprintf(buf,%d年%d月%d日 %d:%d:%d,stlocal.wyear,stlocal.wmonth,stlocal.wday,stlocal.whour,stlocal.wminute,stlocal.wsecond);setdlgitemtext(hwnd,idc_edit5,buf);bool main_oninitdialog(hwnd hwnd, hwnd hwndfocus, lparam lparam)settimer(hwnd,0,1000,timerproc);return true;status *status_p=new status;symbol_instr *symbol_p=new symbol_instr;symbol_instr *instr_p=new symbol_instr ; void main_oncommand(hwnd hwnd, int id, hwnd hwndctl, uint codenotify)switch(id) case idc_button1:init_stack1(hwnd,status_p); /初始化各栈init_stack2(hwnd,symbol_p);init_stack2(hwnd,instr_p);/压进栈初始元素push1(hwnd,status_p,0);push2(hwnd,symbol_p,#);char x;tchar msg256;getdlgitemtext(hwnd,idc_edit3,msg,sizeof(msg);unsigned int i;for(i=0;i top2 != 0)x = pop2(hwnd,symbol_p);push2(hwnd,instr_p,x);print(hwnd,status_p,symbol_p,instr_p);/打印初始分析表*/break;case idc_button2: action(hwnd,status_p,symbol_p,instr_p);break;case idc_button3:setdlgitemtext(hwnd,idc_edit1,text();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 平安银行北京市顺义区2025秋招半结构化面试题库及参考答案
- 九年级体育 第6周 第11次课说课稿
- 农发行亳州市谯城区2025秋招笔试创新题型专练及答案
- 管理岗职称考试题及答案
- 古代书院考试题及答案
- 供电公司专业考试题及答案
- 高等代数二考试题及答案
- 2025中外合作研发合同范本
- 2025合作伙伴合同书
- 提高跨境资金流动中的信息安全保障措施
- 数据挖掘教学课件
- 2025年造价咨询公司廉政制度及保障措施
- 2025至2030中国停机坪货架行业项目调研及市场前景预测评估报告
- 补液课件教学课件
- 电池厂化成柜安全操作规范规章
- 电力公司施工安全培训课件
- 2025年一级建造师《(市政公用工程)管理与实务》考试真题及答案
- 宏村简介课件
- 潍坊市2026届高三开学调研监测考试数学试题及答案
- 车辆产品公告管理办法
- 2025喀什经济开发区兵团分区招聘(10人)考试参考试题及答案解析
评论
0/150
提交评论