编译原理实验报告(一)----词法分析程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
("","");
* 打开关键字表 *
("","");
* 打开运算符表 *
("","");
* 打开常量文件 *
("","");
* 打开标识符文件 *
("","");
* 打开编译后会产生的文件 *
(
)
{ (" ");
* 打开文件有错,退出程序 *
();
}
}
**
()
{;
(<[]" ")
{
(,"\", ,"
编译原理实验报告(一)
----词法分析程序 【目的要求】
通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。 并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析 方法。
【题目分析】
本实验以用户指定的想编译的以语言编写的文件作为词法分析程序的输入数据。
在进行词法分析中,先自文件头开始以行为单位扫描程序,将该行的字符读入预先设定的一个
[] [] ;[]
}[]
关键字文件:
43 标识符文件:
运算符文件: ! "
$
, ( ) *
: ; <
> ? [ ' \ ] . {
}
> <
*
34
*
常量文件:
1 结果完全正确。 词法分析程序如下:
<> <> <> <>
; *; *,*,*,*,*,*;
** [][]; [][]; [][]{"","","","","","","","","","",
[] [] ([] [] [] ,[] [] *[] [] [[] ][] )[]
是
转关键字和标识符处理 是 转数字处理 是
转运算符处理
{[] [] [] ;[]
[] [] ;[]
[] [] '[] [] '[] ;[]
[] [] [] [] [] ;[]
[] [] [] ;[]
[] ([] "[] [] [] [] [] [] \[] [] "[] ,[] [] ,[] [] )[] ;[]
* 处理数字常量 *
} **
() ("
(); * 处理运算符 * } ('\');
.");
* 最后错误个数报告 *
(" .");
();
();
;
}
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*
()
* 打开所有必需的文件 *
( [] )
{ [];
( [], [] );
( []);
( []);
;
[][];
(([])[]'.')
{ [][];
}
[]'\'; (<(([])))
{; }
(<) {
(,""); }
{; (<(([])))
{; }
(<) {
(,""); }
(); }
;
} **
( [] ) { [][];
( [], [] ); ( []); ( []);
"\\","]",".","{","","}","",">","<","","","","","*","*"}; *定义了四个二元数组存放四个表*
( , *[]) { [];
[]; ; **
( [] ); ( [] );
( [] ); ( []); ( []);
( [], [] ); ( []); ( []);
( []);
标识符建立函数
( []);
常量建立函数
( [], [] ); 将词和词所在表中位置写入编译后文件
( []);
字符处理出错报告
( []);
标识符处理出错报告
( []);
运算符处理出错报告
();
打开所有文件
();
将四个表写入文件
();
关闭所有文件
下面简要分析一下词法分析程序的运行流程:
能否打开所要编译的语言文件
"[]);
;
}
;
(<[]" ")
{
(,"\", ,"
"[]);
;
}
;
(<[]" ")
{
} ;
(<[]" ") {
}
(,"\", ," ;
(,"\", ," ;
"[]); "[]);
}
**
()
* 关闭所有文件 *
{
();
();
();
();
();
}
*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*
( []); (); (); ();
**
() { (" (); }
:\");
[]; ([],""); (); ;
(< ) {([]," ");
([]," "); ; } **
(()) {
; ; ([]'\') { (([])) *
(); (([])) ();
*以行的方式读取源代码进行编译* 处理关键字和标识符 *
数组缓冲区中,然后对该数组的字符逐词分割,进行词法分析,将每个词分割成关键字、标识
符、常量和运算符四种词种,最终产生四个相对应的表,即关键字表、标识符表、常量表和运
算符表,它们以文件的形式进行存储。除此之外,还产生一个编译后的文件,它指定了每个词
在四个表中的位置。
【实验过程】
下面就词法分析程序中的文件和主要变量进行说明:
否
能 判断当前字符是否是文件结束符
否 从源程序中读入一行到数组缓冲区
报错 是
结束
是
判断当前字符是否是’\’
否
判断当前字符是否是字母
否 判断当前字符是否是数字
否 判断当前字符是否是运算符
否
【程序调试】 现有源程序清单如下:
<> ( , *[]) {;
; '';
; ; (" \")*打印*
; } 运行词法分析程序后,显示如下结果: 文件: [] [] <[] [] >[]
文件:
主程序文件
关键字文件
运算符文件
标识符文件
常Baidu Nhomakorabea文件
编译后产生的文件
主要变量:
*,*,*,*,*,*;
[][] 标识符表(动态生成)
[][] 常量表(动态生成)
[][] 关键字表(预先定义好的)
[][]
运算符表(预先定义好的)
主要的子函数名:
( [] );
数字处理函数
( [] );
字符处理函数
( [] ); 运算符处理函数
"","","","","","","","","",
"","","","","","","","",
"","","","","","","","","","",
"","","","","","",""};
[][]{"!","\"","\","\$","","",",","(",")","*","","",":",";","<","",">","?","[","'",
( []); ( []); [][];
([]'.') {[][];}
(([]'' []'') ([]'' []'' ([]))) {[][][][];}
(([])) {[][]; }
*对可能出现的十进制数的处理* []'\'; (<(([]))) {; } (([])) {(); }