编译原理实验-词法分析

上传人:壹****1 文档编号:483357499 上传时间:2022-08-16 格式:DOC 页数:8 大小:102.51KB
返回 下载 相关 举报
编译原理实验-词法分析_第1页
第1页 / 共8页
编译原理实验-词法分析_第2页
第2页 / 共8页
编译原理实验-词法分析_第3页
第3页 / 共8页
编译原理实验-词法分析_第4页
第4页 / 共8页
编译原理实验-词法分析_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《编译原理实验-词法分析》由会员分享,可在线阅读,更多相关《编译原理实验-词法分析(8页珍藏版)》请在金锄头文库上搜索。

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

2、e #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 *border10= , , ; , , , ( , ) , , , ; /界符 char *arithmetic6=+ , - , * , / , + , -; /算术运算符 char *relat

3、ion7= , , = , = ,!=; /关系运算符 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)

4、 return(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

5、 *)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错误!;

6、 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

7、 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; char othertp20; int otype,otypetp; othertp0=ch; othertp1=0; if (otype=search(othertp,3) fp.get(ch); o

8、thertp1=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);

9、 goto out; else othertp1=0; coutothertpttt(otype-1)endl; goto out; if (ch=!) fp.get(ch); if (ch=) cout!= (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(

10、) 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”;【实验小结】通过这次实验,我对

11、编译原理这门专业必修课有了进一步的深层次了解,把理论知识应用于实验中,也让我重新熟悉C语言的相关内容,加深了对C语言知识的深化和用途的理解。 实验二 LL(1)语法分析程序设计【实验目的】1熟悉判断LL(1)文法的方法及对某一输入串的分析过程。2学会构造表达式文法的预测分析表。【实验内容】编写一个语法分析程序,对于给定的输入串,能够判断识别该串是否为给定文法的句型。【实验步骤和要求】1 从键盘读入输入串,并判断正误;2 若无误,由程序自动构造FIRST、FOLLOW集以及SELECT集合,判断是否为LL(1)文法;3 若符合LL(1)文法,由程序自动构造LL(1)分析表;由算法判断输入符号串是否为该文法的句型。 开始 进行语法分析判别N显示结果结束YYN是否分析下一条语句?输入字符串构造并显示分析表找出个产生式的select集找出并显示个终结符的foll

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 机械/制造/汽车 > 汽车技术

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号