




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1、 实验目的1、为初等函数运算语言构造语法分析器。2、掌握生成词法分析器的方法,加深对词法分析原理的理解。3、掌握设计、编制并调试词法分析程序的思想和方法。2、实验内容2.1词法模式设计/正则式1、 初等函数运算语言的常量为实数类型,其定义方式为实数的最一般书写方式,如:123.321。具体要求:不支持整数部分大于0时首数字为0;不支持小数点后结尾为0;不支持科学记数法;不支持仅为整数时有小数点。1-90-9*(.0-9*1-9+)?$2、 初等函数运算语言的变量采用与C语言的标识符定义一样的方式:首字符为字母或下划线;其他的为字母、数字及下划线的混合串;区分大小写;变量长度不超过32个字符
2、。a-zA-Z_A-Za-z0-90,31$3、 初等函数运算语言需要处理的函数仅为表一中所列举的内容。4、 初等函数运算语言支持四则运算,其计算的符号与C语言相同,为:+-*/。+-*/$5、 初等函数运算语言的合法的分隔符包括:空格、制表符、分行符圆括号(左、右)、分号。其中空格、制表符、分行符可以出现在任何两个不同的单词中间;圆括号(左、右)用于表达式中,用于改变运算的优先级,以及标识函数的参数;分号用于标识一个语句的结束。ftn();$6、 初等函数运算语言支持的常量还包括:PI,E。PI|E$2.2DFA2.3状态转换表_字符数字运算符小数点0012、4010110200330030
3、4004032.4记号表2、 实验程序清单function.cppmains.cppname.hwordID.txt3、 调试过程和运行结果4、 "x = 0.5*PI;y = E;?1/3*(ln(y)+5*sin(x)+(7+z)2;"5、 程序的主要部分及其功能说明函数声明- name.h -#include<iostream>#include<string>using namespace std;const int Max=1000;const string FunctionName = "sin", "cos&
4、quot;, "tg","ctg","log","lg","ln","PI", "E" ;/关键字class Operateprivate :string str;/输入的字符串string wordMax;/输出的结果int numMax;/输出的类型public:int startPostion;/字符开始位置int nowPostion;/字符当前位置int length;/结果个数 Operate(string s); void getWordAn
5、dNum(); bool isNumber(char c); bool isChar(char c);/ bool isChar(char c); int isOperaters(char c); bool isblank(char c); void addWords(string s,int n);int isKey(string str);void work(string str);- function.cpp -函数定义- function.cpp -#include<iostream>#include<iomanip>#include<string>
6、#include"name.h"using namespace std;/Operate类定义 Operate:Operate(string s) str=s; startPostion=0; nowPostion=0; length=0; void Operate:getWordAndNum() for(int i=0;i<=(length-1);i+) cout<<"<"<<setw(10)<<wordi<<","<<setw(3)<<numi&l
7、t;<">n" bool Operate:isNumber(char c) if (c >= '0' && c <= '9') return true; return false; bool Operate:isChar(char c) if (c >= 'a' && c <= 'z') | (c >= 'A' && c <= 'Z')|c = '_') return
8、true; return false; int Operate:isOperaters(char c) switch (c) case'?':return 10; case '':return 11; case '(':return 12; case ')':return 13; case '+':return 14; case '-':return 15; case '*':return 16; case '/':return 17;case '='
9、:return 18; case '':return 19;default: return 0; ; bool Operate:isblank(char c) switch (c) case ' ': case 't': case 'n': return true; return false; void Operate:addWords(string s,int n) wordlength=s; numlength=n; length+=1; /其他函数/是否是关键字,是返回序号,否返回0int isKey(string str)
10、for(int i=0;i<9;i+)if(str=FunctionNamei)return i+1;return 0;void work(string str)int longs=str.length();string aword;char c;/当前字符int state=0;/状态int number=0;/类型Operate op(str);while(true)/读取字符if(op.nowPostion>=longs)break;else c=strop.nowPostion;switch(state)case 0:/初始状态if(c='$')break;
11、/如果是运算符/保存当前运算符,startPostion=nowPostion=+1number=op.isOperaters(c);if(number>0)op.addWords(str.substr(op.startPostion,1),number);op.nowPostion+;op.startPostion=op.nowPostion;break;if(op.isblank(c)op.nowPostion+;op.startPostion=op.nowPostion;break;/如果是字符if(op.isChar(c)state=1;op.nowPostion+;break;
12、if(c>='1'&&c<='9')state=4;op.nowPostion+;break;if(c='0')state=2;op.nowPostion+;break;break;case 1:/识别字符串if(op.isChar(c)|op.isNumber(c)op.nowPostion+;break; /否则的话,完成当前标识符的识别number=20;aword=str.substr(op.startPostion,op.nowPostion-op.startPostion);if(isKey(aword)&
13、gt;0)number=isKey(aword);op.addWords(aword,number);op.startPostion=op.nowPostion;state=0;break;case 2:/识别0,若下一个字符不是小数点'.'则显示输入错误,跳过0,读取下一个字符if(c='.')state=3;op.nowPostion+;break;if(op.isOperaters(c)>0)state=0;break;op.addWords("0开头,输入错误!跳过",0);op.startPostion=op.nowPosti
14、on;state=0;break;case 3:/识别0-9if(op.isNumber(c)op.nowPostion+;break;op.addWords(str.substr(op.startPostion,op.nowPostion-op.startPostion),21);op.startPostion=op.nowPostion;state=0;break;case 4:/识别1-9if(op.isNumber(c)/识别整数op.nowPostion+;break;if(c='.')/识别小数state=3;op.nowPostion+;break;op.addWords(str.substr(op.startPostion,op.nowPostion-op.startPostion),21);op.startPostion=op.nowPostion;state=0;break;op.getWordAndNum();- mains.cpp -主函数输入测试数据!- mains.cpp -#include<iostream>#include<string>#include"name.h"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《行路难(其一)》教学设计
- 《电子元件封装技术》课件
- 中草药有效成分的提取与结构鉴定课件
- 《优化客户沟通技巧》课件
- 安全培训个人总结
- 航空事故处理考核试卷
- 小草的课件教学课件
- 2025年煤炭采掘机械设备合作协议书
- 《T教育讲座》课件
- 运维安全问题
- 2025年中考理化生实验操作考试考务培训大纲
- 2021年高考地理试卷(全国甲卷)(空白卷)
- 2025四川巴中市国有资本运营集团有限公司招聘17人笔试参考题库附带答案详解
- 二年级古诗词大赛选择题
- pyramid之架构与应用
- 七年级期中考试后家长会课件39820
- 剑9阅读真题原文1-William-Henry-Perkin
- 最新氧气安全标签
- 1100集装箱船体建造工艺指导书
- 橡塑保温材料检验报告模板
- 液压基本回路及典型液压系统.ppt
评论
0/150
提交评论