编译原理实验报告(一)-词法分析程序.doc

上传人:聆听****声音 文档编号:1964394 上传时间:2023-05-02 格式:DOC 页数:11 大小:52.50KB
下载 相关 举报
编译原理实验报告(一)-词法分析程序.doc_第1页
第1页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第2页
第2页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第3页
第3页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第4页
第4页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第5页
第5页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第6页
第6页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第7页
第7页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第8页
第8页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第9页
第9页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第10页
第10页 / 共11页
编译原理实验报告(一)-词法分析程序.doc_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

编译原理实验报告(一)-词法分析程序.doc

《编译原理实验报告(一)-词法分析程序.doc》由会员分享,可在线阅读,更多相关《编译原理实验报告(一)-词法分析程序.doc(11页珍藏版)》请在冰点文库上搜索。

编译原理实验报告(一)-词法分析程序.doc

编译原理实验报告

(一)

----词法分析程序

【目的要求】

通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。

并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析

方法。

【题目分析】

本实验以用户指定的想编译的以C语言编写的文件作为词法分析程序的输入数据。

在进行词法分析中,先自文件头开始以行为单位扫描程序,将该行的字符读入预先设定的一个数组缓冲区中,然后对该数组的字符逐词分割,进行词法分析,将每个词分割成关键字、标识符、常量和运算符四种词种,最终产生四个相对应的表,即关键字表、标识符表、常量表和运算符表,它们以文件的形式进行存储。

除此之外,还产生一个编译后的文件,它指定了每个词在四个表中的位置。

【实验过程】

下面就词法分析程序中的文件和主要变量进行说明:

文件:

cpile.c主程序文件

key.txt关键字文件

operation.txt运算符文件

id.txt标识符文件

const.txt常量文件

after_com.txt编译后产生的文件

主要变量:

FILE*sfp,*nfp,*ifp,*kfp,*cfp,*pfp;

charib[50][20]标识符表(动态生成)

charcb[50][10]常量表(动态生成)

charkb[44][10]关键字表(预先定义好的)

charpb[36][5]运算符表(预先定义好的)

主要的子函数名:

intnumber(chars[],inti);数字处理函数

intletter(chars[],inti);字符处理函数

intoperation(chars[],inti);运算符处理函数

voidseti(chars[]);标识符建立函数

voidsetc(chars[]);常量建立函数

voidcfile(chars1[],chars2[],intm);将词和词所在表中位置写入编译后文件

voiderror1(chars[]);字符处理出错报告

voiderror2(chars[]);标识符处理出错报告

voiderror3(chars[]);运算符处理出错报告

voidopenall();打开所有文件

voidwriteall();将四个表写入文件

voidcloseall();关闭所有文件

下面简要分析一下词法分析程序的运行流程:

能否打开所要编译的C语言文件

报错

判断当前字符是否是文件结束符

结束

从源程序中读入一行到数组缓冲区

判断当前字符是否是’\n’

判断当前字符是否是字母

转关键字和标识符处理

判断当前字符是否是数字

转数字处理

判断当前字符是否是运算符

转运算符处理

【程序调试】

现有源程序a.c清单如下:

#include

intmain(intargc,char*argv[])

{charch;

inti;

ch='a';

ch=ch+32;

i=ch;

printf("%did%c\n",i,ch);/*打印*/

return0;

}

运行词法分析程序后,显示如下结果:

after_com.txt文件:

#[p--2]

include[i--0]

<[p--14]

stdio.h[i--1]

>[p--16]

int[k--2]

main[i--2]

([p--7]

int[k--2]

argc[i--3]

[p--6]

char[k--0]

*[p--9]

argv[i--4]

[[p--18]

][p--21]

)[p--8]

{[p--23]

char[k--0]

ch[i--5]

;[p--13]

int[k--2]

i[i--6]

;[p--13]

ch[i--5]

=[p--15]

'[p--19]

a[i--7]

'[p--19]

;[p--13]

ch[i--5]

=[p--15]

ch[i--5]

+[p--10]

32[c--0]

;[p--13]

i[i--6]

=[p--15]

ch[i--5]

;[p--13]

printf[k--33]

([p--7]

"[p--1]

%[p--4]

d[i--8]

id[i--9]

%[p--4]

c[i--10]

\[p--20]

n[i--11]

"[p--1]

[p--6]

i[i--6]

[p--6]

ch[i--5]

)[p--8]

;[p--13]

return[k--28]

0[c--1]

;[p--13]

}[p--25]

key.txt关键字文件:

0char

1short

2int

3unsigned

4long

5float

6double

7struct

8union

9void

10enum

11signed

12const

13volatile

14typedef

15auto

16register

17static

18extem

19break

20case

21continue

22default

23do

24else

25for

26goto

27if

28return

29switch

30while

31sizeof

32txt

33printf

34FILE

35fopen

36NULL

37fclose

38exit

39r

40read

41close

42w

43fprintf

id.txt标识符文件:

0include

1stdio.h

2main

3argc

4argv

5ch

6i

7a

8d

9id

10c

11n

operation.txt运算符文件:

0!

1"

2#

3$

4%

5&

6,

7(

8)

9*

10+

11-

12:

13;

14<

15=

16>

17?

18[

19'

20\

21]

22.

23{

24||

25}

26!

=

27>=

28<=

29==

30++

31--

32&&

33/*

34*/

const.txt常量文件:

032

10

结果完全正确。

词法分析程序如下:

#include

#include

#include

#include

#defineSIZE256

#definenull0

intline=0,error=0,mark1=0,mark2=0;

char*sname;

FILE*sfp,*nfp,*ifp,*kfp,*cfp,*pfp;

/*--------------------------------------------------------------------*/

charib[50][20];

charcb[50][10];

charkb[44][10]={"char","short","int","unsigned","long","float","double","struct","union","void",

"enum","signed","const","volatile","typedef","auto","register","static","extem",

"break","case","continue","default","do","else","for","goto",

"if","return","switch","while","sizeof","txt","printf","FILE","fopen","NULL",

"fclose","exit","r","read","close","w","fprintf"};

charpb[36][5]={"!

","\"","\#","\$","%","&",",","(",")","*","+","-",":

",";","<","=",">","?

","[","'",

"\\","]",".","{","||","}","!

=",">=","<=","==","++","--","&&","/*","*/"};

/*----------定义了四个二元数组存放四个表--------------------------------*/

intmain(intargc,char*argv[])

{charsbuff[SIZE];

charnbuff[SIZE];

inti;

/*---------------------------------------------------------------------*/

intnumber(chars[],inti);

intletter(chars[],inti);

intoperation(chars[],inti);

voidseti(chars[]);

voidsetc(chars[]);

voidcfile(chars1[],chars2[],intm);

voiderror2(chars[]);

voiderror1(chars[]);

voiderror3(chars[]);

voidopenall();

voidwriteall();

voidcloseall();

/*---------------------------------------------------------------------*/

if(argc!

=2)

{printf("Pleaseinputthefileyouwanttocompile:

\n");

exit

(1);

}

else

sname=argv[1];

sfp=fopen(argv[1],"r");

openall();

i=0;

while(i<50)

{strcpy(ib[i],"");

strcpy(cb[i],"");

i++;

}

/*---------------------------------------------------------------------*/

while(fgets(sbuff,SIZE,sfp))

{

i=0;

line++;/*以行的方式读取源代码进行编译*/

while(sbuff[i]!

='\n')

{

if(isalpha(sbuff[i]))/*处理关键字和标识符*/

i=letter(sbuff,i);

elseif(isdigit(sbuff[i]))

i=number(sbuff,i);/*处理数字常量*/

else

i=operation(sbuff,i);/*处理运算符*/

}

fputc('\n',nfp);

}

/*---------------------------------------------------------------------*/

if(error)

printf("!

Thereare%derrors.",error);/*最后错误个数报告*/

else

printf("Successtocompilethefile.");

writeall();

closeall();

return0;

}

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

voidopenall()/*打开所有必需的文件*/

{

kfp=fopen("key.txt","w+");/*打开关键字表*/

pfp=fopen("operation.txt","w+");/*打开运算符表*/

cfp=fopen("const.txt","w+");/*打开常量文件*/

ifp=fopen("id.txt","w+");/*打开标识符文件*/

nfp=fopen("after_com.txt","w+");/*打开编译后会产生的文件*/

if(kfp==NULL||cfp==NULL||ifp==NULL||pfp==NULL||nfp==NULL)

{printf("Cannotopenthesefiles");/*打开文件有错,退出程序*/

exit(0);

}

}

/*-----------------------------------------------------------------*/

voidwriteall()

{intm=0;

while(m<36&&pb[m]!

="")

{

fprintf(pfp,"%d%s%s\n",m,"",pb[m]);

m++;

}

m=0;

while(m<44&&kb[m]!

="")

{

fprintf(kfp,"%d%s%s\n",m,"",kb[m]);

m++;

}

m=0;

while(m

="")

{

fprintf(cfp,"%d%s%s\n",m,"",cb[m]);

m++;

}

m=0;

while(m

="")

{

fprintf(ifp,"%d%s%s\n",m,"",ib[m]);

m++;

}

}

/*-------------------------------------------------------------------*/

voidcloseall()/*关闭所有文件*/

{

fclose(kfp);

fclose(pfp);

fclose(cfp);

fclose(ifp);

fclose(nfp);

}

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

intletter(charsbuff[],inti)

{charstr[10];

voidcfile(chars1[],chars2[],intm);

voidseti(chars[]);

voidsetc(chars[]);

intm=0,n=0;

str[n]=sbuff[i];

while(isalnum(sbuff[i])||sbuff[i]=='.')

{str[n++]=sbuff[i++];

}

str[n]='\0';

while(m<44&&(strcmp(str,kb[m])!

=0))

{m++;

}

if(m<44)

{

cfile(str,"k",m);

}

else

{m=0;

while(m<50&&(strcmp(str,ib[m])!

=0))

{m++;

}

if(m<50)

{

cfile(str,"i",m);

}

elseseti(str);

}

returni;

}

/*-----------------------------------------------------------------------*/

intnumber(charsbuff[],inti)

{chars[2],str[10];

voidcfile(chars1[],chars2[],intm);

voidseti(chars[]);

voidsetc(chars[]);

voiderror2(chars[]);

voiderror1(chars[]);

intm=0,n=0;str[n]=sbuff[i];i++;n++;

if(sbuff[i]=='.')

{str[n]=sbuff[i];}

if((sbuff[i]=='E'||sbuff[i]=='e')&&(sbuff[i+1]=='+'||sbuff[i+1]=='-'||isdigit(sbuff[i+1])))

{str[n]=sbuff[i];str[n++]=sbuff[i++];}

while(isdigit(sbuff[i]))

{str[n++]=sbuff[i++];

}

/*对可能出现的十进制数的处理*/

str[n++]='\0';

while(m<50&&(strcmp(str,cb[m])!

=0))

{m++;

}

if(isalpha(sbuff[i]))

{error2(str);

}

else

{

if(m<50)

{

cfile(str,"c",m);

}

elsesetc(str);

}

returni;

}

/*------------------------------------------------------------------------------*/

intoperation(charsbuff[],inti)

{voiderror3(chars[]);

voidcfile(chars1[],chars2[],intm);

voidsetc(chars[]);

intk=0,m=0;chars[4];

if(sbuff[i]=='/'&&sbuff[i+1]=='*')

{i=i+2;

while(!

(sbuff[i]=='*'&&sbuff[i+1]=='/'))

{i++;

}

i=i+2;

}/*若为注释则跳过*/

else

{

if(sbuff[i]=='')

{

whil

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

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2