compiler construction sohail aslam lecture 9. 2 dfa minimization the generated dfa may have a large...

31
Compiler Compiler Construction Construction Sohail Aslam Lecture 9

Upload: juliana-clarke

Post on 13-Dec-2015

225 views

Category:

Documents


4 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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