You are on page 1of 34

Week 4 ::

Theory of Programming Languages

Week # 04
Anas Bilal

Theory of Programming Languages


Context Free Grammar

• Grammar: set of 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| ϵ)
Example

• Conversion of language
into grammar
• S →aSb | ϵ
• Derivation tree
Example
Example
Example
Example
CFL to CFG
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)

a (satisfies the minimum


requirement)

Generates ba and does not


satisfy the condition

Trap b/dead state


Deterministic Finite Automaton (DFA)

Trap b/dead state

Create a self loop of a,b


at q1 state

Create a self loop of a,b


at q2 state (dead state)
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, ab, ba, abab, …}
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
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 of all binary strings in which 2nd last element is
1.
NFA of all binary strings in which 2nd last element is
1.
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’}

You might also like