Toc Iii
Toc Iii
SATHYABAMA
Institute of Science and Technology
Deemed to be University
Chennai.
Derive -aaabbb
δ(q0, aaabbb, Z) ⊢ δ(q0, aabbb, aZ)
⊢ δ(q0, abbb, aaZ)
⊢ δ(q0, bbb, aaaZ)
⊢ δ(q1, bb, aaZ)
⊢ δ(q1, b, aZ)
⊢ δ(q1, ε, Z)
⊢ δ(q2, ε)
Trace the moves: a3b3 aaabbb
- q0 aabb Z0
1 q0 abb aZ
2 q0 bb aaZ
3 q1 b aZ
4 q1 ε Z
5 q2 ε Z
2. Design PDA to accept L={xCxr / x = {a, b}*}
Solution:
Some string will come followed by one 'c',
followed by reverse of the string before 'c'.
Logic:
– For every a's and b's push them into STACK
– When 'c' comes do nothing.
– Start popping STACK: 'a' for 'a' and 'b' for 'b'.
• State Diagram:
• Transition Fuction:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, Z) = (q0, bZ)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)
// this is decision step
δ(q0, c, a) = (q1, a)
δ(q0, c, b) = (q1, b)
δ(q1, b, b) = (q1, ε)
δ(q1, a, a) = (q1, ε)
δ(q1, ε, Z) = (qf, Z)
ID
Derive : abCba
δ(q0, abCba, Z) ⊢ δ(q0, bCba, aZ)
⊢ δ(q0, Cba, baZ)
⊢ δ(q1, ba, baZ)
⊢ δ(q1, a, aZ)
⊢ δ(q1, ε, Z)
⊢ δ(qf, ε)
Accept
ID
Derive : baCba
δ(q0, baCba, Z) ⊢ δ(q0, aCba, bZ)
⊢ δ(q0, Cba, abZ)
⊢ δ(q1, ba, abZ)
⊢Reject
Trace the moves: abCba
- q0 abCba Z
1 q0 bCba aZ
2 q0 Cba baZ
3 q1 ba baZ
4 q1 a aZ
5 q1 Null Z
6 q2 Null Z (Accepted)
3. Consider the CFG: S->[S] | {S} | ꓥ Generate the CFL
and PDA
w={ꓥ,[],{},{[]},{{}},[[]], [{}],…..}
Language: Balanced Parentheses
Solution:
Open parentheses followed by symbol ‘S’ and then closed
parentheses.
Define the states:
q0 – push all Open parentheses on to the stack.
q1 – when a close parentheses , pop open parentheses from
stack.
q2 – accepting state.
Transition Diagram
Transition Table
Trace the moves: {[{}]}
Move No. Resulting state Input Stack
- q0 {[{}]} Z0
2 q0 [{}]} {z0
4 q0 {}]} [{z0
6 q0 }]} {[{z0
7 q1 ]} [{z0
10 q1 } {z0
9 q1 Ʌ Z0
11 q2 Ʌ Z0
Accept
Trace the Moves: {[{]
PDA Acceptance
1. Acceptance by Final State:
– Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language
acceptable by the final state can be defined as:
– L(PDA) = {w | (q0, w, Z) ⊢* (p, ε, Z ), p ∈ F}
2. Acceptance by Empty Stack:
– Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language
acceptable by empty stack can be defined as:
– N(PDA) = {w | (q0, w, Z) ⊢* (p, ε, ε), p ∈ F}
3. Language Acceptance:
– A language L ⊆ ∑* is said to be accepted by M, if L is
precisely the set of string accepted by M.
L=L(M)
PDA Corresponding to CFG
Parsing:
– To derive a string using the production rules for a grammar.
– It is used to check whether or not a string is syntactically
correct.
– Parser takes the inputs and builds a parse tree.
Two types of parser:
• Top down Parser- Parsing starts from the top with the
start symbol and derives a string using a parse tree.
• Bottom up parser- Starts from the bottom with the
string and comes to the start symbol using a parse
tree.
Design of Top down parser:
• Derive: abaa
• δ(q0, abaa, Z) ⊢δ(q1, abaa, SZ)
⊢ δ(q1, abaa, SbSZ)
⊢ δ(q1, abaa, abSZ)
⊢ δ(q1, baa, bSZ) LMD
⊢ δ(q1, aa, SZ)
S=>SbS
⊢ δ(q1, aa, aSZ) =>abS S->a
⊢ δ(q1, a, SZ) =>abaS S->as
=>abaa S->a
⊢ δ(q1, a, aZ)
⊢ δ(q1, ε, Z)
⊢ q2 (Accept)
Problem 2:
P: S->S+X | X
X->X*X | Y
Y->(S) | id
String: id+id*id
• State Diagram:
Transition Table:
ID
Example: id*id+id
S=>S+X S->S+X
=>X+X S->X
=>X*X+X X->X*X
=>Y*X+X X->Y
=>id*X+X Y->id
=>id*Y+X X->Y
=>id*id+X Y->id
=>id*id+Y X->Y
=>id*id+id Y->id
Bottom Up Parsing
• Right Most Derivation in reverse is used.
• Steps:
– Push the current input symbol into the stack.
– Replace the right-hand side of a production at the
top of the stack with its left-hand side.
– If the top of the stack element matches with the
current input symbol, pop it.
– If the input string is fully read and only if the start
symbol ‘S’ remains in the stack, pop it and go to
the final state ‘F’.
• Example
P: S->S+T
S->T
T->T*a
T->a
String: a+a*a RMD in Reverse