Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large...
-
Upload
juliana-clarke -
Category
Documents
-
view
225 -
download
4
Transcript of Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large...
![Page 1: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/1.jpg)
Compiler Compiler ConstructionConstruction
Compiler Compiler ConstructionConstruction
Sohail Aslam
Lecture 9
![Page 2: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/2.jpg)
2
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization The generated DFA may
have a large number of states.
Hopcroft’s algorithm: minimizes DFA states
![Page 3: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/3.jpg)
3
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization The generated DFA may
have a large number of states.
Hopcroft’s algorithm: minimizes DFA states
![Page 4: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/4.jpg)
4
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization Idea: find groups of
equivalent states. All transitions from
states in one group G1 go to states in the same group G2
![Page 5: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/5.jpg)
5
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization Idea: find groups of
equivalent states. All transitions from
states in one group G1 go to states in the same group G2
![Page 6: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/6.jpg)
6
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization
Construct the minimized DFA such that there is one state for each group of states from the initial DFA.
![Page 7: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/7.jpg)
7
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization
DFA for (a | b )*abb
A
a
b a
b bEB D
C
a
b
b
a
a
![Page 8: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/8.jpg)
8
DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization
Minimized DFA for (a | b )*abb
A,C
ab
b bEB D
a
b
a
a
![Page 9: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/9.jpg)
9
Optimized AcceptorOptimized AcceptorOptimized AcceptorOptimized Acceptor
input string
RE
w
R
yes, if w L(R)
no, if w L(R)
RE=>NFA
NFA=>DFA
Min. DFA
SimulateDFA
![Page 10: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/10.jpg)
10
Lexical AnalyzersLexical AnalyzersLexical AnalyzersLexical Analyzers Lexical analyzers (scanners)
use the same mechanism but they:
• Have multiple RE descriptions for multiple tokens
• Have a character stream at the input
![Page 11: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/11.jpg)
11
Lexical AnalyzersLexical AnalyzersLexical AnalyzersLexical Analyzers Lexical analyzers (scanners)
use the same mechanism but they:
• Have multiple RE descriptions for multiple tokens
• Have a character stream at the input
![Page 12: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/12.jpg)
12
Lexical AnalyzersLexical AnalyzersLexical AnalyzersLexical Analyzers Lexical analyzers (scanners)
use the same mechanism but they:
• Have multiple RE descriptions for multiple tokens
• Have a character stream at the input
![Page 13: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/13.jpg)
13
Lexical AnalyzersLexical AnalyzersLexical AnalyzersLexical Analyzers
• Return a sequence of matching tokens at the output (or an error)
• Always return the longest matching token
![Page 14: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/14.jpg)
14
Lexical AnalyzersLexical AnalyzersLexical AnalyzersLexical Analyzers
• Return a sequence of matching tokens at the output (or an error)
• Always return the longest matching token
![Page 15: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/15.jpg)
15
Lexical AnalyzersLexical AnalyzersLexical AnalyzersLexical Analyzers
characterstream
R1…R2
Tokenstream
RE=>NFA
NFA=>DFA
Min. DFA
SimulateDFA
R1…R2
![Page 16: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/16.jpg)
16
Lexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsThe lexical analysis process
can automatedWe only need to specify
• Regular expressions for tokens
• Rule priorities for multiple longest match cases
![Page 17: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/17.jpg)
17
Lexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsThe lexical analysis process
can automatedWe only need to specify
• Regular expressions for tokens
• Rule priorities for multiple longest match cases
![Page 18: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/18.jpg)
18
Lexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsFlex
generates lexical analyzer in C or C++
Jlexwritten in Java. Generates lexical analyzer in Java
![Page 19: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/19.jpg)
19
Lexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsLexical Analyzer GeneratorsFlex
generates lexical analyzer in C or C++
Jlexwritten in Java. Generates lexical analyzer in Java
![Page 20: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/20.jpg)
20
Using FlexUsing FlexUsing FlexUsing FlexProvide a specification file
Flex reads this file and produces C or C++ output file contains the scanner.
The file consists of three sections
![Page 21: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/21.jpg)
21
Using FlexUsing FlexUsing FlexUsing FlexProvide a specification file
Flex reads this file and produces C or C++ output file contains the scanner.
The file consists of three sections
![Page 22: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/22.jpg)
22
Using FlexUsing FlexUsing FlexUsing FlexProvide a specification file
Flex reads this file and produces C or C++ output file contains the scanner.
The file consists of three sections
![Page 23: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/23.jpg)
23
Flex Specification FileFlex Specification FileFlex Specification FileFlex Specification File
C or C++ and flex definitions 1
![Page 24: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/24.jpg)
24
Flex Specification FileFlex Specification FileFlex Specification FileFlex Specification File
C or C++ and flex definitions
%%
token definitions and actions
1
2
![Page 25: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/25.jpg)
25
Flex Specification FileFlex Specification FileFlex Specification FileFlex Specification FileC or C++ and flex definitions
%%
token definitions and actions
%%
user code 3
1
2
![Page 26: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/26.jpg)
26
Specification File Specification File lex.llex.lSpecification File Specification File lex.llex.l%{#include “tokdefs.h”%}D [0-9]L [a-zA-Z_]id {L}({L}|{D})*%%"void" {return(TOK_VOID);}"int" {return(TOK_INT);}"if" {return(TOK_IF);}
![Page 27: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/27.jpg)
27
Specification File Specification File lex.llex.lSpecification File Specification File lex.llex.l"else" {return(TOK_ELSE);}"while"{return(TOK_WHILE)};"<=" {return(TOK_LE);}">=" {return(TOK_GE);}"==" {return(TOK_EQ);}"!=" {return(TOK_NE);}{D}+ {return(TOK_INT);}{id} {return(TOK_ID);}[\n]|[\t]|[ ];%%
![Page 28: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/28.jpg)
28
File File tokdefs.htokdefs.hFile File tokdefs.htokdefs.h#define TOK_VOID 1#define TOK_INT 2#define TOK_IF 3#define TOK_ELSE 4#define TOK_WHILE 5#define TOK_LE 6#define TOK_GE 7#define TOK_EQ 8#define TOK_NE 9#define TOK_INT 10#define TOK_ID 111
![Page 29: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/29.jpg)
29
Invoking FlexInvoking FlexInvoking FlexInvoking Flex
lex.l lex.cppflex
![Page 30: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/30.jpg)
30
Using Generated ScannerUsing Generated ScannerUsing Generated ScannerUsing Generated Scannervoid main(){ FlexLexer lex; int tc = lex.yylex(); while(tc != 0) cout << tc << “,” <<lex.YYText() << endl; tc = lex.yylex();}
![Page 31: Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization The generated DFA may have a large number of states. Hopcroft’s algorithm: minimizes.](https://reader034.fdocuments.in/reader034/viewer/2022042522/56649f575503460f94c7c869/html5/thumbnails/31.jpg)
31
Creating Scanner EXECreating Scanner EXECreating Scanner EXECreating Scanner EXE
flex lex.lg++ –c lex.cppg++ –c main.cppg++ –o lex.exe lex.o main.o
lex <main.cpp