Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes.

Similar presentations


Presentation on theme: "Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes."— Presentation transcript:

1 Compiler Construction Sohail Aslam Lecture 9

2 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes DFA states

3 3 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes DFA states

4 4 DFA Minimization  Idea: find groups of equivalent states.  All transitions from states in one group G 1 go to states in the same group G 2

5 5 DFA Minimization  Idea: find groups of equivalent states.  All transitions from states in one group G 1 go to states in the same group G 2

6 6 DFA Minimization  Construct the minimized DFA such that there is one state for each group of states from the initial DFA.

7 7 DFA Minimization DFA for (a | b )*abb A a ba bb E BD C a b b a a

8 8 DFA Minimization Minimized DFA for (a | b )*abb A,C a b bb E BD a b a a

9 9 Optimized Acceptor input string RE w R yes, if w  L(R) no, if w  L(R) RE=>NFA NFA=>DFA Min. DFA Simulate DFA

10 10 Lexical Analyzers  Lexical analyzers (scanners) use the same mechanism  but they: Have multiple RE descriptions for multiple tokens Have a character stream at the input

11 11 Lexical Analyzers  Lexical analyzers (scanners) use the same mechanism  but they: Have multiple RE descriptions for multiple tokens Have a character stream at the input

12 12 Lexical Analyzers  Lexical analyzers (scanners) use the same mechanism  but they: Have multiple RE descriptions for multiple tokens Have a character stream at the input

13 13 Lexical Analyzers Return a sequence of matching tokens at the output (or an error) Always return the longest matching token

14 14 Lexical Analyzers Return a sequence of matching tokens at the output (or an error) Always return the longest matching token

15 15 Lexical Analyzers character stream R 1 …R 2 Token stream RE=>NFA NFA=>DFA Min. DFA Simulate DFA R 1 …R 2

16 16 Lexical Analyzer Generators  The lexical analysis process can automated  We only need to specify Regular expressions for tokens Rule priorities for multiple longest match cases

17 17 Lexical Analyzer Generators  The lexical analysis process can automated  We only need to specify Regular expressions for tokens Rule priorities for multiple longest match cases

18 18 Lexical Analyzer Generators  Flex generates lexical analyzer in C or C++  Jlex written in Java. Generates lexical analyzer in Java

19 19 Lexical Analyzer Generators  Flex generates lexical analyzer in C or C++  Jlex written in Java. Generates lexical analyzer in Java

20 20 Using Flex  Provide a specification file  Flex reads this file and produces C or C++ output file contains the scanner.  The file consists of three sections

21 21 Using Flex  Provide a specification file  Flex reads this file and produces C or C++ output file contains the scanner.  The file consists of three sections

22 22 Using Flex  Provide a specification file  Flex reads this file and produces C or C++ output file contains the scanner.  The file consists of three sections

23 23 Flex Specification File C or C++ and flex definitions 1

24 24 Flex Specification File C or C++ and flex definitions % token definitions and actions 1 2

25 25 Flex Specification File C or C++ and flex definitions % token definitions and actions % user code 3 1 2

26 26 Specification File lex.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);}

27 27 Specification File lex.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]|[ ]; %

28 28 File tokdefs.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

29 29 Invoking Flex lex.l lex.cpp flex

30 30 Using Generated Scanner void main() { FlexLexer lex; int tc = lex.yylex(); while(tc != 0) cout << tc << “,” <<lex.YYText() << endl; tc = lex.yylex(); }

31 31 Creating Scanner EXE flex lex.l g++ –c lex.cpp g++ –c main.cpp g++ –o lex.exe lex.o main.o lex <main.cpp


Download ppt "Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes."

Similar presentations


Ads by Google