已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include #includeusing namespace std;#include#include #includeint i,j,k,flag,number,status,m=0,n=0;/*status which is use to judge the string is keywords or not!*/char ch;char words10 = ;char program500;int flags500; /存储输入句子string cnt500;/标识符int temp=0; /数组下标int is_right; /判断输出信息/-词法分析-int Scan(char program) char *keywords9 = int,char,float,if,else,do,while,printf,main; /关键字number=0;status=0;j=0;ch=programi+; /遍历while(ch= |ch=n) /跳过空字符(无效输入) ch=programi+; if (ch = a) & (ch = a) & (ch = z ) wordsj+=ch; ch=programi+; i-; wordsj+ = 0;for (k = 0; k = 0) & (ch = 0 ) & (ch : if (ch = ) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 403; else i-; flag = 404; break; case: if (ch = )wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 405;elsei-;flag = 406;break;case!:if (ch = !)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 407;elsei-;flag = 408;break;case+:if (ch = +)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 409;else if (ch = +)wordsj+ = ch;wordsj = 0;flag = 410;elsei-;flag = 411;break;case-:if (ch = -)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 412;else if( ch = -)wordsj+ = ch;wordsj = 0;flag = 413;elsei-;flag = 414;break;case*:if (ch = *)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 415;elsei-;flag = 416;break;case/:if (ch = /)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 417;elsei-;flag = 418;break;case:wordsj = ch;wordsj+1 = 0;flag = 419;break;case;:wordsj = ch;wordsj+1 = 0;flag = 501;break;case(:wordsj = ch;wordsj+1 = 0;flag = 502;break;case):wordsj = ch;wordsj+1 = 0;flag = 503;break;case:wordsj = ch;wordsj+1 = 0;flag = 504;break;case:wordsj = ch;wordsj+1 = 0;flag = 505;break;case:wordsj = ch;wordsj+1 = 0;flag = 506;break;case:wordsj = ch;wordsj+1 = 0;flag = 507;break;case:wordsj = ch;wordsj+1 = 0;flag = 508;break;case:wordsj = ch;wordsj+1 = 0;flag = 509;break;case%:if (ch = %)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 510;elsei-;flag = 511;break;case,:wordsj = ch;wordsj+1 = 0;flag = 512;break;case#:wordsj = ch;wordsj+1 = 0;flag = 513;break;case:wordsj = ch;wordsj+1 = 0;flag = 514;break;case :/空格 wordsj =_; wordsj+1 = 0; flag = 515; break;case$:wordsj = #;wordsj+1 = 0;flag = 0;break;default:flag = -1;break;return flag;/-语法分析(递归下降)-void s();void s1();void e();void e1();void t();void t1();void f();void s()coutES endl;e();s1();void s1()if(flagstemp=404)cout Eendl;temp+;e();else if(flagstemp=406)cout Eendl;temp+;e();else if (flagstemp!=0|flagstemp!=503) coutendl; temp+; return ; elseis_right=0;void e()coutTEendl;t();e1();void e1()if(flagstemp=411)cout+Tendl;temp+;t();else if(flagstemp=414)cout-Tendl;temp+;t(); else if (flagstemp!=0|flagstemp!=503) coutendl; temp+; return ; elseis_right=0;void t()coutFTendl;f();t1();void t1()if(flagstemp=416)cout*Fendl;temp+;f();else if(flagstemp=418)cout/Fendl;temp+;f();else if (flagstemp!=0|flagstemp!=503) coutendl; temp+; return ; else is_right=0;void f()if(flagstemp=100|flagstemp=200)coutiendl;temp+;if(flagstemp=402) cout=Eendl; temp+; e();elseif(flagstemp=502)cout(E)endl;temp+;e();if(flagstemp=503)cout(E)endl;temp+;elseis_right=0;else is_right =0;void main() int i=0,num=0; cout请输入测试程序或者表达式,以$结束endl; do ch =getchar(); programi+ = ch; while(ch != $); i = 0; cout词法分析:endl; do flag = Scan(program);/词法分析 if (flag = 200) cout(flag,number)endl;flagsnum=flag;stringstream stream;streamcntnum;stream.clear();num+; else if (flag = -1) couterror!endl; else cout(flag,words)endl; if
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- KRAS-G12C-IN-71-生命科学试剂-MCE
- Q-ZTGD 03-2023 输水用PVC-U管材标准
- 2026年公司文秘测试题及答案
- 2026年人体变态测试题及答案
- 2026年恋爱满分测试题及答案
- 第四季度员工思想动态分析报告(2篇)
- 2026年小学数学状元测试题及答案
- 2026年生物21章测试题及答案
- (新)治疗室管理制度2篇
- 职高语文词语题目及答案
- KDIGO慢性肾脏病贫血管理临床实践指南(2026年)解读课件
- 医疗机构医用高压氧治疗技术管理规范(2025年版)
- 2025年河北机关事业单位工人技能等级考试(渠道维护工-技师)试卷及答案
- 2025年四川三支一扶真题
- 2025四川雅砻江流域水电开发有限公司校园招聘100人笔试历年常考点试题专练附带答案详解试卷3套
- 公路交通安全设施设计细则
- 交警客运驾驶员安全课件
- 小学道德与法治新部编版二年级上册全册教案(2025秋)
- 2025年中国静音舱行业市场全景分析及前景机遇研判报告
- 2025年河北石家庄交通投资发展集团有限责任公司公开招聘操作类工作人员336人笔试参考题库附带答案详解
- 随车吊吊装安全知识培训课件
评论
0/150
提交评论