Module 1
Module 1
Error
Messages
Interpreter
• Interpreter is a translator that reads a program written in source
language and translates it into an equivalent program in target
language line by line
Assembly Error
Messages Machine Code
Code
Compiler
• A compiler is a translator that reads a program written in source
language and translates it into an equivalent program in target
language. (Object/Machine Language/0’s, 1’s).
Synthesis
Analysis phase phase
Lexical
analysis Intermediate Code
code optimization
Syntax
analysis generation
Code
Semantic
generation
analysis
Lexical analysis
• Lexical Analysis is also called linear analysis or
scanning. Position = initial + rate * 60
• Lexical Analyzer divides the given source
statement into the tokens.
• Ex: Position = initial + rate * 60 would be Lexical analysis
grouped into the following tokens:
id1 = id2 + id3 * 60
Position (identifier)
= (Assignment operator)
initial (identifier)
+ (Plus Operator)
rate (identifier)
* (Multiplication Operator)
60 (digit/Number)
Lexeme
• Reads the stream of char making up the source program &
group the char into meaningful sequences called lexeme.
• Lexical analyzer represents the lexeme in the form of tokens.
• It is a sequence of characters in the source code that are matched
by given predefined language rules for every lexeme to be
specified as a valid token.
• Example:
• main is lexeme of type keyword(token)
• (,),{,} are lexemes of type punctuation(token)
Patterns
• Specifies a set of rules that a scanner follows to create a token.
• Example:
• For a keyword to be identified as a valid token, the pattern is the
sequence of characters that make the keyword.
Synthesis
Analysis phase phase
Lexical
analysis Intermediate Code
code optimization
Syntax
analysis generation
Code
Semantic
generation
analysis
Syntax analysis
Position = initial + rate*60
id3 60
Phases of compiler
Compiler
Synthesis
Analysis phase phase
Lexical
analysis Intermediate Code
code optimization
Syntax
analysis generation
Code
Semantic
generation
analysis
Semantic analysis
• Semantic analyzer determines the meaning =
of a source string. id1 +
• It performs following operations: id2 * int to
1. Matching of parenthesis in the expression. real
id3 60
2. Matching of if..else statement.
3. Performing arithmetic operation that are type
Semantic analysis
compatible.
4. Checking the scope of operation. =
*Note: Consider id1, id2 and id3 are real
id1 +
id2 *
id3 inttoreal
60
Phases of compiler
Compiler
Synthesis
Analysis phase phase
Lexical
analysis Intermediate Code
code optimization
Syntax
analysis generation
Code
Semantic
generation
analysis
Intermediate code generator
• Two important properties of intermediate =
code : +
id1
1. It should be easy to produce.
id2 *
2. Easy to translate into target program.
t3 id3 inttoreal
• Intermediate form can be represented using t2 t1
60
“three address code”.[Postfix notation & Syntax
Analysis tree] Intermediate code
Synthesis
Analysis phase phase
Lexical
analysis Intermediate Code
code optimization
Syntax
analysis generation
Code
Semantic
generation
analysis
Code optimization
• It improves the intermediate code.
• This is necessary to have a faster Intermediate code
execution of code or less consumption
t1= int to real(60)
of memory. t2= id3 * t1
t3= t2 + id2
id1= t3
Code optimization
Synthesis
Analysis phase phase
Lexical
analysis Intermediate Code
code optimization
Syntax
analysis generation
Code
Semantic
generation
analysis
Code generation
• The intermediate code instructions are
translated into sequence of machine Code optimization
instruction.
t1= id3 * 60.0
id1 = id2 + t1
Code generation
MOV id3, R2
MUL #60.0, R2
MOV id2, R1
ADD R2,R1
MOV R1, id1
Id3→R2
Id2→R1
Symbol table
• Symbol table are data structures that are used by compilers to hold
information about source-program constructs.
• It is used to store information about the occurrences of various entities
such as, objects, classes, variable names, functions, etc.,
• It is used by both analysis phase and synthesis phase.
• Symbol table is used for the following purposes
• It is used to store the name of all the entities in a structured form at one place
• It is used to verify if a variable has been declared
• It is used to determine the scope of a name
• It is used to implement type checking by verifying assignments and
expression in the source code are semantically correct.
Cont.,
• Symbol table can be a linear (Linked list) or hash table
• It maintain a entry for each name as,
• <symbol name, type, attribute>
• Example:
• If a symbol table has to store information about the following
variable declaration:
• static int interest;
• then it should store the entry such as:
• <interest, int, static>
Operations of Symbol table
Scope Management
void pro_one() void pro_two()
{ {
int one_1; int two_1;
int one_2; int two_2;
{ \ { \
int one_3; |_ inner scope 1 int two_3; |_ inner scope 3
int one_4; | int two_4; |
} / } /
Analysis
Lexical analysis Phase
Syntax analysis
Semantic analysis
Symbol
Error detection
table
and recovery
Intermediate code
Back end
Depends on target machine and do not depends on source program.
It includes following phases:
1. Code optimization
2. Code generation phase
3. Error handling and symbol table operation
Context of Compiler
(Cousins of compiler)
Context of compiler (Cousins of compiler)
Skeletal Source Program
• In addition to compiler, many other
system programs are required to Preprocessor
generate absolute machine code. Source
Program
• These system programs are: Compiler
Target Assembly
• Preprocessor Program
• Assembler Assembler
• Linker Relocatable
• Loader Object Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
Context of compiler (Cousins of compiler)
Skeletal Source Program
Preprocessor
Some of the task performed by preprocessor: Preprocessor
Target Assembly
Program
Assembler
Relocatable
Object Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
Context of compiler (Cousins of compiler)
Skeletal Source Program
Assembler
Preprocessor
Assembler is a translator which takes the
assembly program (mnemonic) as an input Source
Program
and generates the machine code as an output.
Compiler
Target Assembly
Program
Assembler
Relocatable
Object Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
Context of compiler (Cousins of compiler)
Skeletal Source Program
Linker
Linker makes a single program from a several files of Preprocessor
relocatable machine code. Source
Program
These files may have been the result of several
Compiler
different compilation, and one or more library files.
Target Assembly
Loader Program
Assembler
The process of loading consists of:
Taking relocatable machine code Relocatable
Machine Code
Altering the relocatable address
Libraries & Linker / Loader
Placing the altered instructions and data in Object Files
main memory at the proper location.
Absolute Machine
Code
Pass structure
Pass structure
• One complete scan of a source program is called pass.
• Pass includes reading an input file and writing to the output file.
• In a single pass compiler analysis of source statement is immediately
followed by synthesis of equivalent target statement.
• In a two pass compiler intermediate code is generated between analysis
and synthesis phase.
• It is difficult to compile the source program into single pass due to:
forward reference
Pass structure
Forward reference: A forward reference of a program entity is a reference
to the entity which precedes its definition in the program.
• Can be solved by postponing the generation of target code until more
information concerning the entity becomes available.
• It leads to multi pass model of compilation.
Pass I:
Pass II:
Symbol Table
• Upon receiving a “Get next token” command from parser, the lexical
analyzer reads the input character until it can identify the next token.
Lexical Analysis
• Secondary tasks:
• Stripping out from the source program comments and white
spaces in the form of blank, tab, and new line characters.
• Correlating error messages from the compiler with the
source program.
• Inserting lexemes for user-defined names into the symbol
table.
Issues in Lexical and Syntax Analysis
Why to separate lexical analysis & parsing?
1. Simplicity in design
• Separation allows the simplification of one or the other.
• Example: A parser with comments or white spaces is more complex
2. Improves compiler efficiency
• Optimization of lexical analysis because a large amount of time is
spent reading the source program and partitioning it into tokens.
3. Enhance compiler portability
• Input alphabet peculiarities and other device-specific anomalies can
be restricted to the lexical analyzer.
Token, Pattern & Lexemes
Token Pattern
The set of rules called pattern
Sequence of character having a
associated with a token.
collective meaning is known as
Example: “non-empty sequence of digits”,
token. “letter followed by letters and digits”
Categories of Tokens:
1.Identifier Lexemes
= Operator1
Tokens
sum Identifier2
+ Operator2
45 Constant1
Lexemes
Lexemes of identifier: total, sum
Lexemes of operator: =, +
Lexemes of constant: 45
Attributes of Tokens
•The
When
token
more
names
than and
one associated
lexeme canattribute
match a values
pattern,for
thethe
lexical
Fortran
analyzer
statement
must
E =provide
M * C **the
2 subsequent compiler phases additional information about the
areparticular
written below
lexeme
asthat
a sequence
matched.
of pairs.
<id,• pointer
For example,
to symbol-table
the pattern
entry
for for
token
E> number matches both 0 and 1
•<assign
The lexical
op> analyzer returns to the parser not only a token name, but an
<id,
attribute
pointervalue
to symbol-table
that describes
entry
thefor
lexeme
M> represented by the token;
• Normally,
<mult op> information about an identifier e.g., its lexeme, its type, and the
<id, pointer
location
to symbol-table
at which it isentry
firstfor
found
C> is kept in the symbol table. Thus, the
<exp op>
appropriate attribute value for an identifier is a pointer to the symbol-table
<number,
entryinteger
for thatvalue
identifier.
2>
Divide the following program into appropriate lexemes
• The lexical analysis scans the input string from left to right one character at a time.
• A specialized buffering techniques have been developed to reduce the amount of
overhead required to process a single input character
• Buffer divided into two N-character halves, where N is the number of character on
one disk block [size - 4096 bytes].
• Using one system read command we can read N characters into a buffer.
• If fewer than N characters remain in the input file, then a special character, represented by eof
: : : E : : = : : Mi : * : : : C: * : * : 2 : eof : : :
Buffer pairs
: : : E : : = : : Mi : * : : : C: * : * : 2 : eof : : :
forward forward
lexeme_beginnig
forward forward
lexeme_beginnig
Code to advance forward pointer
if forward at end of first half then begin
reload second half;
forward := forward + 1;
end
else if forward at end of second half then begin
reload first half;
move forward to beginning of first half;
end
else forward := forward + 1;
Sentinels
: : E : : = : : Mi : * : eof : C: * : * : 2 : eof : : eof
forward
lexeme_beginnig
forward
forward forward
lexeme_beginnig
forward := forward + 1;
if forward = eof then begin
if forward at end of first half then begin
reload second half;
forward := forward + 1;
end
else if forward at end of second half then begin
reload first half;
move forward to beginning of first half;
end
else terminate lexical analysis;
end
Specification of tokens
Strings and languages
Term Definition
Prefix of s A string obtained by removing zero or more trailing
symbol of string S.
e.g., ban is prefix of banana.
Suffix of S A string obtained by removing zero or more leading
symbol of string S.
e.g., nana is suffix of banana.
Sub string of S A string obtained by removing prefix and suffix from S.
e.g., nan is substring of banana
Proper prefix, suffix Any nonempty string x that is respectively proper prefix,
and substring of S suffix or substring of S, such that s≠x.
Subsequence of S A string obtained by removing zero or more not
necessarily contiguous symbol from S.
e.g., baaa is subsequence of banana.
Exercise
• Write prefix, suffix, substring, proper prefix, proper suffix and
subsequence of following string:
String: Compiler
Operations on languages
Operation Definition
Union of L and M L U M = {s | t is in L or t is in M }
Written as L U M
Concatenation of L
and M LM = {st | s is in L and t is in M }
Written as LM
Kleene closure of L
L∗ denotes “zero or more concatenation of” L.
Written as L∗
Positive closure of L
L+ denotes “one or more concatenation of” L.
Written as L+
Regular Expression &
Regular Definition
Regular expression
• A regular expression is a sequence of characters that define a pattern.
Notational shorthand's
1. One or more instances: +
2. Zero or more instances: *
3. Zero or one instances: ?
4. Alphabets: Σ
Rules to define regular expression
1. ∈ is a regular expression that denotes {∈}, the set containing empty
string.
2. If 𝑎 is a symbol in 𝛴 then 𝑎 is a regular expression, 𝐿 𝑎 = {𝑎}
3. Suppose 𝑟 and 𝑠 are regular expression denoting the languages 𝐿 𝑟
and 𝐿 𝑠 . Then,
a. r | s is a regular expression denoting L r U L(s)
b. r s is a regular expression denoting L r L s
∗
c. r * is a regular expression denoting L r
d. r is a regular expression denoting L( r )
𝜖
a
aa Infinite
aaa
…..
aaaa
aaaaa…..
Regular expression
• L = One or More Occurrences of a = a+
a
aa
aaa Infinite …..
aaaa
aaaaa…..
Precedence and associativity of operators
Operator Precedence Associative
Kleene * 1 left
Concatenation . 2 left
Union | 3 left
Regular expression examples
1. 0 or 1
𝐒𝐭𝐫𝐢𝐧𝐠𝐬: 𝟎, 𝟏 𝐑. 𝐄. = 𝟎 | 𝟏
2. 0 or 11 or 111
𝐒𝐭𝐫𝐢𝐧𝐠𝐬: 𝟎, 𝟏𝟏, 𝟏𝟏𝟏 𝐑. 𝐄. = 𝟎 𝟏𝟏 𝟏𝟏𝟏
17. All binary string with at least 3 characters and 3rd character should be zero
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟎, 𝟏𝟎𝟎, 𝟏𝟏𝟎𝟎, 𝟏𝟎𝟎𝟏… 𝑹. 𝑬. = 𝟎 𝟏 𝟎 𝟏 𝟎(𝟎 | 𝟏) ∗
18. Language which consist of exactly two b’s over the set 𝛴 = {𝑎, 𝑏}
∗ ∗ ∗
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒃𝒃, 𝒃𝒂𝒃, 𝒂𝒂𝒃𝒃, 𝒂𝒃𝒃𝒂… 𝑹. 𝑬. = 𝒂 𝒃 𝒂 𝒃 𝒂
Regular expression examples
24. The language with 𝛴 = {𝑎, 𝑏, 𝑐} where 𝑎 should be multiple of 3
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒂𝒂, 𝒃𝒂𝒂𝒂, 𝒃𝒂𝒄𝒂𝒃𝒂, 𝒂𝒂𝒂𝒂𝒂𝒂. . 𝑹. 𝑬. = ( 𝒃|𝒄 ∗ 𝒂 𝒃|𝒄 ∗ 𝒂 𝒃|𝒄 ∗ 𝒂 𝒃|𝒄 ∗ )∗
25. Even no. of 0
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎, 𝟎𝟏𝟎𝟏, 𝟎𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = (𝟏∗ 𝟎𝟏∗ 𝟎𝟏∗ )∗
26. String should have odd length
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟎𝟏𝟎, 𝟏𝟏𝟎, 𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎…. 𝑹. 𝑬. = 𝟎|𝟏 ( 𝟎 𝟏 (𝟎|𝟏))∗
27. String should have even length
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎, 𝟎𝟏𝟎𝟏, 𝟎𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = ( 𝟎 𝟏 (𝟎|𝟏))∗
28. String start with 0 and has odd length
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟎𝟏𝟎, 𝟎𝟏𝟎, 𝟎𝟎𝟎, 𝟎𝟎𝟎𝟏𝟎…. 𝑹. 𝑬. = 𝟎 ( 𝟎 𝟏 (𝟎|𝟏))∗
30. String start with 1 and has even length
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏𝟎, 𝟏𝟏𝟎𝟎, 𝟏𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = 𝟏(𝟎|𝟏)( 𝟎 𝟏 (𝟎|𝟏))∗
31. All string begins or ends with 00 or 11
∗
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟏𝟎𝟏, 𝟏𝟎𝟏𝟎𝟎, 𝟏𝟏𝟎, 𝟎𝟏𝟎𝟏𝟏 … 𝑹. 𝑬. = (𝟎𝟎|𝟏𝟏)(𝟎 | 𝟏) ∗ | 𝟎 𝟏 (𝟎𝟎|𝟏𝟏)
Regular expression examples
31.Language of all string containing both 11 and 00 as substring
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟏𝟏, 𝟏𝟏𝟎𝟎, 𝟏𝟎𝟎𝟏𝟏𝟎, 𝟎𝟏𝟎𝟎𝟏𝟏 … 𝑹. 𝑬. = ((𝟎|𝟏)∗ 𝟎𝟎(𝟎|𝟏)∗ 𝟏𝟏(𝟎|𝟏)∗ ) | ((𝟎|𝟏)∗ 𝟏𝟏(𝟎|𝟏)∗ 𝟎𝟎(𝟎|𝟏)∗ )
33.Language of C identifier
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒓𝒆𝒂, 𝒊, 𝒓𝒆𝒅𝒊𝒐𝒖𝒔, 𝒈𝒓𝒂𝒅𝒆𝟏 … . 𝑹. 𝑬. = (_ + 𝑳)(_ + 𝑳 + 𝑫)∗
𝒘𝒉𝒆𝒓𝒆 𝑳 𝒊𝒔 𝑳𝒆𝒕𝒕𝒆𝒓 & 𝐃 𝐢𝐬 𝐝𝐢𝐠𝐢𝐭
Regular definition
• A regular definition gives names to certain regular expressions and uses
those names in other regular expressions.
• Regular definition is a sequence of definitions of the form:
𝑑1 → 𝑟1
𝑑2 → 𝑟2
……
𝑑𝑛 → 𝑟𝑛
Where 𝑑𝑖 is a distinct name & 𝑟𝑖 is a regular expression.
▪ Example: Regular definition for identifier
letter → A|B|C|………..|Z|a|b|………..|z
digit → 0|1|…….|9|
id→ letter (letter | digit)*
Regular definition example
• Example: Unsigned Pascal numbers
3
5280
39.37
6.336E4
1.894E-4
2.56E+7
Regular Definition
digit → 0|1|…..|9
digits → digit digit*
optional_fraction → .digits | 𝜖
optional_exponent → (E(+|-|𝜖)digits)|𝜖
num → digits optional_fraction optional_exponent
Practice exercise
Write regular definitions for the following languages:
• All strings of lowercase letters that contain the five vowels in order.
• All strings of a’s and b’s that do not contain the substring abb.
• All strings of a’s and b’s that do not contain the subsequence abb.
Solutions
Transition Diagram
Transition Diagram
• A stylized flowchart is called transition diagram.
is a state
is a transition
is a start state
is a final state
Transition Diagram : Relational operator
< =
0 1 2 return (relop,LE)
>
3 return (relop,NE)
=
other
5
4 return (relop,LT)
return (relop,EQ)
>
=
6 7 return (relop,GE)
other
8 return (relop,GT)
Transition diagram : Unsigned number
E digit
3 other other
5280
9 10
39.37
1.894 E - 4 A transition diagram for whitespace
2.56 E + 7
45 E + 6
96 E 2
Hard coding and automatic generation lexical analyzers
• Hard coding lexical analyzer
• Lexical analysis is about identifying the pattern from the input.
• To recognize the pattern, transition diagram is constructed.
• Example: to represent identifier in ‘C’, the first character must be letter and other
characters are either letter or digits.
• To recognize this pattern, hard coding lexical analyzer will work with a transition
diagram.
• The automatic generation lexical analyzer takes special notation as input.
• Example: lex compiler tool will take regular expression as input and finds out the
pattern matching to that regular expression.
Letter or digit
start
start 𝜖 𝑖 N(s) N(t) 𝑓
𝑖 𝑓
Ex: ab
2. For 𝑎 in 𝛴, construct the NFA
start a a b
𝑖 𝑓 1 2 3
Regular expression to NFA using Thompson's rule
4. For regular expression 𝑠|𝑡 5. For regular expression 𝑠*
𝜖
N(s) 𝜖
𝜖
start 𝜖 𝜖
start 𝑖 N(s) 𝑓
𝑖 𝑓
𝜖 N(t) 𝜖 𝜖
Ex: a*
𝜖
Ex: (a|b) a
2 3
𝜖 𝜖 𝜖 𝑎 𝜖
1 2 3 4
1 6
𝜖 𝜖 𝜖
4 5
b
Regular expression to NFA using Thompson's rule
• a*b
𝜖 𝑎 𝜖 𝑏
1 2 3 4 5
𝜖
• b*ab
𝜖 𝑏 𝜖 𝑎 𝑏
1 2 3 4 5 6
𝜖
Exercise
Convert following regular expression to NFA:
1. abba
2. bb(a)*
3. (a|b)*
4. a* | b*
5. a(a)*ab
6. aa*+ bb*
7. (a+b)*abb
8. 10(0+1)*1
9. (a+b)*a(a+b)
10. (0+1)*010(0+1)*
11. (010+00)*(10)*
12. 100(1)*00(0+1)*
Conversion from NFA to
DFA using subset
construction method
Subset construction algorithm
Input: An NFA 𝑁.
Output: A DFA D accepting the same language.
Method: Algorithm construct a transition table 𝐷𝑡𝑟𝑎𝑛 for D. We use the
following operation:
OPERATION DESCRIPTION
− 𝑐𝑙𝑜𝑠𝑢𝑟𝑒(𝑠) Set of NFA states reachable from NFA state 𝑠
on – transition alone.
𝑴𝒐𝒗𝒆 (𝑇, 𝑎) Set of NFA states to which there is a
transition on input symbol 𝑎 from some NFA
state 𝑠 in 𝑇.
Conversion from NFA to DFA
(a|b)*abb 𝜖
a
2 3
𝜖 𝜖
𝜖 𝜖 a b b
0 1 6 7 8 9 10
𝜖 𝜖
4 5
b
𝜖
Conversion from NFA to DFA
𝜖
a
2 3
𝜖 𝜖
𝜖 𝜖 a b b
0 1 6 7 8 9 10
𝜖 𝜖
4 5
b
𝜖- Closure(0)= {0, 1, 7, 2, 4}
= {0,1,2,4,7} ---- A
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8}
𝜖 𝜖
4 5
b
𝜖
A= {0, 1, 2, 4, 7}
Move(A,a) = {3,8}
𝜖- Closure(Move(A,a)) = {3, 6, 7, 1, 2, 4, 8}
= {1,2,3,4,6,7,8} ---- B
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8}
C = {1,2,4,5,6,7}
𝜖 𝜖
4 5
b
𝜖
A= {0, 1, 2, 4, 7}
Move(A,b) = {5}
𝜖- Closure(Move(A,b)) = {5, 6, 7, 1, 2, 4}
= {1,2,4,5,6,7} ---- C
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B
C = {1,2,4,5,6,7}
𝜖 𝜖
4 5
b
𝜖
B = {1, 2, 3, 4, 6, 7, 8}
Move(B,a) = {3,8}
𝜖- Closure(Move(B,a)) = {3, 6, 7, 1, 2, 4, 8}
= {1,2,3,4,6,7,8} ---- B
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7}
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9}
b
B= {1, 2, 3, 4, 6, 7, 8}
Move(B,b) = {5,9}
𝜖- Closure(Move(B,b)) = {5, 6, 7, 1, 2, 4, 9}
= {1,2,4,5,6,7,9} ---- D
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7} B
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9}
b
C= {1, 2, 4, 5, 6 ,7}
Move(C,a) = {3,8}
𝜖- Closure(Move(C,a)) = {3, 6, 7, 1, 2, 4, 8}
= {1,2,3,4,6,7,8} ---- B
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7} B C
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9}
b
𝜖
C= {1, 2, 4, 5, 6, 7}
Move(C,b) = {5}
𝜖- Closure(Move(C,b))= {5, 6, 7, 1, 2, 4}
= {1,2,4,5,6,7} ---- C
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7} B C
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9} B
b
D= {1, 2, 4, 5, 6, 7, 9}
Move(D,a) = {3,8}
𝜖- Closure(Move(D,a)) = {3, 6, 7, 1, 2, 4, 8}
= {1,2,3,4,6,7,8} ---- B
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7} B C
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9} B E
b
E = {1,2,4,5,6,7,10}
𝜖
D= {1, 2, 4, 5, 6, 7, 9}
Move(D,b)= {5,10}
𝜖- Closure(Move(D,b)) = {5, 6, 7, 1, 2, 4, 10}
= {1,2,4,5,6,7,10} ---- E
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7} B C
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9} B E
b
E = {1,2,4,5,6,7,10} B
𝜖
E= {1, 2, 4, 5, 6, 7, 10}
Move(E,a) = {3,8}
𝜖- Closure(Move(E,a)) = {3, 6, 7, 1, 2, 4, 8}
= {1,2,3,4,6,7,8} ---- B
Conversion from NFA to DFA
𝜖
a
2 3 States a b
𝜖 𝜖
A = {0,1,2,4,7} B C
𝜖 𝜖 a b b
0 1 6 7 8 9 10 B = {1,2,3,4,6,7,8} B D
C = {1,2,4,5,6,7} B C
𝜖 𝜖
4 5 D = {1,2,4,5,6,7,9} B E
b
E = {1,2,4,5,6,7,10} B C
𝜖
E= {1, 2, 4, 5, 6, 7, 10}
Move(E,b)= {5}
𝜖- Closure(Move(E,b))= {5,6,7,1,2,4}
= {1,2,4,5,6,7} ---- C
Conversion from NFA to DFA
a
b
States a b B D
a
A = {0,1,2,4,7} B C a
B = {1,2,3,4,6,7,8} B D
A a a b
C = {1,2,4,5,6,7} B C
D = {1,2,4,5,6,7,9} B E b
C E
E = {1,2,4,5,6,7,10} B C b
Transition Table
b
Note:
• Accepting state in NFA is 10 DFA
• 10 is element of E
• So, E is acceptance state in DFA
Exercise
• Convert following regular expression to DFA using subset construction
method:
1. (a+b)*a(a+b)
2. (a+b)*ab*a
DFA optimization
1. Construct an initial partition Π of the set of states with two groups: the
accepting states 𝐹 and the non-accepting states 𝑆 − 𝐹.
2. Apply the repartition procedure to Π to construct a new partition Π𝑛𝑒𝑤.
3. If Π 𝑛𝑒𝑤 = Π, let Π𝑓𝑖𝑛𝑎𝑙 = Π and continue with step (4). Otherwise,
repeat step (2) with Π = Π𝑛𝑒𝑤.
for each group 𝐺 of Π do begin
partition 𝐺 into subgroups such that two states 𝑠 and 𝑡
of 𝐺 are in the same subgroup if and only if for all
input symbols 𝑎, states 𝑠 and 𝑡 have transitions on 𝑎
to states in the same group of Π.
replace 𝐺 in Π𝑛𝑒𝑤 by the set of all subgroups formed.
end
DFA optimization
4. Choose one state in each group of the partition Π𝑓𝑖𝑛𝑎𝑙 as the
representative for that group. The representatives will be the states of
𝑀′. Let s be a representative state, and suppose on input a there is a
transition of 𝑀 from 𝑠 to 𝑡. Let 𝑟 be the representative of 𝑡′s group. Then
𝑀′ has a transition from 𝑠 to 𝑟 on 𝑎. Let the start state of 𝑀′ be the
representative of the group containing start state 𝑠0 of 𝑀, and let the
accepting states of 𝑀′ be the representatives that are in 𝐹.
5. If 𝑀′ has a dead state 𝑑, then remove 𝑑 from 𝑀′. Also remove any state
not reachable from the start state.
DFA optimization
States a b
{𝐴, 𝐵, 𝐶, 𝐷, 𝐸}
A B C
B B D
Nonaccepting States Accepting States
C B C
{𝐴, 𝐵, 𝐶, 𝐷} {𝐸}
D B E
E B C
{𝐴, 𝐵, 𝐶} {𝐷}
States a b
{𝐴, 𝐶} {𝐵}
A B A
B B D
• firstpos(n)
• The set of positions that can match the first symbol of a string generated by the subtree at
node 𝑛.
• lastpos(n)
• The set of positions that can match the last symbol of a string generated be the subtree at
node 𝑛.
• followpos(i)
• The set of positions that can follow position 𝑖 in the tree.
Rules to compute nullable, firstpos, lastpos
Node n nullable(n) firstpos(n) lastpos(n)
A leaf labeled by true ∅ ∅
A leaf with
false {i} {i}
position 𝐢
𝑎 𝑏
𝟏 𝟐
Conversion from regular expression to DFA
Step 3: Calculate firstpos
Firstpos
{1,2,3} .
{1,2} | {1,2}
𝑖 = 𝑙𝑎𝑠𝑡𝑝𝑜𝑠(𝑐1) = {5}
𝑓𝑖𝑟𝑠𝑡𝑝𝑜𝑠 𝑐2 = 6
𝑎 𝑏 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 5 = 6
{1} {1} {2} {2}
𝟏 𝟐
Conversion from regular expression to DFA
Step 4: Calculate followpos Position followpos
5 6
{1,2,3} . {6}
4 5
{1,2,3} . {5}
{6} # {6}
{1,2,3} . {4} 𝟔
{5} 𝑏 {5}
{1,2,3} . {3} {4} 𝑏 {4} 𝟓 .
𝟒
{1,2} ∗ {1,2} {3} 𝑎 {3} {1,2,3} 𝒄 {4}
𝟏
{5} 𝒄𝟐 {5}
𝟑
{1,2} | {1,2}
𝑖 = 𝑙𝑎𝑠𝑡𝑝𝑜𝑠(𝑐1) = {4}
𝑓𝑖𝑟𝑠𝑡𝑝𝑜𝑠 𝑐2 = 5
𝑎 𝑏 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 4 = 5
{1} {1} {2} {2}
𝟏 𝟐
Conversion from regular expression to DFA
Step 4: Calculate followpos Position followpos
5 6
Firstpos {1,2,3} . {6}
4 5
Lastpos
{1,2,3} . {5} 3 4
{6} # {6}
{1,2,3} . {4} 𝟔
{5} 𝑏 {5}
{1,2,3} . {3} {4} 𝑏 {4} 𝟓 .
𝟒
{1,2} ∗ {1,2} {3} 𝑎 {3} {1,2,3} 𝒄 {3}
𝟏
{4} 𝒄𝟐 {4}
𝟑
{1,2} | {1,2}
𝑖 = 𝑙𝑎𝑠𝑡𝑝𝑜𝑠(𝑐1) = {3}
𝑓𝑖𝑟𝑠𝑡𝑝𝑜𝑠 𝑐2 = 4
𝑎 𝑏 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 3 = 4
{1} {1} {2} {2}
𝟏 𝟐
Conversion from regular expression to DFA
Step 4: Calculate followpos Position followpos
5 6
Firstpos {1,2,3} . {6}
4 5
Lastpos
{1,2,3} . {5} 3 4
{6} # {6}
2 3
{1,2,3} . {4} 𝟔
{5} 𝑏 {5} 1 3
{1,2,3} . {3} {4} 𝑏 {4} 𝟓 .
𝟒
{1,2} ∗ {1,2} {3} 𝑎 {3} {1,2} 𝒄 {1,2} {3} 𝒄 {3}
𝟏 𝟐
𝟑
{1,2} | {1,2}
𝑖 = 𝑙𝑎𝑠𝑡𝑝𝑜𝑠(𝑐1) = {1,2}
𝑓𝑖𝑟𝑠𝑡𝑝𝑜𝑠 𝑐2 = 3
𝑎 𝑏 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 1 = 3
{1} {1} {2} {2}
𝟏 𝟐 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 2 = 3
Conversion from regular expression to DFA
Step 4: Calculate followpos Position followpos
5 6
Firstpos {1,2,3} . {6}
4 5
Lastpos
{1,2,3} . {5} 3 4
{6} # {6}
2 1,2, 3
{1,2,3} . {4} 𝟔
{5} 𝑏 {5} 1 1,2, 3
{1,2,3} . {3} {4} 𝑏 {4} 𝟓
𝟒 {1,2} * {1,2}
{1,2} ∗ {1,2} {3} 𝑎 {3} 𝒏
𝟑
{1,2} | {1,2}
𝑖 = 𝑙𝑎𝑠𝑡𝑝𝑜𝑠(𝑛) = {1,2}
𝑓𝑖𝑟𝑠𝑡𝑝𝑜𝑠 𝑛 = 1,2
𝑎 𝑏 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 1 = 1,2
{1} {1} {2} {2}
𝟏 𝟐 𝑓𝑜𝑙𝑙𝑜𝑤𝑝𝑜𝑠 2 = 1,2
Conversion from regular expression to DFA
Initial state = 𝑓𝑖𝑟𝑠𝑡𝑝𝑜𝑠 of root = {1,2,3} ----- A
Position followpos
State A 5 6
δ( (1,2,3),a) = followpos(1) U followpos(3) 4 5
3 4
=(1,2,3) U (4) = {1,2,3,4} ----- B
2 1,2,3
1 1,2,3
δ( (1,2,3),b) = followpos(2)
=(1,2,3) ----- A States a b
A={1,2,3} B A
B={1,2,3,4}
Conversion from regular expression to DFA
State B
Position followpos
δ( (1,2,3,4),a) = followpos(1) U followpos(3)
5 6
=(1,2,3) U (4) = {1,2,3,4} ----- B 4 5
3 4
δ( (1,2,3,4),b) = followpos(2) U followpos(4) 2 1,2,3
State C
δ( (1,2,3,5),a) = followpos(1) U followpos(3) States a b
A={1,2,3} B A
=(1,2,3) U (4) = {1,2,3,4} ----- B
B={1,2,3,4} B C
C={1,2,3,5} B D
δ( (1,2,3,5),b) = followpos(2) U followpos(5) D={1,2,3,6}
DFA
Example 2
Convert the following RE to
DFA (Direct Method):
ba(a+b)*ab
Practice Problems
Convert the following regular expressions to deterministic finite
automata:
1. (a|b)*
2. (a*|b*)*
3. ((ε|a)|b*)*
4. (a|b)*abb(a|b)*
Solutions to practice problems
(a*|b*)*
(a|b)*