Flat - Unit 3
Flat - Unit 3
Flat - Unit 3
• i.e., the left-hand side of the production contains only one nonterminal
and right hand side of the production contains any set of terminals
and non-terminals.
Example
Derivation:
Then each nonterminal is replaced with its right side of the productions.
FLAT NOTES_UNIT 3 1
Example:
Basically there are two types of derivations in context free grammar. They
are
Example
The rightmost derivation for the string a+a*a from above grammar is −
FLAT NOTES_UNIT 3 2
Derivation Tree
A derivation tree or parse tree is an ordered rooted tree that graphically
represents the semantic information of a string derived from a context-free
grammar.
Representation Technique
• Root vertex − Must be labeled by the start symbol.
Example
FLAT NOTES_UNIT 3 3
Ambiguity in Context-Free Grammars
If a context free grammar G has more than one derivation trees for some
string w ∈ L(G), it is called an ambiguous grammar. There exist multiple
right-most or left-most derivations for some string generated from that
grammar.
Problem
Check whether the grammar G with production rules −
is ambiguous or not.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost
derivations.
FLAT NOTES_UNIT 3 4
Derivation 2 − X → X*X → X+X*X → a+ X*X → a+a*X → a+a*a
Parse tree 2
Since there are two parse trees for a single string "a+a*a", then
grammar G is ambiguous.
FLAT NOTES_UNIT 3 5
Applications of Context Free Grammars:
Data Processing
Neural Networks
FLAT NOTES_UNIT 3 6
Pushdown Automata Introduction
• an input tape,
• a control unit, and
• a stack with infinite size.
The stack head scans the top symbol of the stack.
• No-change – top of the stack symbol is read and that remains same.
A PDA may or may not read an input symbol, but it has to read the top of
the stack in every transition.
FLAT NOTES_UNIT 3 7
A PDA can be formally described as a 7-tuple (Q, ∑, S, δ, q0, Z0, F) −
• ∑ is input alphabet
• S is stack symbols
This means at state q1, if we encounter an input string ‘a’ and top symbol of
the stack is ‘b’, then we push ‘c’ on top of the stack ‘b’ and move to
state q2.
• q is the state
• w is unconsumed input
FLAT NOTES_UNIT 3 8
Turnstile Notation
The "turnstile" notation is used for connecting pairs of ID's that represent
one or many moves of a PDA. The process of transition is denoted by the
turnstile symbol "⊢".
In each of the string, the number of a’s are followed by equal number
of b’s.
Here, we need to maintain the order of a’s and b’s. That is, all the a’s
are coming first and then all the b’s are coming. Thus, we need a
stack along with the state diagram. The count of a’s and b’s is
maintained by the stack. We will take 2 stack alphabets: S= { a, z }
Where, S= set of all the stack alphabet
z = stack start symbol
As we want to design a PDA, thus every time ‘a’ comes before ‘b’.
When ‘a’ comes then push it in stack and if again ‘a’ comes then also
push it. After that, when ‘b’ comes then pop one ‘a’ from the stack
FLAT NOTES_UNIT 3 9
each time. So, at the end if the stack becomes empty then we can
say that the string is accepted by the PDA.
Finally stack empty and PDA reached to final State so we can conclude
string aabb is accepted.
FLAT NOTES_UNIT 3 10
2. Construct PDA on language that accepts L = {an bm cn | m,n>=1}?
Sol:
Here, we need to maintain the order of a’s, b’s and c’s.That is, all the a’s
are are coming first and then all the b’s and then c’s are coming. Thus, we
need a stack along with the state diagram. The count of a’s and c’s is
maintained by the stack. The number of a’s is exactly equal to the number
of c’s.
Every time ‘a’ comes before ‘b’. When ‘a’ comes then push it in stack and
if again ‘a’ comes then also push it. And, when ‘c’ comes then pop one ‘a’
from the stack each time . And for ‘b’, we will do nothing in the stack only
change the state in state diagram. So, at the end if the stack becomes
empty then we can say that the string is accepted by the PDA.
FLAT NOTES_UNIT 3 11
Pushdown Automata Acceptance
There are two different ways to define PDA acceptability.
For a PDA (Q, ∑, S, δ, q0, Z0, F), the language accepted by the set of final
states F is −
Initially we put a special symbol ‘$’ into the empty stack. At state q2,
the w is being read. In state q3, each 0 or 1 is popped when it matches the
input. If any other input is given, the PDA will go to a dead state. When we
reach that special symbol ‘$’, we go to the accepting state q4.
FLAT NOTES_UNIT 3 12
Empty Stack Acceptability
Here a PDA accepts a string when, after reading the entire string, the PDA
has emptied its stack.
For a PDA (Q, ∑, S, δ, q0, I, F), the language accepted by the empty stack
is −
Here, in this example, the number of ‘a’ and ‘b’ have to be same.
• Initially we put a special symbol ‘$’ into the empty stack.
FLAT NOTES_UNIT 3 13
Equivalence of PDA and CFGs:
If a grammar G is context-free, we can build an equivalent nondeterministic
PDA which accepts the language that is produced by the context-free
grammar G.
L(G) = L(P)
In the next two topics, we will discuss how to convert from PDA to CFG
and CFG to PDA.
Step 3 − The start symbol of CFG will be the start symbol in the PDA.
Step 4 − All non-terminals of the CFG will be the stack symbols of the PDA
and all the terminals of the CFG will be the input symbols of the PDA.
Step 5 − For each Non terminal production in the grammar of the form
A → aX, where a is terminal and A, X are combination of terminal and non-
terminals, make a transition
Step 6 – For each terminal in the grammar add new PDA transitions as
δ (q, a, a) = (q, ε )
FLAT NOTES_UNIT 3 14
Problem
Construct a PDA from the following CFG.
S → XS | ε , A → aXb | Ab | ab
Solution
Let the equivalent PDA,
δ(q, a, a) = {(q, ε )}
δ(q, b, b) = {(q, ε )}
FLAT NOTES_UNIT 3 15
Step 2: For every action of Popping move in PDA, production to be written
as
[q, Z, q’] → a
where q,q1 are different states, a is current input symbol, Z is present top
of the stack then no change operation is represented with same current top
of stack symbol.
where q,q1 are different states, a is current input symbol, Z0 is present top
of the stack, Z1 is new top of stack after the transition.
FLAT NOTES_UNIT 3 16
Problem
δ ( q0 ,1 ,x ) = (q0 , xx)
δ ( q0 ,0 ,x ) = (q0 , x)
δ ( q0 , 1 ,x ) = (q1 , ε)
δ ( q1 , ε ,x ) = (q1 , ε)
Sol:
δ ( q0 ,1 ,x ) = (q0 , xx)
FLAT NOTES_UNIT 3 17
δ ( q0 ,0 ,x ) = (q0 , x) : it is no change operation, so use step 3.
[q0, x, q1] → 1
δ ( q1 , ε ,x ) = (q1 , ε)
[q1, x, q1] → ε
FLAT NOTES_UNIT 3 18