利用LEX工具构造TINY语言的词法分析器

实验目的

构造 tiny 语言的词法分析器(扫描器),要求利用第三方的 lex 工具进行构造。实验结果:构造出的扫描器,能够读入教材样例中给出的 tiny 语言的示例代码,分解成 token 输出。

源文件地址

源文件地址

输入设计

{ Sample program

in TINY language -

computes factorial

}

read x; { input an integer }

if 0 < x then { don't compute if x <= 0 }

fact := 1;

repeat

fact := fact * x;

x := x - 1

until x = 0;

write fact  { output factorial of x }
end

输出设计

reserved word:read
ID,name= x
;
reserved word:if
NUM,val= 0
<
ID,name= x
reserved word:then
ID,name= fact
:=
NUM,val= 1
;
reserved word:repeat
ID,name= fact
:=
ID,name= fact
*
ID,name= x
;
ID,name= x
:=
ID,name= x
-
NUM,val= 1
reserved word:until
ID,name= x
=
NUM,val= 0
;
reserved word:write
ID,name= fact
reserved word:end

完成效果

在这里插入图片描述
在这里插入图片描述

发布了35 篇原创文章 · 获赞 1 · 访问量 1854

猜你喜欢

转载自blog.csdn.net/qq_40672635/article/details/104485203