0% found this document useful (0 votes)
16 views36 pages

Lecture-3-Cfg + Dfa + Nfa

This document covers the theory of Context-Free Grammars (CFG) and Finite Automata, including Deterministic Finite Automata (DFA) and Non-deterministic Finite Automata (NFA). It provides formal definitions, examples, and graphical representations of these concepts, highlighting their differences and applications in programming languages. Additionally, it includes assignments and deadlines related to the topic.

Uploaded by

survivor000111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views36 pages

Lecture-3-Cfg + Dfa + Nfa

This document covers the theory of Context-Free Grammars (CFG) and Finite Automata, including Deterministic Finite Automata (DFA) and Non-deterministic Finite Automata (NFA). It provides formal definitions, examples, and graphical representations of these concepts, highlighting their differences and applications in programming languages. Additionally, it includes assignments and deadlines related to the topic.

Uploaded by

survivor000111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter 2

CFG, DFA, NFA


Week 04 = Lecture 03

Theory of Programming Languages


Context Free Grammar

• Grammar: Setof rules to construct the


sentences in a language
• Grammar in English language-to construct
the valid sentences
• CFG-defines set of rules that will then
generate strings
• Collection of strings=language
CFG
• Formal definition of CFG
• CFG: (V,T,P,S)
• V: finite set of variables (non-terminals)
– Value can be substitute in V
– Variables or terminals
– Represented by capital letters
• T:finite set of terminals (V∩T=Φ)
– Nothing is substituted in terminals
– Helps in generating strings
CFL to CFG
• P: most important
– Substitution rules
– A→B
– LHS=only one variable
– RHS=variable/terminal/both
• S:given at the starting of the leftmost
– S →0S1 (S →0S1| ϵ)
– S→ϵ
– Start symbol: S
– Variable: S
– Terminals: 0,1, ϵ
CFG
• S →0A
• A →A0/0
• Keep on replacing A with A0, and put 0 where
want to terminate
Example

• Solve (S →0S1| ϵ)
• Generate parse tree
• CFL?
• {0n1n, n>=0}
Example

• Conversion of language into grammar


• CFL -> CFG
• Anbn, n>=0
• S →aSb | ϵ
• Derivation tree
Example

• CFL: Anbn, n>=1


• Generate CFG
• Verify it using Derivation tree
Example
Example
Example
Example

• Assignment 02
• Deadline: 31 Oct 2022
Types of Finite Automata

• Finite Automaton can be classified into two types:


– Deterministic Finite Automaton (DFA)
– Non-deterministic Finite Automaton (NDFA / NFA)
• Deterministic Finite Automata (DFA)
– In DFA, for each input symbol, one can determine the
state to which the machine will move. Hence, it is
called Deterministic Automata
– As it has a finite number of states, the machine is
called Deterministic Finite Machine or Deterministic
Finite Automata
– In DFA, there is only one path for specific input from
the current state to the next state
Types of Finite Automata

• Deterministic Finite Automata (DFA)


– DFA does not accept the null move, i.e., the DFA cannot
change state without any input character
– DFA can contain multiple final states
• In the figure, from state q0 for input a, there is only
one path which is going to q1
• Similarly, from q0, there is only one path for input b
going to q2
Deterministic Finite Automaton (DFA)

• Formal Definition of a DFA


• A DFA can be represented by a 5-tuple
(Q, ∑, δ, q0, F) where −
– Q is a finite set of states
– ∑ is a finite set of symbols called the alphabet
– δ is the transition function where δ: Q × ∑ → Q
– q0 is the initial state from where any input is processed
(q0 ∈ Q)
– F is a set of final state/states of Q (F ⊆ Q)
• Transition function can be defined as:
– δ: Q x ∑→Q
Deterministic Finite Automaton (DFA)

• Graphical Representation of a DFA


– A DFA is represented by digraphs called state diagram
– The vertices represent the states
– The arcs labeled with an input alphabet show the
transitions
– The initial state is denoted by an empty single incoming
arc/an arrow
– The final state is indicated by double circles
Deterministic Finite Automaton (DFA)

• String starting with a


– {a, aa,aaa,…. ab,abb,aab….}
– ba, baa etc not allowed
Deterministic Finite Automaton (DFA)

a (satisfies the minimum


requirement)

It will accept, b at the start


Which is against our question

Generates ba, so it will also


not work
Deterministic Finite Automaton (DFA)

We will trap, b in state q2

Dead state usually, also loops


into itself
Deterministic Finite Automaton (DFA)

• Transition function can be defined as:


– δ: Q x ∑→Q
Deterministic Finite Automaton (DFA)

• Example: Construct a DFA which accepts a


language of all strings
– Containing ‘a’
– Ending at ‘a’
Start with min req{a, aa, aaa, ba, aba, …}
Deterministic Finite Automaton (DFA)

• Here we have three possibilities for b at q0

Won’t accept ever


Deterministic Finite Automaton (DFA)

String=ab

• Three possibilities for b at q0

Accepted Accepting
ba, bba, bbba Will not accept
single b ba
Going to trap
Deterministic Finite Automaton (DFA)

State q1
String=ab

Trap state

Create a self
loop of a at q1

If goes to trap state for b, then will not accept


the string aaba.
B can’t go to trap state.
Deterministic Finite Automaton (DFA)

For string aaba, it will not return to the


final state

Will not accept the


string aab
Deterministic Finite Automaton (DFA)

• Example: Construct a DFA which accepts a


language of all strings
– Ending at ‘a’
Non-Deterministic Finite Automata (NFA)

• In NFA, for a particular input symbol, the machine


can move to any combination of the states in the
machine.
• In other words, the exact state to which the
machine moves cannot be determined. Hence, it is
called Non-deterministic Automaton.
• As it has finite number of states, the machine is
called Non-deterministic Finite
Machine or Non-deterministic Finite
Automaton.
Non-Deterministic Finite Automata (NFA)

• Formal Definition of an NFA:


– An NDFA can be represented by a 5-tuple (Q, ∑, δ, q0,
F) where −
– Q is a finite set of states.
– ∑ is a finite set of symbols called the alphabets.
– δ is the transition function where δ: Q × ∑ → 2Q
– (Here the power set of Q (2Q) has been taken because in
case of NDFA, from a state, transition can occur to any
combination of Q states)
– q0 is the initial state from where any input is processed
(q0 ∈ Q).
– F is a set of final state/states of Q (F ⊆ Q).
Non-Deterministic Finite Automata (DFA)

• Graphical Representation of an NFA: (same as


DFA)
• An NDFA is represented by digraphs called state
diagram.
– The vertices represent the states.
– The arcs labeled with an input alphabet show the
transitions.
– The initial state is denoted by an empty single incoming
arc.
– The final state is indicated by double circles.
Non-Deterministic Finite Automata (NFA)

transition function: δ: Q x ∑ →2Q


Non-Deterministic Finite Automata (NFA)

transition function:
DFA VS NFA
DFA
• Dead configuration is NOT allowed
– δ (transition function)

• Multiple choices are NOT allowed for an input


• Null move is not allowed
• Digital computers are deterministic
• Designing and understanding is difficult
DFA VS NFA
NFA
• Dead configuration is allowed
• Multiple choices are allowed for an input
• Null move is allowed
• Non-deterministic feature is not associated with
real computers
• Designing and understanding is easy
NFA Example

• L1={Set of all strings that ends with ‘1’}


• L2={Set of all strings that contains ‘0’}
• L3={Set of all strings that ends with ‘10’}
• L4={Set of all strings that contains ‘01’}
• L5={Set of all strings that ends with ‘11’}
NFA Example

• L1={Set of all strings that have 2nd last ‘1’}


Conversion from NFA to DFA

• L1={Set of all strings that have 2nd last ‘1’}

You might also like