《编译原理与技术》 词法分析Ⅱ -...

28
计算机科学与技术学院 13/09/2018 编译原理与技术 词法分析

Upload: others

Post on 23-Sep-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

计算机科学与技术学院

李 诚

13/09/2018

《编译原理与技术》

词法分析Ⅱ

Page 2: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

2/51

主要内容

词法分析器的自动生成 正则表达式→NFA → DFA →化简的DFA

词法分析器的生成器 Lex: flex、jflex

词法分析器 语法分析器

符号表

记号(token)

getNextToken

源程序

Fast lexical analyzer generator

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 3: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

3/23

Regular Expr to NFA

正则表达式 = Specification

有限自动机 = Implementation

NFA

Regular Expressions

LexicalSpecification

DFA

Table-drivenImplementation of DFA

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 4: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

4/23

Regular Expr to NFA

正则表达式 = Specification

有限自动机 = Implementation

二者之间的转换: 用语法制导的算法,它用正则表达式的语法结构来

指导有限自动机的构造过程

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 5: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

5/23

语法制导的构造算法

首先构造识别和字母表中一个符号a的NFA 重要特点:仅一个接受状态,它没有向外的转换

对于加括号的正则表达式(s),使用N(s)本身作为它的NFA

i开始

识别正则表达式的NFA

afi f

开始

识别正则表达式a的NFA

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 6: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

6/23

语法制导的构造算法

构造识别主算符为选择的正则表达式的NFA 重要特点:仅一个接受状态,它没有向外的转换

fi开始

识别正则表达式s | t 的NFA

N (s)

N (t)

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 7: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

7/23

语法制导的构造算法

构造识别主算符为连接的正则表达式的NFA 重要特点:仅一个接受状态,它没有向外的转换

识别正则表达式st 的NFA

i N (s)f

开始N (t)

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 8: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

8/23

语法制导的构造算法

构造识别主算符为闭包的正则表达式的NFA 重要特点:仅一个接受状态,它没有向外的转换

N (s) f开始

识别正则表达式s* 的NFA

i

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 9: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

9/23

语法制导的构造算法

由本方法产生的NFA具有下列性质: N(r)的状态数最多是r中符号和算符总数的两倍

N(r)只有一个接受状态,接受状态没有向外的转换

1 9

开始

0

a

b

a b6 7 8

2 3

4 5

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 10: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

10/23

语法制导的构造算法

由本方法产生的NFA具有下列性质: N(r)的每个状态有一个用的符号标记指向其它结

点的转换,或者最多两个指向其它结点的转换

1 9

开始

0

a

b

a b6 7 8

2 3

4 5

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 11: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

11/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 12: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

12/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 13: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

13/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 14: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

14/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 15: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

15/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 16: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

16/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 17: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

17/23

NFA构造过程举例

1 9开始

0

a

b

a b6 7 8

2 3

4 5

r9

r7 r8

r4

r3

r5 r6

*)(

r2r1

a

|

b

ab

(a|b)*ab的分解

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 18: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

18/51

NFA构造过程举例

(a|b)*ab的两个NFA的比较

1 2开始 a

0

a

b

b手工构造:

算法构造:

1 9

开始

0

a

b

a b6 7 8

2 3

4 5

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 19: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

19/51

词法分析的自动生成

从正则表达式建立识别器的步骤 从正则表达式构造NFA(已介绍) 用语法制导的算法,它用正规式语法结构来指导

构造过程

把NFA变成DFA (子集构造法,已介绍)

将DFA化简 (合并不可区别状态,也已介绍)

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 20: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

20/23

思考问题

正则表达式(a|b)*与(a*|b*)*是否等价? 提示:可利用其最简化DFA的

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 21: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

21/51

主要内容

词法分析器的自动生成 正则表达式→NFA → DFA →化简的DFA

词法分析器的生成器 Lex: flex、jflex

词法分析器 语法分析器

符号表

记号(token)

getNextToken

源程序

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 22: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

22/51

词法分析器的生成器

用Lex建立词法分析器的步骤

Lex

编译器Lex源程序lex.l lex.yy.c

C

编译器lex.yy.c a.out

a.out输入流 记号序列

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 23: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

23/23

Lex程序

包括三个部分

声明

%%

翻译规则

%%

辅助过程

Lex程序的翻译规则

p1 {动作1}

p2 {动作2}

… …

pn {动作n}

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 24: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

24/23

Lex文件举例—声明部分

%{/* 常量LT, LE, EQ, NE, GT, GE,

WHILE, DO, ID, NUMBER, RELOP的定义*/%}

/*正则定义 */delim [ \t \n ]ws {delim}+letter [A Za z]digit [09]id {letter}({letter}|{digit})*number {digit}+(\ .{digit}+)?(E[+\]?{digit}+)?

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 25: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

25/51

Lex文件举例—翻译规则部分

{ws} {/*没有动作,也不返回 */}while {return (WHILE);}do {return (DO);}{id} {yylval = install_id ( ); return (ID);}{number} {yylval = install_num( );

return (NUMBER);}“ < ” {yylval = LT; return (RELOP);}“ <= ” {yylval = LE; return (RELOP);}“ = ” {yylval = EQ; return (RELOP);}“ <> ” {yylval = NE; return (RELOP);}“ > ” {yylval = GT; return (RELOP);}“ >= ” {yylval = GE; return (RELOP);}

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 26: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

26/51

Lex文件举例—辅助过程部分

installId ( ) {/* 把词法单元装入符号表并返回指针。yytext指向该词法单元的第一个字符,yyleng给出的它的长度 */

}

installNum ( ) {/* 类似上面的过程,但词法单元不是标识符而

是数 */}

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 27: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

27/23

总结

词法分析器的作用和接口,用高级语言编写词法分析器等内容

掌握下面涉及的一些概念,它们之间转换的技巧、方法或算法 非形式描述的语言正规式

正规式 NFA

非形式描述的语言 NFA

NFA DFA

DFA 最简DFA

非形式描述的语言 DFA(或最简DFA)

2018/9/14 Cheng Li @ Compiler Fall 2018, USTC

Page 28: 《编译原理与技术》 词法分析Ⅱ - USTCstaff.ustc.edu.cn/.../compiler18/notes/Lecture3_Lexical_analysis_part… · Lexical Specification DFA Table-driven Implementation

KISS (Keep It Simple, Stupid!)

It is easier to modify a working system than to get a system working.

——Wisdom

《编译原理与技术》

词法分析Ⅱ