Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

1

COMPILER CONSTRUCTION(CS-462)
(LECTURE 8)

______________________________________________________
INSTRUCTOR: Gul Sher Ali
DFA Minimization 2

The generated DFA


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

Idea: find groups of


equivalent states.
All transitions from
states in one group G1
go to states in the
same group G2
DFA Minimization 4

Construct the
minimized DFA such
that there is one state
for each group of
states from the initial
DFA.
DFA Minimization 5
a a
a b b
A B D E
a
b a
b
C

b
DFA for (a | b )*abb
DFA Minimization 6
b a a
a b b
A,C B D E
a
b

Minimized DFA for (a | b )*abb


Optimized Acceptor 7

RE R RE=>NFA

NFA=>DFA

Min. DFA

input w Simulate yes, if w e L(R)


string DFA no, if w e L(R)
Lexical Analyzers 8
 Lexical analyzers (scanners) use the same mechanism
 but they:
 Have multiple RE descriptions for multiple tokens
 Have a character stream at the input
Lexical Analyzers 9

 Return a sequence of matching tokens at the output (or an error)


 Always return the longest matching token
Lexical Analyzers 10

R1…R2 RE=>NFA
NFA=>DFA

Min. DFA
character Simulate Token
stream DFA stream
11

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
12

Lexical Analyzer Generators


 Flex
generates lexical analyzer in C or C++
 Jlex
written in Java. Generates lexical analyzer in Java
Using Flex 13

 Provide a specification file


 Flex reads this file and produces C or C++ output file contains
the scanner.
 The file consists of three sections
Using Flex 14

 Provide a specification file


 Flex reads this file and produces C or C++ output file contains
the scanner.
 The file consists of three sections
Using Flex 15

 Provide a specification file


 Flex reads this file and produces C or C++ output file contains
the scanner.
 The file consists of three sections
Flex Specification File 16

1C or C++ and flex definitions


Flex Specification File 17

1 C or C++ and flex definitions


%%
2 token definitions and actions
Flex Specification File 18

1 C or C++ and flex definitions


%%
2 token definitions and actions
%%
3 user code
19
Specification File lex.l 20
%{
#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);}
Specification File lex.l 21

"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]|[ ] ;
%%
File tokdefs.h 22
#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
Invoking Flex 23

lex.l flex lex.cpp


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

flex lex.l
g++ –c lex.cpp
g++ –c main.cpp
g++ –o lex.exe lex.o main.o

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

You might also like