电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

编译原理实验-词法分析

8页
  • 卖家[上传人]:壹****1
  • 文档编号:483357499
  • 上传时间:2022-08-16
  • 文档格式:DOC
  • 文档大小:102.51KB
  • / 8 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、 实验一 词法分析器设计【实验目的】1熟悉词法分析的基本原理,词法分析的过程以及词法分析中要注意的问题。2复习高级语言,进一步加强用高级语言来解决实际问题的能力。3通过完成词法分析程序,了解词法分析的过程。【实验内容】用C语言编写一个PL/0词法分析器,为语法语义分析提供单词,使之能把输入的字符串形式的源程序分割成一个个单词符号传递给语法语义分析,并把分析结果(基本字,运算符,标识符,常数以及界符)输出。【实验步骤和要求】1 要求绘出词法分析过程的流程图。2 根据词法分析的目的以及内容,确定完成分析过程所需模块。3 写出每个模块的源代码。4 整理程序清单及所得结果。/源程序如下:#include #include #include #include #include #define NULL abcusing namespace std;ifstream fp(d:cifa.cpp,ios:in);char cch;char *key12=if,else,for,while,do,return,break,continue,int,void,main,const; /基本字 char

      2、*border10= , , ; , , , ( , ) , , , ; /界符 char *arithmetic6=+ , - , * , / , + , -; /算术运算符 char *relation7= , , = , = ,!=; /关系运算符 char *lableconst80; /标识符 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=5;i+) if (strcmp(arithmetici,searchchar)=0) return(i+1);

      3、 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-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错误!;

      4、 char alphaprocess(char ch) int atype; int i=-1; char alphatp20; while ( (isalpha(ch) | (isdigit(ch) ) alphatp+i=ch; fp.get(ch); alphatpi+1=0; if (atype=search(alphatp,1) coutalphatpttt(atype-1)endl; else atype=search(alphatp,6); coutalphatpttt(atype-1)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); coutdigittpttt(dtype-40)endl; return(ch);char otherprocess(char ch) int i= -1; c

      5、har 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) coutothertpttt(otypetp-1)endl; fp.get(ch); goto out; else othertp1=0; coutothertpttt(otype-1)endl; goto out; if (otype=search(othertp,4) fp.get(ch); othertp1=ch; othertp2=0; if (otypetp=search(othertp,4) coutothertpttt(otypetp-1)endl; fp.get(ch); goto out; else othertp1=0; coutothertpttt(otype-1)endl; goto out; if (ch=!) fp.get(ch); if (ch=) co

      6、ut!= (2,2)n; fp.get(ch); goto out; else if (otype=search(othertp,2) coutothertpttt(otype-1)endl; fp.get(ch); goto out; if (ch!=n)&(ch!= ) cout错误!,字符非法tttchendl; fp.get(ch); out: return(ch); void main() int i; for (i=0;i=50;i+) lableconsti=NULL; if (!fp) cout文件打开错误!endl; else fp.get (cch); while (!fp.eof() if (isalpha(cch) cch=alphaprocess(cch); else if (isdigit(cch) cch=digitprocess(cch); else cch=otherprocess(cch); cout成功n; getchar();cifa.cpp#includevoid main() cout”Hello World”;【实验小结】通过这次实验,我对编译原理这门专业必修课有了进一步的深层次了解,把理论知识应用于实验中,也让我重新熟悉C语言的相关内容,加深了对C语言知识的深化和用途的理解。 实验二 LL(1)语法分析程序设计【实验目的】1熟悉判断LL(1)文法的方法及对某一输入串的分析过程。2学会构造表达式文法的预测分析表。【实验内容】编写一个语法分析程序,对于给定的输入串,能够判断识别该串是否为给定文法的句型。【实验步骤和要求】1 从键盘读入输入串,并判断正误;2 若无误,由程序自动构造FIRST、FOLLOW集以及SELECT集合,判断是否为LL(1)文法;3 若符合LL(1)文法,由程序自动构造LL(1)分析表;由算法判断输入符号串是否为该文法的句型。 开始 进行语法分析判别N显示结果结束YYN是否分析下一条语句?输入字符串构造并显示分析表找出个产生式的select集找出并显示个终结符的foll

      《编译原理实验-词法分析》由会员壹****1分享,可在线阅读,更多相关《编译原理实验-词法分析》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    监控施工 信息化课堂中的合作学习结业作业七年级语文 发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.