


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验一词法分析器设计【实验目的】1熟悉词法分析的基本原理,词法分析的过程以及词法分析中要注意的问题。2. 复习高级语言,进一步加强用高级语言来解决实际问题的能力。3通过完成词法分析程序,了解词法分析的过程。【实验内容】用C语言编写一个PL/O词法分析器,为语法语义分析提供单词,使之能把输入的字符串形 式的源程序分割成一个个单词符号传递给语法语义分析,并把分析结果(基本字,运算符, 标识符,常数以及界符)输出。【实验步骤和要求】1. 要求绘出词法分析过程的流程图。2. 根据词法分析的目的以及内容,确定完成分析过程所需模块。3. 写出每个模块的源代码。4. 整理程序清单及所得结果。/源程序如下:#
2、in elude <iostream>#include <ctype.h>#include <fstream>#include <string.h>#include <malloc.h>#define NULL "abc"using namespace std;ifstream fp("d:cifa.cpp",ios:in);char cch;char *key12="if","else","for","while"
3、,"do","return","break","continue","int","void" ,"main","const"/ 基本字char *border10= "," , "" , "" , "" , "(" , ")" , "" , "",""&quo
4、t;,""" ;/界符char *arithmetic6="+" , "-" , "*" , "/" , "+" , "-"/ 算术运算符char *relation7="<" , "<=" , "=" , ">" , ">=" , "=" ,"!="/关系运算符char *lable
5、const80;/标识符int constnum=40;int lableconstnum=0;int search(char searchchar,int wordtype)int i=0,t=0;switch (wordtype)case 1:for (i=0;i<=11;i+)if (strcmp(keyi,searchchar)=0)return(i+1);return(0);case 2:for (i=0;i<=9;i+)if (strcmp(borderi,searchchar)=0)return(i+1);return(0);case 3:for (i=0;i<
6、=5;i+)if (strcmp(arithmetici,searchchar)=0)return(i+1);return(0);case 4:for (i=0;i<=6;i+)if (strcmp(relationi,searchchar)=0)return(i+1);return(0);case 5:for (t=40;t<=constnum;t+)if (strcmp(searchchar,lableconstt)=0)return(t+1);lableconstt-1=(char *)malloc(sizeof(searchchar);strcpy(lableconstt-
7、1,searchchar);constnum+;return(t);case 6:for (i=0;i<=lableconstnum;i+)if (strcmp(searchchar,lableconsti)=0)return(i+1);lableconsti-1=(char *)malloc(sizeof(searchchar);strcpy(lableconsti-1,searchchar);lableconstnum+;return(i);default:cout<<" 错误! "char alphaprocess(char ch)int atype
8、;int i=-1;char alphatp20;while ( (isalpha(ch) | (isdigit(ch) ) alphatp+i=ch; fp.get(ch); alphatpi+1='0'if (atype=search(alphatp,1)cout<<alphatp<<"ttt"<<(atype-1)<<endl; else atype=search(alphatp,6); cout<<alphatp<<"ttt"<<(atype-1)
9、<<endl; return(ch);char digitprocess(char ch)int I = -1;char digittp20;int dtype;while (isdigit(ch) digittp+i=ch; fp.get(ch); digittpi+1='0' dtype=search(digittp,5); cout<<digittp<<"ttt"<<(dtype-40)<<endl; return(ch);char otherprocess(char ch)int i= -1
10、;char othertp20; int otype,otypetp; othertp0=ch; othertp1='0'if (otype=search(othertp,3) fp.get(ch); othertp1=ch; othertp2='0'if (otypetp=search(othertp,3) cout<<othertp<<"ttt"<<(otypetp-1)<<endl; fp.get(ch); goto out;else othertp1='0' cout&l
11、t;<othertp<<"ttt"<<(otype-1)<<endl; goto out;if (otype=search(othertp,4)fp.get(ch); othertp1=ch; othertp2='0'if (otypetp=search(othertp,4) cout<<othertp<<"ttt"<<(otypetp-1)<<endl; fp.get(ch);goto out;else othertp1='0' c
12、out<<othertp<<"ttt"<<(otype-1)<<endl; goto out;if (ch='!') fp.get(ch);if (ch='=')cout<<"!= (2,2)n"fp.get(ch);goto out;elseif (otype=search(othertp,2) cout<<othertp<<"ttt"<<(otype-1)<<endl; fp.get(ch);
13、goto out;if (ch!='n')&&(ch!=' ')cout<<" 错误! ,字符非法 "<<"ttt"<<ch<<endl; fp.get(ch);out: return(ch);void main()int i;for (i=0;i<=50;i+) lableconsti=NULL; if (!fp) cout<<" 文件打开错误! ! "<<endl;elsefp.get (cch);whil
14、e (!fp.eof()if (isalpha(cch) cch=alphaprocess(cch);else if (isdigit(cch) cch=digitprocess(cch);else cch=otherprocess(cch);cout<<" 成功 n" getchar();cifa.cpp#include<stdio.h> void main()cout<< ”Hello World ”;【实验小结】通过这次实验,我对编译原理这门专业必修课有了进一步的深层次了解,把理论知识使用于实验中,也让我重新熟悉 C语言的相关内容,加深了对C语言知识的深化和用途的理解。实验二LL( 1)语法分析程序设计【实验目的】1. 熟
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025湖北恩施州宣恩县园投人力资源服务有限公司招聘多家企业人员人员模拟试卷及答案详解一套
- 2025菏泽曹县教育系统公开招聘初级岗位教师(166人)考前自测高频考点模拟试题及答案详解(必刷)
- 2025河南洛阳市老城区招聘劳务派遣人员5人考前自测高频考点模拟试题及一套答案详解
- 2025广西钦州市浦北县县直中学(职校)教师专场招聘55人考前自测高频考点模拟试题有完整答案详解
- 2025年亳州利辛县开源水务有限公司招聘专业技术人员15人考试参考题库及答案解析
- 2025年山东辉煌国际物流发展有限公司社会招聘模拟试卷有答案详解
- 2025广西柳州市城中区人民法院招录3人(二)考前自测高频考点模拟试题及答案详解(典优)
- 2025北京大兴区妇幼保健院临时辅助用工招录岗位12人模拟试卷及一套完整答案详解
- 2025年甘肃财贸职业学院考核招聘博士研究生考前自测高频考点模拟试题及答案详解(名校卷)
- 2025国家电网内蒙古新正产业发展有限公司高校毕业生招聘41人(第三批)模拟试卷参考答案详解
- 2024安徽水利考试试题及答案
- 项目725T型客车电气系统7.1综合控制柜的概况7.2综合
- 学习解读《水利水电建设工程验收规程》SLT223-2025课件
- 大数据时代下的幼儿园安全管理信息化策略研究
- 食品验收合同协议
- 作业票管理制度国标
- 数字人合同协议
- 研发项目规范管理及核算
- 广西江河资产管理有限公司招聘考试真题2024
- 高校实施财会监督的思考
- 《精神医学概论》课件
评论
0/150
提交评论