实验安排+词法分析程序设计与实现.doc

上传人:laozhun 文档编号:2385931 上传时间:2023-02-17 格式:DOC 页数:14 大小:41.50KB
返回 下载 相关 举报
实验安排+词法分析程序设计与实现.doc_第1页
第1页 / 共14页
实验安排+词法分析程序设计与实现.doc_第2页
第2页 / 共14页
实验安排+词法分析程序设计与实现.doc_第3页
第3页 / 共14页
实验安排+词法分析程序设计与实现.doc_第4页
第4页 / 共14页
实验安排+词法分析程序设计与实现.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《实验安排+词法分析程序设计与实现.doc》由会员分享,可在线阅读,更多相关《实验安排+词法分析程序设计与实现.doc(14页珍藏版)》请在三一办公上搜索。

1、实验一 词法分析程序设计与实现一、实验目的:加深对词法分析器的工作过程的理解;加强对词法分析方法的掌握;能够采用一种编程语言实现简单的词法分析程序;能够使用自己编写的分析程序对简单的程序段进行词法分析。二、实验内容:自定义一种程序设计语言,或者选择已有的一种高级语言(C语言),编制它的词法分析程序。词法分析程序的实现可以采用任何一种编程工具。三、实验要求:1. 对单词的构词规则有明确的定义;2. 编写的分析程序能够正确识别源程序中的单词符号;3. 识别出的单词以的形式保存在符号表中;4. 词法分析中源程序的输入以.c格式,分析后的符号表保存在.txt文件中。5. *对于源程序中的词法错误,能够

2、做出简单的错误处理,给出简单的错误提示,保证顺利完成整个源程序的词法分析;6. 实验报告要求用自动机或者文法的形式对词法定义做出详细说明,说明词法分析程序的工作过程,说明错误处理的实现*。四、实验学时:6学时五、实验步骤:1. 定义目标语言的可用符号表和构词规则;2. 依次读入源程序符号,对源程序进行单词切分和识别,直到源程序结束;3. 对正确的单词,按照它的种别以的形式保存在符号表中;4. *对不正确的单词,做出错误处理*。六、选作实验学生可以根据自身的情况完善词法分析程序的错误处理功能(实验要求5&6),如对错误的单词给出准确的位置和错误类型提示。七、作业提交时间第8周实验课结束后提交词法

3、分析程序(zzwyanqiu)。八、提示1. char Scanin100,Scanout100; /用于接收输入输出文件名FILE *fin,*fout; /用于指向输入输出文件的指针2. /下面定义保留,为简化程序,使用字符指针数组保存所有保留字。/如果想增加保留字,可继续添加,并修改保留字数目#define keywordSum 8char *keywordkeywordSum= if,else,for,while,do,int,read,write;3. /下面定义纯单分界符,如需要可添加char singleword50=+-*();,:;4. /下面定义双分界符的首字符char d

4、oubleword10=!; 5. scanf(%s,Scanin); printf(请输入词法分析输出文件名(包括路径):); scanf(%s,Scanout); 6. if (fin=fopen(Scanin,r)=NULL) /判断输入文件名是否正确 printf(n打开词法分析输入文件出错!n); return(1);/输入文件出错返回错误代码1 if (fout=fopen(Scanout,w)=NULL) /判断输出文件名是否正确 printf(n创建词法分析输出文件出错!n); return(2); /输出文件出错返回错误代码2 7. ch=getc(fin);/读取文件里的一

5、个字符8. isalpha(ch) /字母判断函数isalnum(ch) /数字判断函数strcmp(token,keywordn) /串比较fprintf(fout,%st%sn,ID,token); /输出标识符符号到fout指定的文件strchr(singleword,ch) /声明: char *strchr( const char *string, int c ); /在字符串string中搜索字符c,若成功则返回一个指向该字符第一次出现的位置,否则返回NULL /这个例子中的变量x的值为5:char string=hello; char *p; int x; p=strchr(st

6、ring,o); x=p-string+1; /c库函数查找地址任务1:识别小型语言所有单词的词法分析程序设计 源程序设计语言 G . VAR:;| ,| INTEGER | ; | := IFTHENELSE WHILEDO BEGINEND |+|- |*|/ |() | 0| | |=| A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z 1|2|3|4|5|6|7|8|9 |0 要求和提示: 词法分析阶段,可以打开任意位置和名称的源文件进行词法分析,可以进行非法字符和数字后边跟字母的错误判断,如果没有错误则提示“词法分析正确完成!”,

7、并且可以选择输出token.txt(token文件)string.txt(符号表)两个文件; 1词法分析程序的主要任务如下: 组织源程序的输入,识别出源程序中的各个基本语法单位(也称为单词或语法符号),按规则转换成二元式的形式; 删除无用的空白字符、回车符、及其它非实质性符号; 删除注解行; 为后面的语法和语义分析提供二元式链表; 单词 编码 单词 编码 标识符 1 15 正整数 2 17 END 4 = 18 IF 5 19 THEN 6 = 20 ELSE 7 ; 21 WHILE 8 22 DO 9 := 23 INTEGER 10 , 24 + 11 ( 25 - 12 ) 26 *

8、13 / 14 1) 对标识符的长度控制在8个字符(包括8个)以内,超过的做截断处理; 2) 数字不大于65535,否则报错; 3) 能跳过源程序中的空白格:两个单词之间的任何空格,制表符,回车,换行都是白空格,除了用来分隔单词以外,没有意义; 4) 能跳过注释: a) 接连出现的/*到下一次接连出现的*/之间的任何文字都是注释(多行); b) 从某行接连出现的/到该行的结尾的任何文字都是注释(单行)。 3.怎样编写词法分析程序: 1) 预处理:把源文件一个字符一个字符的读入词法分析程序设置的输入字符结构体数组中(输入缓冲区),读入过程要删除注释,删除多余的白空格; 2) 从源程序字符数组中获

9、得单词, 编码为二元式.: 二元式采用结构体数组存储, 把单词类型和词元记录下来。 分解单词的方法: 1) Case多路转换语句根据单词的特点直接编写; 2) 通过描述单词的正规文法得到相应的有穷自动机,通过case多路转换语句完成有穷自动机的处理流程。 3编写词法分析程序要注意的问题: 1) 检查词法是否有错误 检查是否有非法字符:如 , &, ! 检查标志符和数字是否满足限制条件 检查注释符号是否配对 2) 符分隔单词 能够区分两个单词的符号为界符 有些界符不是单词:如白空格 有些界符仅仅用来分隔:如; 有些界符本身还是源程序不可缺少的单词,如(, ), +, /, 等等 有些界符包含两个

10、字符:如, =等等 3) 输出词法错误 如果有错误,需要报告词法错误的原因。并且要能够越过错误,分解下一个单词,直到源程序结束。 4) 输出的二元式流保存在二元式结构体数组中。#include #include #include #include #include #include using namespace std;bool isLetter(char ch) if (ch=A & ch=a & ch=0 & ch=9) return true; else return false;bool isP(char ch) if(ch=+|ch=*|ch=-|ch=/) return true

11、; /ch=:|ch=,|ch=|ch=;|ch=(|ch=) else return false;bool isJ(char ch) if(ch=,|ch=;|ch=.|ch=(|ch=)|ch=|ch=|ch=|ch=:|ch=|ch=|ch=|ch=#) return true; / else return false;bool isBlank(char ch) if(ch= |ch=t) return true; else return false;int main() string src,ste,s; char ch0,ch,ch12; char ktt4820=and,begin

12、,const,div,do,else,end,function,if,integer, not,or,procedure,program,read,real,then,type,var,while,write,标识符,无符号数, ,;,:,.,(,),.,+,-,+,-,*,/,=,=,:=,#; int pos=0; FILE *fp; fp=fopen(d:in.txt,r); ch0=fgetc(fp); while(ch0!=EOF) /if(ch0!=t)src+=ch0; src+=ch0; ch0=fgetc(fp); src+=#; coutsrcendl; ch=srcpos

13、+; ste= ; for(int j=0;j47;j+)coutjkttjendl; cout词法分析:n; while(ch!=#) char str20; if(ch!=n) if(isDigit(ch) /判断常数 int i=0; while(isDigit(ch)|ch=.) stri+=ch; /i+; ch=srcpos+; stri=0; ste=ste+|+22; coutstr; continue; else if(isLetter(ch) /判断字符 int i=0,j; while(isLetter(ch)|isDigit(ch) stri+=ch; /i+; ch=

14、srcpos+; stri=0; for(j=0;j21;j+) /判断是否关键字 int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; if(j=21)ste=ste+|+21; / cout ; coutstr; continue; else if(isP(ch) /判断是否运算符 int i=0,j; stri+=ch; stri=0; for(j=34;j38;j+) int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; sss

15、te;ssste; break; coutstr; ch=srcpos+; continue; else if(isJ(ch) /判断是否界符 int i=0,j; while(isJ(ch) stri+=ch; ch=srcpos+; stri=0; for(j=23;j47;j+) int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; coutstr; continue; else if(isBlank(ch) coutch; ch=srcpos+; continue; else couts

16、teendl; ste= ; ch=srcpos+; return 0;#include #include char *process1(); void printend(int,char); void process(); void processb(); int issymbol(char); int iskeyword(char *); int kwnum=0,valnum=0,symbolnum=0,digitnum=0,othernum=0; char keywords20010,val20030,symbol2002,digit20010,other20020; int main(

17、) char words100; clrscr(); strcpy(words,process1(); process(words, ; ); printend(kwnum, k ); printend(valnum, v ); printend(symbolnum, s ); printend(digitnum, d ); printend(othernum, o ); printf( nPress any key to continue.n ); getch(); return 0; void printend(int number,char sign) int i; if(number)

18、 for(i=1;i =number;i+) switch(sign) case k : printf( ( 1.keyword , %s )n ,keywordsi);break; case v : printf( ( %d.val , %s )n ,i+1*(kwnum!=0),vali);break; case s : printf( ( %d.operator , %s )n ,valnum+1*(kwnum!=0)+1,symboli);break; case d : printf( ( %d.digit , %s )n ,i+valnum+1*(kwnum!=0)+1*(symbo

19、lnum!=0),digiti);break; case o : printf( ( %d.others , %s )n ,i+digitnum+valnum+1*(kwnum!=0)+1*(symbolnum!=0),otheri);break; default:; char *process1() int i=0; char temp100; printf( Please input the words(end with ):n ); while(tempi=getchar()!= ) i+; tempi+= ; ; tempi= 0 ; tempi+1=i; for(i=0;tempi!

20、= 0 ;i+) if (tempi= n ) int j=i; for(;tempj!= 0 ;j+) tempj=tempj+1; i-; if (tempi= )&(tempi+1= ) int j=i+1; for(;tempj!= 0 ;j+) tempj=tempj+1; i-; return temp; void process(char *temp,char sign) int i=0; while(tempi!= 0 ) char a50; int j=0; for(;tempi!=sign;j+) if (tempi!= 0 ) aj=tempi; i+; else i-;

21、 break; aj= 0 ; if (sign= ; ) process(a, ); else processb(a); i+; void processb(char *temp) int i=0; for(;tempi!= 0 ;i+) int j=0,head=i; if (isalpha(tempi)|tempi= _ ) if (iskeyword(temp) kwnum+; strcpy(keywordskwnum,temp); break; else while (issymbol(tempi+1)=0) if (tempi!= 0 ) i+; else break; valnu

22、m+; for(j=0;head =i;j+) valvalnumj=temphead+; i+; loop: if(tempi= 0 ) /*valnum-;*/ break; if (tempi+1= = ) while(tempi+2= = ) if (tempi+1!= 0 ) i+; else break; symbolnum+; symbolsymbolnum0=tempi; symbolsymbolnum1=tempi+1; i+; if (issymbol(tempi+1)&(tempi+1!= - ) i+; else if (tempi+1= - &isdigit(temp

23、i+2) digitnum+; i+; digitdigitnum0= - ; i+; for(j=1;isdigit(tempi);j+) digitdigitnumj=tempi; i+; i-; else i+; else if (tempi!= 0 ) symbolnum+; symbolsymbolnum0=tempi; if (tempi+1= - &isdigit(tempi+2) digitnum+; i+; digitdigitnum0= - ; i+; for(j=1;isdigit(tempi);j+) digitdigitnumj=tempi; i+; i-; else

24、 break; else if (issymbol(tempi)!=0) goto loop; else if (isdigit(tempi) while (isdigit(tempi-1) i-; head=i; while(isdigit(tempi+1)!=0) if(tempi!= 0 ) i+; else break; digitnum+; for(j=0;head =i;j+) digitdigitnumj=temphead+; else while (!isalnum(tempi-1)&(!issymbol(tempi-1) if (tempi!= 0 ) i+; else break; othernum+; head=i; for(j=0;head =i;j+) otherothernumj=temphead+; int iskeyword(char *temp) char keywords3010= int , float , char , double , return , main , if , else ; int i=0; for(i=0;i , , ! , % , & , | , , , ; for(;i 30;i+) if (symbol=signi) return 1; return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号