Professional Documents
Culture Documents
FLAT Unit 1 August 2023
FLAT Unit 1 August 2023
Automata Theory
Subject Code
• Finite Automata (FA) 3151605
• Regular Expression (RE)
• Languages
• Grammars
1
Formal Language and Automata Theory
(3151605)
Rationale:
The course should in addition clarify the practical view towards the
applications of these ideas in engineering.
2
ASSESSMENT GUIDELINES
Theory Marks (100)
1) End Semester Examination: 70 Marks (Conducted by The University)
2) Progressive Assessment: 30 Marks
Mid Semester Examination: 20 Marks
Class Tests: 05 Marks
Attendance: 05 Marks
3
WHAT IS FA?
Finite Automata(FA) is the simplest machine to recognize patterns.
A+C+D*T–F*X/V–Y
A+C+D*T–F*X/T–Y
4
Example: Finite Automata
δ Input
State
• is defined as
1 1 0
𝑞0 0
𝑞1 0
𝑞2
1
5
Finite Automata
• A finite automaton, or finite state machine is a 5-tuple where
• is finite set of states;
• is finite alphabet of input symbols;
• (initial state);
• (the set of accepting states);
• is a function from (the transition function).
• For any element of and any symbol , we interpret as the state to
which the FA moves, if it is in state and receives the input .
6
Applications of FA
• Lexical analysis phase of a compiler.
• Design of digital circuit.
• String matching.
• Communication Protocol for information exchange.
7
WHAT IS DFA and NFA?
Deterministic Finite Automata (DFA)
Verify for below strings:
1010001010
11010100
11211011
01010011111
010101010111000
DFA with Σ = {0, 1} accepts all strings ending with 0.
11
00
8
FA Examples
• The strings ending with 10.
0 1
𝐴 1
𝐵 𝐶
1
9
Nondeterministic Finite Automata (NFA)
• A nondeterministic finite automaton is a 5-tuple where and are
nonempty finite sets, , and
is the set of states, is the alphabet, is the initial state and is the
set of accepting states.
10
Exercise:
Design a DFA with ∑ = {0, 1} accepts the only input 101
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and
ends with 0.
11
Regular Expression
12
Regular expression
• A regular expression is a sequence of characters that define
a pattern.
• Notational shorthand's
1. One or more occurrences:
2. Zero or more occurrences:
3. Alphabets:
13
Regular expression
L = Zero or More Occurrences of a = a*
*
𝜖
a
aa
aaa Infinite …..
aaaa
aaaaa…..
14
Regular expression
+
L = One or More Occurrences of a = a+
a
aa
aaa Infinite …..
aaaa
aaaaa…..
15
Regular expression examples
1. 0 or 1
𝑺𝒕𝒓𝒊𝒏𝒈𝒔 :𝟎 ,𝟏𝑹 . 𝑬 .=𝟎∨𝟏
2. 0 or 11 or 111
𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝟎 ,𝟏𝟏,𝟏𝟏𝟏 𝑹. 𝑬 .=𝟎|𝟏𝟏| 𝟏𝟏𝟏
𝑹 . 𝑬 .=𝒂 ∗
String having zero or more a.
3. 𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝜖,𝒂,𝒂𝒂,𝒂𝒂𝒂,𝒂𝒂𝒂𝒂…..
4. 𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝒂,𝒂𝒂,𝒂𝒂𝒂,
String having one or 𝑹a.. 𝑬 .=𝒂 +¿
𝒂𝒂𝒂𝒂…..
more
12. 𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝟎𝟎,𝟏𝟎𝟏,𝒂𝒃𝒂,𝒃𝒂𝒂𝒃…
String starts and ends with same character
17
Regular expression examples
13. All string of a and b starting with a
… 𝑹. 𝑬 .=𝒂(𝒂∨𝒃)∗
14. String of 0 and 1 ends with 00
… 𝑹. 𝑬 .=(𝟎∨𝟏)∗𝟎𝟎
15. String ends with abb
… 𝑹. 𝑬 .=(𝒂∨𝒃)∗𝒂𝒃𝒃
16. String starts with 1 and ends with 0
… 𝑹. 𝑬 .=𝟏(𝟎∨𝟏)∗𝟎
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
… 𝑹. 𝑬 .=𝒂∗𝒃 𝒂∗𝒃𝒂∗
18
Regular expression examples
19. The language with such that 3rd character from right end of the string is
always
… 𝑹.𝑬.=(𝒂∨𝒃)∗𝒂(𝒂∨𝒃)(𝒂∨𝒃)
20. Any no. of followed by any no. of followed by any no. of
… 𝑹. 𝑬 .=𝒂∗𝒃∗𝒄 ∗
21. String should contain at least three
∗ ∗ ∗ ∗
…. 𝑹.𝑬.=(𝟎∨𝟏) 𝟏(𝟎∨𝟏) 𝟏(𝟎∨𝟏) 𝟏(𝟎∨𝟏)
22. String should contain exactly two
∗ ∗ ∗
….
𝑹 . 𝑬 .=𝟎
23. Length of string should be at least 1 and at most 3
𝟏 𝟎 𝟏 𝟎
…. 𝑹.𝑬.=( 𝟎∨𝟏)|( 𝟎∨𝟏)( 𝟎∨𝟏)|( 𝟎∨𝟏)( 𝟎∨𝟏 )( 𝟎∨𝟏
24. No. of zero should be multiple of 3
∗ ∗ ∗
…. 𝑹. 𝑬 .=(𝟏 𝟎𝟏 𝟎𝟏 𝟎
19
Regular expression - Exercise
1. The language with where should be multiple of 3
𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝒂𝒂𝒂,𝒃𝒂𝒂𝒂,𝒃𝒂𝒄𝒂𝒃𝒂,𝒂𝒂𝒂𝒂𝒂𝒂.. ∗ ∗ ∗
𝑹.𝑬.=( ( 𝒃∨𝒄 ) 𝒂 ( 𝒃∨𝒄 ) 𝒂 ( 𝒃∨𝒄 ) 𝒂 ( 𝒃∨𝒄 ) )
∗∗
2. Even no. of 0
∗ ∗ ∗ ∗
…. 𝑹 . 𝑬 .=(𝟏 𝟎 𝟏 𝟎 𝟏 )
3. String should have odd length
∗
…. 𝑹. 𝑬 .=( 𝟎∨𝟏 ) (( 𝟎|𝟏 ) (𝟎∨𝟏))
4. String should have even length
∗
…. 𝑹 . 𝑬 .=( ( 𝟎|𝟏 ) ( 𝟎∨𝟏))
5. String start with 0 and has odd length
∗
…. 𝑹. 𝑬 .=( 𝟎 ) ( ( 𝟎|𝟏 ) (𝟎∨𝟏))
6. String start with 1 and has even length
∗
…. 𝑹. 𝑬 .=𝟏(𝟎∨𝟏)(( 𝟎|𝟏 ) (𝟎∨𝟏))
20
Regular expression - Exercise
7. All string begins or ends with 00 or 11
𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝟎𝟎𝟏𝟎𝟏,𝟏𝟎𝟏𝟎𝟎,𝟏𝟏𝟎𝟎,𝟎𝟏𝟎𝟏𝟏… 𝑹.𝑬.=(𝟎𝟎∨𝟏𝟏)(𝟎∨𝟏)∗∨( 𝟎|𝟏 ) ∗(𝟎𝟎∨𝟏𝟏)
8. Language of all string containing both 11 and 00 as substring
𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝟎𝟎𝟏𝟏,𝟏𝟏𝟎𝟎,𝟏𝟎𝟎𝟏𝟏𝟎,𝟎𝟏𝟎𝟎𝟏𝟏…
9. String ending with 1 and not contain 00
𝑹 . 𝑬 .=( 𝟏|𝟎𝟏 ) +¿
𝑺𝒕𝒓𝒊𝒏𝒈𝒔:𝟎𝟏𝟏,𝟏𝟏𝟎𝟏,𝟏𝟎𝟏𝟏….
10. Language of C identifier
21
Precedence and associativity of operators
Operator Precedence Associative
Kleene * 1 left
Concatenation 2 left
Union | 3 left
22
Exercise:
Draw DFA over {a, b} for all strings with zero or more a followed
by exact one b a
RE: a*b
𝑞0 b
𝑞1
Draw DFA over {a, b} for all strings with one or more a followed
by exact one b a
RE: a+b
𝑞0 a
𝑞1 b
𝑞2
23
FA Examples
• (a+b)*baaa OR (a|b)*baaa b
a B a
b
b
A C
b b
a a
a
E D
24
Example of NFA for
δ Input
State
• is defined as
𝑞1
1 1
𝑞0 0
𝑞4
1 0
𝑞2 1
𝑞3
25
Languages
26
Language
• A set of strings all of which are chosen from some , where is a
particular alphabet, is called a language. If is an alphabet, and ,
then is said to be language over alphabet .
• Language comprises of:
• Set of characters –
• Set of strings (words) defined from set of character -
• Language L is defined from , and because contains many
string which may not satisfy the rules of language.
• Example:
• = {a, b}
• = {^, a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, …}
27
Operations over Language
• Operations over the language are:
1. Concatenation
2. | (Union)
3. * (Kleene closure)
4. +
If then concatenation is defined as
Example:
= {hope, fear} and = {less, fully}
hopeless
Example:
= {hope, fear} and = {less, fully}
29
Operations over Language
• Operations over the language are:
1. Concatenation
2. | (Union)
3. * (Kleene closure)
4. +
• If is a set of words then by we mean the set of all finite strings
formed by concatenating words from S, where any word may be
used as often, we like, and where the null string is also included.
Example: = {ab}
Example: = {ab}
32
Chomsky hierarchy (Classification of grammar)
Grammar
Unrestricted Restricted
grammar (type 0) grammar
33
Definition: Grammar
• A grammar is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set of productions of the form
34
Type 0 grammar (Phrase Structure Grammar)
• Their productions are of the form:
35
Type 1 grammar (Context Sensitive Grammar)
• Their productions are of the form:
36
Type 2 grammar (Context Free Grammar)
• Their productions are of the form:
37
Type 3 grammar (Linear or Regular grammar)
• Their productions are of the form:
or
• Where are nonterminals and is terminal.
• Example: X → a | aY
Y→b
38
Hierarchy of grammar
Type 3
(Regular)
39
Context free grammar
40
Context Free Grammar
• A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set of productions of the form where and .
• Application of CFG:
1. CFG are extensively used to specify the syntax of
programming language.
2. CFG is used to develop a parser.
41
Context Free Language
• Let be a CFG. The language generated by is
42
CFG Examples
If in any CFG the productions are −
S → AB
A → aaA | ε
B → Bb| ε
the partial derivation tree can be the following −
Input string: a + a * a
Solution: 1 Solution: 2
X → X+X X → X*X
X → a+X X → X*a
X → a + X*X X → X+X*a
X → a+a*X X → X+a*a
X → a+a*a X → a+a*a
44
CFG Examples
Let any set of production rules in a CFG be Right most derivation
X → X+X | X*X | a
over an alphabet {a}.
Input string: a + a * a
Solution: 1 Solution: 2
X → X+X X → X*X
X → a+X X → X*a
X → a + X*X X → X+X*a
X → a+a*X X → X+a*a
X → a+a*a X → a+a*a
45
CFG Examples
• Write CFG for either a or b
S a | b
• Write CFG for a+
S aS | a
• Write CFG for a*
S aS | Ɛ
• Write CFG for (ab)*
S abS | Ɛ
• Write CFG for any string of a and b
S aS | bS | a | b
46
CFG Exercise
Arithmetic expressions
The terminals are {+,-,*,a}. The symbol a represents a token that we identify by
other means. It could be a number, variable, etc.
CFG E⇒E+T
E→E+T|E-T|T E => T + T
T→T*F|F E⇒F+T
F→a E⇒x+T
Where a = {a,b,c,d,….z} E⇒x+T*F
E⇒x+F*F
Input string: x + y * z E⇒x+y*F
E⇒x+y*z
47
CFG Exercise
Balanced Parentheses
S⇒(S)
CFG S⇒(SS)
S → SS | (S) | ε S⇒((S)S)
S⇒((S)(S))
Input string: ( ( ) ( ) ) S⇒(()(S))
S⇒(()())
48
CFG Exercise
Program structure
CFG
Prog → { Stmts } Prog ⇒ { Stmts }
Stmts →Stmts ; Stmt ; Prog ⇒ { Stmts ; Stmt ;}
Stmts →Stmt; Prog ⇒ { Stmt ; Stmt ;}
Stmt →id = Expr Prog ⇒ { id = Expr ; Stmt ;}
Expr →id Prog ⇒ { id = id ; Stmt ;}
Expr →Expr + id Prog ⇒ { id = id ; id = Expr ;}
Prog ⇒ { id = id ; id = Expr + id ;}
Input string: Prog ⇒ { id = id ; id = id + id ;}
{ Prog ⇒ { id = id ; id = id + id ;}
id = id;
id = id + id;
}
49
CFG Exercise
Consider the following grammar
CFG
S → aB | bA
A → aS | bAA | a
B → bS | aBB | b
Input string:
aaabbabbba
Que:
Derive the string using leftmost derivation and rightmost derivation.
Draw parse tree for both derivations.
50
S → aB
CFG Exercise S → aaBB (Using B → aBB)
S → aaaBBB (Using B → aBB)
Consider the following grammar S → aaabBB (Using B → b)
CFG S → aaabbB (Using B → b)
S → aB | bA S → aaabbaBB (Using B → aBB)
A → aS | bAA | a S → aaabbabB (Using B → b)
B → bS | aBB | b S → aaabbabbS (Using B → bS)
S → aaabbabbbA (Using S → bA)
Input string: S → aaabbabbba (Using A → a)
aaabbabbba
51
Recursive Definitions
52
Recursive Definitions
1. Recursive Definition of {a,b}* CFG: S aS | bS | ^
˄∈L.
For any S∈L, aS∈L.
For any S∈L, bS∈L.
No other strings are in L.
2. Recursive Definition of Palindrome CFG: S aSa | bSb | a | b | ˄
˄, a, b ∈ L
For any S ∈ L , aSa ∈ L and bSb ∈ L
No other string are in L
3. Recursive Definition of the language {anbn | n≥0} CFG: S aSb | ˄
˄∈ L
For every S ∈ L, aSb ∈L
No other strings are in L
53
CFG Examples
• Write CFG for ab*
SaX
X˄| bX
• Write CFG for a*b*
SXY
XaX|˄
YbY|˄
• Write CFG for (a|b)*
SaS | bS | ^
• Write CFG for a(a|b)*
SaX
XaX | bX | ^
54
Extended Transition Function for FA
• Let be a Finite Automata. We define the function
as follows:
1. For any
2. For any and ,
55
Example
Consider FA
a
𝑞 𝑞1 b
𝑞2 c
𝑞3
Calculate
56
Exercise :
Consider following FA
0 1
𝐴 1
𝐵 𝐶
1
Calculate
57
Acceptance by FA
• Let be an FA. A string is accepted by if . If string is not accepted,
we can say it is rejected by . The language accepted by , or the
language recognized by , is the set
58
Union, Intersection & Complement of Languages
• Suppose and accepts languages and , respectively. Let be an FA
defined by , where
59
Example
Draw Finite Automata for following languages:
1. L1={x/x 00 is not substring of x, x ∈ {0,1}*}
2. L2={x/x ends with 01, x ∈ {0,1}*}
Draw FA for , and
1 0,1 1 0
0 1
𝐴 1
𝐵 0
𝐶 𝑃 0
𝑄 0
𝑅
1
𝑴𝟏 𝑴𝟐
60
Computation for , and
• Here and 1
• So,
𝐴𝑃 𝐵𝑃 𝐶𝑃
• Computing Transition Function 0
𝛿 ( ( 𝐴, 𝑃 ) , 0 ) =(𝛿 ( 𝐴, 0 ) , 𝛿 ( 𝑃 , 0 ) ) 𝐴𝑄 𝐵𝑄 𝐶𝑄
¿ 𝐵𝑄
𝛿 ( ( 𝐴, 𝑃 ) ,1 ) =(𝛿 ( 𝐴 ,1 ) , 𝛿 ( 𝑃 ,1 ))
¿ 𝐴𝑃 𝐴𝑅 𝐵𝑅 𝐶𝑅
BQ
61
Computation for , and
𝛿 ( ( 𝐵 ,𝑄 ) , 0 ) =(𝛿 ( 𝐵, 0 ) , 𝛿 (𝑄 , 0 ) ) 1
¿ 𝐶𝑄
𝛿 ( ( 𝐵 ,𝑄 ) , 1 ) =(𝛿 ( 𝐵 ,1 ) , 𝛿 ( 𝑄 , 1 ) )
¿ 𝐴𝑅
𝛿 ( ( 𝐶 ,𝑄 ) ,0 ) =(𝛿 ( 𝐶 ,0 ) , 𝛿 (𝑄 , 0 ) )
𝐴𝑃 𝐵𝑃 𝐶𝑃
0
¿ 𝐶𝑄 0
𝛿 ( ( 𝐶 ,𝑄 ) ,1 ) =(𝛿 ( 𝐶 ,1 ) , 𝛿 ( 𝑄 , 1 ) )
¿ 𝐶𝑅 1 𝐴𝑄 𝐵𝑄 0
𝐶𝑄
𝛿 ( ( 𝐴, 𝑅 ) , 0 ) =(𝛿 ( 𝐴 , 0 ) , 𝛿 ( 𝑅 , 0 ) ) 0
1
¿ 𝐵𝑄 1
𝛿 ( ( 𝐴, 𝑅 ) , 1 ) =(𝛿 ( 𝐴, 1 ) , 𝛿 ( 𝑅 ,1 ) )
¿ 𝐴𝑃
𝐴𝑅 𝐵𝑅 𝐶𝑅
BQ CQ AR CR
62
Computation for , and
𝛿 ( ( 𝐶 , 𝑅 ) ,0 ) =(𝛿 ( 𝐶 ,0 ) , 𝛿 ( 𝑅 , 0 ) ) 1 1
¿ 𝐶𝑄
𝛿 ( ( 𝐶 , 𝑅 ) ,1 ) =(𝛿 ( 𝐶 ,1 ) , 𝛿 ( 𝑅 , 1 ) ) 𝐴𝑃 𝐵𝑃 𝐶𝑃
¿ 𝐶𝑃 0
0 0
𝛿 ( ( 𝐶 , 𝑃 ) , 0 ) =(𝛿 ( 𝐶 , 0 ) , 𝛿 ( 𝑃 , 0 ) )
¿ 𝐶𝑄 1 𝐴𝑄 𝐵𝑄 0
𝐶𝑄 1
0
𝛿 ( ( 𝐶 , 𝑃 ) , 1 )=(𝛿 ( 𝐶 , 1 ) , 𝛿 ( 𝑃 ,1 ) ) 1
0 1
¿ 𝐶𝑃 𝐴𝑅 𝐵𝑅 𝐶𝑅
BQ CQ AR CR CP
63
Removing Unconnected States
1 1
𝐴𝑃 𝐵𝑃 𝐶𝑃
0
0 0
1 𝐴𝑄 𝐵𝑄 0
𝐶𝑄 1
0
0 1
1
𝐴𝑅 𝐵𝑅 𝐶𝑅
64
𝐿1 ∪ 𝐿2
1 1
𝐴1={ 𝐴, 𝐵 }
𝐴2={ 𝑅 }
As per theorem stated
𝐴𝑃 𝐶𝑃
0
earlier, the states which 0 0
consists A or B or R will be
Accepting states in the 1 𝐵𝑄 0
𝐶𝑄 1
resultant FA. 0
0 1
1
𝐴𝑅 𝐶𝑅
Accepting States
65
𝐿1 ∩ 𝐿2
𝐴1={ 𝐴, 𝐵 } 1 1
𝐴2={ 𝑅 }
Therefore, as per theorem 𝐴𝑃 𝐶𝑃
0
stated earlier, the states 0 0
which consists (A and R)
and (B and R) will be 1 𝐵𝑄 0
𝐶𝑄 1
𝐴𝑅 𝐶𝑅
Accepting States
66
𝐿1 − 𝐿2
𝐴1={ 𝐴, 𝐵 } 1 1
𝐴2={ 𝑅 }
Therefore, as per theorem 𝐴𝑃 𝐶𝑃
0
stated earlier, the states 0 0
which consists A or B but
should not contain R with 1
𝐵𝑄 0
𝐶𝑄 1
𝐴𝑅 𝐶𝑅
Accepting States
67
Exercise
Draw Finite Automata for following languages:
1. L1={x/x 11 is not substring of x, x ∈ {0,1}*}
2. L2={x/x ends with 10, x ∈ {0,1}*}
Draw FA for , and .
0 0,1 0 1
1 0
𝐴 0
𝐵 1
𝐶 𝑃 1
𝑄 1
𝑅
0
𝑴𝟏 𝑴𝟐
68
Thank You
69