词法分析程序实验报告Word格式.docx

上传人:b****6 文档编号:8392232 上传时间:2023-05-11 格式:DOCX 页数:12 大小:103.21KB
下载 相关 举报
词法分析程序实验报告Word格式.docx_第1页
第1页 / 共12页
词法分析程序实验报告Word格式.docx_第2页
第2页 / 共12页
词法分析程序实验报告Word格式.docx_第3页
第3页 / 共12页
词法分析程序实验报告Word格式.docx_第4页
第4页 / 共12页
词法分析程序实验报告Word格式.docx_第5页
第5页 / 共12页
词法分析程序实验报告Word格式.docx_第6页
第6页 / 共12页
词法分析程序实验报告Word格式.docx_第7页
第7页 / 共12页
词法分析程序实验报告Word格式.docx_第8页
第8页 / 共12页
词法分析程序实验报告Word格式.docx_第9页
第9页 / 共12页
词法分析程序实验报告Word格式.docx_第10页
第10页 / 共12页
词法分析程序实验报告Word格式.docx_第11页
第11页 / 共12页
词法分析程序实验报告Word格式.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

词法分析程序实验报告Word格式.docx

《词法分析程序实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《词法分析程序实验报告Word格式.docx(12页珍藏版)》请在冰点文库上搜索。

词法分析程序实验报告Word格式.docx

<

20

else

3

21

for

4

!

22

then

5

>

23

while

6

24

do

7

==

25

endfunction

8

;

26

letter(letter|digit)*

10

27

digitdigit*

11

28

+

13

{

29

-

14

}

30

*

15

#

/

16

2、主程序流程图

三、识别单词的状态转换图

Scanner()函数流程图

2.分析直接转向法和表驱动法的优缺点

状态转换图的实现通常有两种方法:

状态转换表和直接转向法

(1):

状态转换表法又称数据中心,是把状态转换图看作一种数据结构,由控制程序控制字符在其上运行,从而完成词法分析。

优点是程序短,但是占存储空间多,

(2)直接转向法又称程序中心法,是把状态转换图看成一个流程图,从状态转换图的初态开始,对它的每一个状态节点都编写一段相应的程序。

源程序如下

#include<

stdio.h>

string.h>

charch;

//扫描哪一个字符

charstoken[100];

//扫描到的单词序列

chartoken[10];

//存储已扫描到的单词

intp,m,n;

intstate;

//词法分析时进入哪一个状态

introw;

//用于标注哪一行不能识别

longintnum;

char*key[8]={"

function"

"

if"

else"

for"

then"

while"

do"

endfunction"

};

//定义好的能识别的关键字

voidscanner()

for(n=0;

n<

10;

n++)//变量初始化

token[n]=NULL;

m=0;

ch=stoken[p];

p++;

while(ch=='

'

||ch=='

\n'

)//跳过空格或是换行符

{

}

if(ch>

='

a'

&

&

ch<

z'

||ch>

A'

Z'

m=0;

while(ch>

0'

9'

{

token[m++]=ch;

ch=stoken[p++];

//扫描下一个

}

ch=stoken[--p];

state=10;

for(n=0;

n<

8;

n++)

if(strcmp(token,key[n])==0)

{

state=n+1;

break;

}

else

if(ch>

)//是数字

num=0;

while(ch>

)//可能是多位数字

num=num*10+ch-'

ch=stoken[p++];

ch=stoken[--p];

state=11;

else

switch(ch)

case'

'

:

m=0;

token[m++]=ch;

ch=stoken[p++];

if(ch=='

='

{

state=21;

token[m++]=ch;

}

else

state=20;

ch=stoken[--p];

state=24;

state=23;

case'

m=0;

token[m++]=ch;

if(ch=='

state=25;

token[m++]=ch;

state=18;

state=22;

state=-1;

}

+'

state=13;

token[0]=ch;

-'

state=14;

*'

state=15;

/'

state=16;

state=26;

('

state=27;

)'

state=28;

{'

state=29;

}'

state=30;

#'

state=0;

token[0]=ch;

default:

state=-1;

main()

p=0;

row=1;

printf("

请输入要分析的单词:

\n"

);

while(ch!

scanf("

%c"

&

ch);

stoken[p++]=ch;

printf("

词法分析如下:

do

scanner();

switch(state)

case11:

printf("

\n(%d,%d)"

num,state);

break;

case-1:

printf("

\n不能识别%d"

row);

default:

\n(%s,%d)"

token,state);

while(state!

=0);

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

当前位置:首页 > 医药卫生 > 基础医学

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

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