Toa Lec 4
Toa Lec 4
AUTOMATION
L A G
Example of Symbols:
In programming, an alphabet could be made up of any characters, such as
Examples:
The set of all strings over Σ = {a, b} where the string contains exactly one
'a' (e.g., {"a", "ba", "ab"}).
Context-Free Languages (CFLs):
• Recognized by Pushdown Automata (PDAs).
• Described by context-free grammars (CFGs).
Context-Sensitive Languages:
• Recognized by Linear Bounded Automata (LBAs).
• Described by context-sensitive grammars.
Finite Automata
• In FA we can represent language in form of diagram or
(FA)
graphically.
A finite set of transitions that tell for each state and for each
letter of the input alphabet which state to go to next
Applications of Finite Automata
Application
Area Description
Language Processing Used in email filters and smart assistants like Alexa, Siri, and Google Assistant.
Computer Networks Underlies the design of network protocols such as HTTP and HTTPS.
Video Games Manages game mechanics and AI, as seen in games like Warcraft 3.
Biomedical Problem Solving Utilized in medical imaging technologies like X-ray diffraction and tomography.
CONTEXT FREE
GRAMMAR(CFG)
a grammar is a formal set of rules that define how strings in a language are generated.
Grammars are essential for describing the syntax of programming languages, natural
languages, and other formal systems.
A grammar is a 4-tuple G = (N, Σ, P, S), where:
N (Non-terminal symbols): A finite set of symbols, called non-terminals, that represent
intermediate constructs or variables used in generating the strings of the language.
Σ (Terminal symbols): A finite set of symbols, called terminals, that form the actual strings of
the language.
(N∩Σ=∅)
P (Production rules): A finite set of production rules, which describe how the non-terminal
symbols can be replaced by terminals or other non-terminals. Each production rule has the
𝐴→𝛼
form:
Where A is a non-terminal, and α is a string of terminals and/or non-terminals. The rule states
that the non-terminal A can be replaced by α.
S (Start symbol): A special non-terminal symbol, called the start symbol, from which string
generation begins.
Example of a Grammar:
Σ = {a, b} (terminals are 'a' and 'b’),
P = {S → aSb, S → ε} (the production rules),
S is the start symbol.
This grammar generates strings where the number of 'a's is equal to the
number of 'b's. For example:
S → ε (The empty string ε is a valid string in the language).
S → aSb → ab (One 'a' followed by one 'b’).
S → aSb → aaSbb → aabbb (Two 'a's followed by two 'b's).
RE to CFG Conversion (Examples)
1) RE= 2) RE= (a+b) (a+b) 4) RE= (a+b) (aa+bb)
a+b S AA (a+b)
S ABA
A a|b
S or a|b A. a|b
S a 3) RE= (a+b)a +
B. YY |
S b ab ZZ
S
or Y. a
X Y
S X X Z. b
JK
|Y
J. a|b
X. a K. a
Y. b Y
KB
CFG Derivation
• Derivation is generation of string from grammar.
X bbX|ɛ bb ε aY ε
X
Y aY|ɛ aY ε
bb ε
X
string generated by given grammar
will be: bbX
ε
aY
ε
bbbbbbaaa
ε ε
In general: b2n an ; n>=1
CFG Derivation (Example)
• Find out is “baab” valid string of given
CFG?
S A B A
S a|b B A
S a|b YY|ZZ A
S a|b aa|bb A
S a|b aa|bb a|b
: Transition/movement
5. F (final states).
FA Tuples
q0definition
(start state or initial state)
• It is represented by: q0 or
Cont
…
FA (Final state)
• In FA the minimum number of final state should be 1 and maximum
number of final states can be more then 1.
It is represented by: + OR
Cont
…
In Finite Automata (FA), Q is the set of all possible states that
the automaton can be in at any given time.
Formally:
Q={q0,q1,q2,…,qn} where:
• q0 is the start state (or initial state)
• F⊆Q is the set of final (accepting) states
• The rest are intermediate states the automaton may pass
through
: Transition/movement
• Transition or movement is represent with sign.
Types of FA
• The finite automata are called NFA when there exist many paths for
specific input from the current state to the next state.
• In NFA dead configuration is allowed.
• In other words, the exact state to which the machine moves cannot
be determined. Hence, it is called Non-deterministic Automation.
q1 q2
q0
q3
First
Choice
a a
All input is
consumed
q1 q2
“accept”
q0
q3
26
Second
Choice
a a
q1 q2
q0
q3
27
Second
a a Choice
Input cannot be
consumed
q1 q2
Automaton
q0 Halts
q3
“reject”
28
An NFA accepts a string:
if there is a computation of the
NFA that accepts the string
29
aa is accepted by the
NFA:
“accept
q1 ” q2 q1 q2
q0 q0
q3 q3 “reject
because ”
this this
computatio computation
is 6
ignored
2
n accepts
An NFA rejects a string:
if there is no computation of the
NFA that accepts the string.
OR
Rejection
example
q1 q2
q0
q3
32
First
Choice
“reject
”
q1 q2
q0
q3
33
Second
Choice
q1 q2
q0
q3
34
Second
Choice
q1 q2
q0
q3
35
Another Rejection
example
q1 q2
q0
q3
36
First
Choice
q1 q2
q0
q3
37
First
Choice
38
Second
Choice
q1 q2
q0
q3
39
Second
Choice
q0 q0
q3 “reject q3
”
ction
is rejected by the
NFA:
“reject
q1 ” q2 q1 q2
q0 q0
q3 q3 “reject
”
ction
Language accepted: L
{aa}
q1 q2
q0
q3
Lambda
Transitions
q0 q1 q2 q3
a a
q0 q1 q2 q3
a a
q0 q1 q2 q3
input tape head does not
move
a a
q0 q1 q2 q3
all input is consumed
a a
“accept
”
q0 q1 q2 q3
String aa is
Rejection
Example
a a a
q0 q1 q2 q3
a a a
q0 q1 q2 q3
(read head doesn’t
move)
a a a
q0 q1 q2 q3
Input cannot be consumed
a a a
Automaton halts
“reject
”
q0 q1 q2 q3
String is
rejected
Language accepted: L
{aa}
q0 q1 q2 q3
Another NFA
Example
a q1 b q2 q3
q0
a b
a q1 b q2 q3
q0
a b
a b q2 q3
q0 q1
a b
“accept
a b ”
q0 q1 q2 q3
Another
String
a b a b
a b
q0 q1 q2 q3
a b a b
a b
q0 q1 q2 q3
a b a b
a b
q0 q1 q2 q3
a b a b
a b
q0 q1 q2 q3
a b a b
a b
q0 q1 q2 q3
a b a b
a b
q0 q1 q2 q3
a b a b
“accept
a b ”
q0 q1 q2 q3
NFA
Examples
• Draw NFA for RE =
a
a
q0 q1
NFA Examples
q0
NFA Examples
Draw NFA for all strings that starts with ‘a’ and end with ‘bb’,
defined over alphabet ∑ = {a, b}.
• The string that will be generated according to given condition may be
of following type
S= abb
= a [^, a, b, ab, aaa,bbb…] bb
RE = a (a+b)* a, b
bb
a b b
q0 q1 q2 q3
NFA
Examples
Draw a NFA for all possible strings start and end with same letter
defined over alphabet ∑ = {a, b}.
• Given condition means if string start with ‘a’ then it also end with ‘a’ or if
it start with ‘b’ then it also end with ‘b’
a, b
• L1= { aa}
a
= {a q1 q2
(a+b)*a} a
• L2= {bb} q0 b
b
= {b (a+b)*b} q3
a, b
RE= a(a+b)*a + b (a+b)*
b
NFA
Examples
Draw a NFA for all possible strings start and end with different
letter defined over alphabet ∑ = {a, b}.
• Given condition means if string start with ‘a’ then it should end with ‘b’ or
if it start with ‘b’ then it should end with ‘a’
a, b
• L1= { ab}
b
= {a q1 q2
(a+b)*b} a
• L2= {ba} q0 a
b
= {b (a+b)*a} q3
a, b
RE= a(a+b)*b + b (a+b)*
a
Draw NFA for all the strings that contains ‘aa’, defined over alphabet ∑ = {a,
b}.
• Draw NFA for string that contain at least one ‘a’ and end with ‘b’
defined over alphabet ∑ = {a, b}.
a a
q0 q1 q2
NFA
• Draw NFA for string that contain at least one ‘a’ and end with ‘b’
Examples
defined over alphabet ∑ = {a, b}.
a, b a, b
a b
q0 q1 q2