已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 选择性必修3 第4单元 第11课 创新思维要善于联想
- 2026年油库防火防爆培训试题(附答案)
- 具身智能+智慧农业机器人种植管理方案
- 2025年绿色家居市场品牌策略研究及可行性报告
- 线上健身房服务消费者品牌忠诚度评价报告2025
- 雨污管网建设项目顶管专项施工方案 - 计算书
- 电力保密工作专项知识考试题库
- 2026年林长制岗位测试考试题及答案解析
- 医疗卫生岗临床医学基础历年真题试卷
- 医疗卫生岗护理技能操作易错题试卷
- 《孙子兵法》文言文与白话文对照
- 中建基础设施公司“主要领导讲质量”
- 应急通信课件教学课件
- 人教版体育与健康《足球》单元作业设计
- DB34T∕ 2805-2016 焦炉煤气生产硫化钠技术规程
- 国有企业成本管控的优化措施研究
- 示范村建设项目勘察设计后续服务的安排及保证措施
- 收费站机电维护知识讲座
- 非遗文化创意产品设计 课件全套 第1-5章 概述- 非遗文创产品设计案例解析
- 新概念英语第二册+Lesson+4+An+exciting+trip+讲义
- 初中奥数28条知识点总结
评论
0/150
提交评论