编译原理实验报告一词法分析程序Word格式.docx

上传人:b****5 文档编号:16180676 上传时间:2022-11-21 格式:DOCX 页数:18 大小:49.71KB
下载 相关 举报
编译原理实验报告一词法分析程序Word格式.docx_第1页
第1页 / 共18页
编译原理实验报告一词法分析程序Word格式.docx_第2页
第2页 / 共18页
编译原理实验报告一词法分析程序Word格式.docx_第3页
第3页 / 共18页
编译原理实验报告一词法分析程序Word格式.docx_第4页
第4页 / 共18页
编译原理实验报告一词法分析程序Word格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

编译原理实验报告一词法分析程序Word格式.docx

《编译原理实验报告一词法分析程序Word格式.docx》由会员分享,可在线阅读,更多相关《编译原理实验报告一词法分析程序Word格式.docx(18页珍藏版)》请在冰豆网上搜索。

编译原理实验报告一词法分析程序Word格式.docx

【程序调试】

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

#include<

stdio.h>

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]

argc[i--3]

[p--6]

char[k--0]

*[p--9]

argv[i--4]

[[p--18]

][p--21]

)[p--8]

{[p--23]

ch[i--5]

[p--13]

i[i--6]

=[p--15]

'

[p--19]

a[i--7]

+[p--10]

32[c--0]

printf[k--33]

"

[p--1]

%[p--4]

d[i--8]

id[i--9]

c[i--10]

\[p--20]

n[i--11]

return[k--28]

0[c--1]

}[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<

string.h>

stdlib.h>

ctype.h>

#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]={"

!

\"

\#"

\$"

%"

("

)"

*"

+"

-"

:

="

?

["

\\"

]"

."

{"

||"

}"

=="

++"

--"

/*"

*/"

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

{charsbuff[SIZE];

charnbuff[SIZE];

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

intletter(chars[],inti);

intoperation(chars[],inti);

voidseti(chars[]);

voidsetc(chars[]);

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

voiderror2(chars[]);

voiderror1(chars[]);

voiderror3(chars[]);

if(argc!

=2)

{printf("

Pleaseinputthefileyouwanttocompile:

\n"

);

exit

(1);

else

sname=argv[1];

sfp=fopen(argv[1],"

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);

/*处理数字常量*/

i=operation(sbuff,i);

/*处理运算符*/

}

fputc('

nfp);

}

if(error)

Thereare%derrors."

error);

/*最后错误个数报告*/

Successtocompilethefile."

writeall();

closeall();

return0;

}

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

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

{

kfp=fopen("

key.txt"

w+"

/*打开关键字表*/

pfp=fopen("

operation.txt"

/*打开运算符表*/

cfp=fopen("

const.txt"

/*打开常量文件*/

ifp=fopen("

id.txt"

/*打开标识符文件*/

nfp=fopen("

after_com.txt"

/*打开编译后会产生的文件*/

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

Cannotopenthesefiles"

/*打开文件有错,退出程序*/

exit(0);

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

voidwriteall()

{intm=0;

while(m<

36&

pb[m]!

{

fprintf(pfp,"

%d%s%s\n"

m,"

pb[m]);

m++;

m=0;

44&

kb[m]!

fprintf(kfp,"

kb[m]);

mark2&

cb[m]!

fprintf(cfp,"

cb[m]);

mark1&

ib[m]!

fprintf(ifp,"

ib[m]);

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

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

fclose(kfp);

fclose(pfp);

fclose(cfp);

fclose(ifp);

fclose(nfp);

intletter(charsbuff[],inti)

{charstr[10];

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<

(strcmp(str,kb[m])!

=0))

{m++;

if(m<

44)

cfile(str,"

k"

m);

{m=0;

50&

(strcmp(str,ib[m])!

if(m<

50)

{

cfile(str,"

i"

}

elseseti(str);

returni;

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

intnumber(charsbuff[],inti)

{chars[2],str[10];

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++];

while(isdigit(sbuff[i]))

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

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

str[n++]='

(strcmp(str,cb[m])!

if(isalpha(sbuff[i]))

{error2(str);

else

{

cfile(str,"

c"

elsesetc(str);

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

intoperation(charsbuff[],inti)

{voiderror3(chars[]);

intk=0,m=0;

chars[4];

if(sbuff[i]=='

/'

sbuff[i+1]=='

*'

{i=i+2;

while(!

(sbuff[i]=='

))

{i++;

i=i+2;

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

'

while(sbuff[i]=='

s[k]=sbuff[i];

while(k<

3)

{s[k++]=sbuff[i++];

while(k>

=0)

{s[k]='

(strcmp(s,pb[m])!

if(m<

36)

cfile(s,"

p"

break;

else{k--;

i--;

if(k==-1)

{i++;

error3(s);

}

returni;

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

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

fprintf(nfp,"

%s[%s--%d]\n"

s1,s2,m);

return;

voidseti(chars[])

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

strcpy(ib[mark1],s);

cfile(s,"

mark1);

mark1++;

return;

voidsetc(chars[])

strcpy(cb[mark2],s);

mark2);

mark2++;

voiderror1(chars[])

{error++;

printf("

line%d:

%sisanillegalletter.\n"

line,s);

return;

voiderror2(chars[])

%sisanillegalword.\n"

voiderror3(chars[])

printf("

%sisanillegaloperation.\n"

【编程体会】

.编写程序之前应该要有一个整体的规划,然后在考虑细节问题,不断地发现问题,修改程序,使之精益求精。

B.通过本实验,我更加深了对文件操作的理解,能够较好的掌握文件操作的基本要领。

C.应该培养良好的程序设计风格。

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

当前位置:首页 > PPT模板 > 卡通动漫

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1