编译原理实验----词法分析.doc_第1页
编译原理实验----词法分析.doc_第2页
编译原理实验----词法分析.doc_第3页
编译原理实验----词法分析.doc_第4页
编译原理实验----词法分析.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 实验一 词法分析器设计【实验目的】1熟悉词法分析的基本原理,词法分析的过程以及词法分析中要注意的问题。2复习高级语言,进一步加强用高级语言来解决实际问题的能力.3通过完成词法分析程序,了解词法分析的过程。【实验内容】用C语言编写一个PL/0词法分析器,为语法语义分析提供单词,使之能把输入的字符串形式的源程序分割成一个个单词符号传递给语法语义分析,并把分析结果(基本字,运算符,标识符,常数以及界符)输出。【实验步骤和要求】1 要求绘出词法分析过程的流程图.2 根据词法分析的目的以及内容,确定完成分析过程所需模块。3 写出每个模块的源代码。4 整理程序清单及所得结果。/源程序如下:include

2、 iostream#include ctype.h#include fstreaminclude 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”,"do”,"return”,"break”,"continue",”int”,”void","main&qu

3、ot;,"const”; /基本字 char *border10= ”,” , ";” , ”" , ”" , ”(” , ")" , "” , ”",”,”"" ; /界符 char *arithmetic6=”+” , "-" , ” , "/” , ”+” , "-"; /算术运算符 char *relation7=”<” , "<=” , ”=" , ”>" , ”=” , ”="

4、 ,”!="; /关系运算符 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) retu

5、rn(i+1); return(0); case 3: for (i=0;i=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 )mal

6、loc(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<<&qu

7、ot;错误!”; 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)) cout<alphatp<”ttt”<(atype-1)endl; else atype=search(alphatp,6); cout<alphatp<<”ttt”<<(atype-1)&l

8、t;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”<(dtype40)<<endl; return(ch);char otherprocess(char ch) int i= 1; char othertp20;

9、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<<othertp<<"ttt”<&l

10、t;(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"<(otypetp1)<endl; fp。get(ch); goto out; else othertp1='0; cout<othertp<"ttt”<(otype1)<<endl; goto out; if (ch='!)

11、 fp。get(ch); if (ch='=') cout<"!= (2,2)n"; fp.get(ch); goto out; else if (otype=search(othertp,2) cout<othertp<"ttt"<(otype-1)<<endl; fp.get(ch); goto out; if ((ch!='n')&&(ch!= )) cout<<"错误!,字符非法"<”ttt"<ch<en

12、dl; 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<

13、;"成功n"; getchar();cifa。cpp#includestdio。h>void main() cout”Hello World";【实验小结】通过这次实验,我对编译原理这门专业必修课有了进一步的深层次了解,把理论知识应用于实验中,也让我重新熟悉C语言的相关内容,加深了对C语言知识的深化和用途的理解。 实验二 LL(1)语法分析程序设计【实验目的】1熟悉判断LL(1)文法的方法及对某一输入串的分析过程.2学会构造表达式文法的预测分析表.【实验内容】编写一个语法分析程序,对于给定的输入串,能够判断识别该串是否为给定文法的句型.【实验步骤和要求】1 从键盘读入输入串,并判断正误;2 若无误,由程序自动构造FIRST、FOLLOW集以及SELECT集合,判断是否为LL(1)文法;3 若符合LL(1)文法,由程序自动构造LL

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论