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

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

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

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

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

报错

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

结束

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

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

判断当前字符是否是字母

转关键字和标识符处理

判断当前字符是否是数字

转数字处理

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

转运算符处理

【程序调试】

现有源程序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];

inti;

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

intnumber(chars[],inti);

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

/*处理数字常量*/

else

i=operation(sbuff,i);

/*处理运算符*/

}

fputc('

nfp);

}

if(error)

printf("

Thereare%derrors."

error);

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

else

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)

{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,"

kb[m]);

while(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];

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<

(strcmp(str,kb[m])!

=0))

{m++;

}

if(m<

44)

{

cfile(str,"

k"

m);

}

else

{m=0;

while(m<

50&

(strcmp(str,ib[m])!

{m++;

}

if(m<

50)

{

cfile(str,"

i"

}

elseseti(str);

}

returni;

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

intnumber(charsbuff[],inti)

{chars[2],str[10];

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

voidseti(chars[]);

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

while(isdigit(sbuff[i]))

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

}

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

str[n++]='

while(m<

(strcmp(str,cb[m])!

if(isalpha(sbuff[i]))

{error2(str);

else

{

{

cfile(str,"

c"

}

elsesetc(str);

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

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]=='

))

{i++;

}

i=i+2;

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

else

{

'

{

whil

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

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

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

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