TOC II Updated
TOC II Updated
SATHYABAMA
Institute of Science and Technology
Deemed to be University
Chennai.
Derivation:
S → aSa rule 1
S =>aSa
S → bSb rule 2 => abSba from rule 2
=> abbSbba from rule 2
S→c => abbcbba from rule 3
G= ( {S} ,{a,b,c} , P, {S}) =>abbcbba
3.Construct a CFG for the language L = anb2n
where n>=1.
String = {abb, aabbbb, aaabbbbbb....}.
P:
S → aSbb | abb
Tuples: G =({S},{a,b},P, {S})
Derive: aabbbb
Derivation
S =>aSbb
=>aabbbb
4. Design a CFG for a language accepting balanced
parenthesis. Derive: ({[]})
Derivation:
T={ (,),{,},[,] } S=> ( S)
=>( { S } )
=>( { [ s ] } )
w= { (),{},[],(()),({[]}), …….} =>( { [ ] } )
P:
S->(S) /[S]/{S} / SS
S-> ε
5. Design a CFG for a language accepting Arithmetic
Expression Drive- a+a-a
Derivation:
E=>E+E
T={ +, -, *, /, a } =>E+E-E
=>a+E-E
P: =>a+a-E
=>a+a-a
E->E+E/E-E/E*E/ E/E / (E) / a
Given a CFG, find the Language generated by G
• Regular Grammar:
– A regular grammar is a mathematical object, G, with four
components,
G = (N, Σ, P, S),
where P is in any of the following form
• A → aB, A → a , A → ε
for A, B ∈ N, a ∈ Σ, and ε the empty string
Union, Concatenation and * of CFL’s
S=>S1S2=>*x1S2 =>*x1x2=x
(01) :
C->01
(01)* :
D->CD| ε
(011+1)*.(01)*
S->BD
B->AB / ε
A->011/1
D->CD/ ε
C->01
Derivation
Process of deriving a string using the grammar
Types :
Left Most Derivation (LMD)
Right Most Derivation (RMD)
Example :
E->E+E|E-E|E*E|E/E|(E)|id
String : id+id*id
LMD
Derivation trees and Ambiguity
• Derivation tree is a graphical representation for
the derivation.
• Called as Parse Tree
Properties:
• The root node is always a node indicating start
symbols.
• The leaf node is always terminal nodes.
• The interior nodes are always the non-terminal
nodes.
Example
Derivation Tree
P: E = E + E
E=E*E
E=a|b|c
String= a * b + c
E=>E+E
=>E*E+E
=>a*E+E
=>a*b+E
=> a*b+c
2. Draw a derivation tree for the string "bab" from
the CFG given by
P: S → bSb | a | b
Derivation Tree
String : bbabb
S=>bSb
=>bbSbb
=>bbabb
3. Draw a Parse Tree for the string " aabbabba " from
the CFG given by
P:S → aB | bA Derivation tree
A → a | aS | bAA
B → b | bS | aBB
String=aabbabba
S=>aB
=>aaBB
=>aabSB
=>aabbAB
=>aabbaB
=>aabbabS
=>aabbabbA
=>aabbabba
Parse Tree
• Yield of a Tree:
– It is the string obtained by reading the leaf's from left
to right without considering null symbol.
Hence G is ambiguous
2. Check whether the given grammar G is ambiguous
or not.
• P: S → aSb | SS DT1 DT2
S→ε
• String =aabb
Solution:
Step 1: Eliminate start symbol from the RHS.
S1 → S Where S1 is the new start symbol.
Step 2: In the grammar, remove the null, unit and
useless productions.
No Null Production
Step 3: Eliminate terminals from the RHS of the
production if they exist with other non-terminals or
terminals.
For example, production
S → aA can be decomposed as:
S → RA
R→a
Step 4: Eliminate RHS with more than two non-terminals.
For example,
S → ASB
can be decomposed as:
• S → RS
• R → AS
1. Convert the given CFG to CNF. Consider the given grammar G1:
S → a | aA | B
A → aBB | ε
B → Aa | b
Solution:
Step 1: We will create a new production S1 → S, as the start symbol S appears on
the RHS.
The grammar will be:
S1 → S
S → a | aA | B
A → aBB | ε
B → Aa | b
Step 2: Removal of null productions. As grammar G1 contains A → ε null
production
S1 → S
S → a | aA | B
A → aBB
B → Aa | b | a
Now, as grammar G1 contains Unit production S → B, its removal
yield:
S1 → S
S → a | aA | Aa | b
A → aBB
B → Aa | b | a
Also remove the unit production S1 → S, its removal from the
grammar yields:
S1 → a | aA | Aa | b
S → a | aA | Aa | b
A → aBB
B → Aa | b | a
• Step 3: In the production rule
S0 → aA | Aa,
S → aA | Aa,
A → aBB
B → Aa,
terminal a exists on RHS with non-terminals. So we will replace
terminal a with X:
S0 → a | XA | AX | b
S → a | XA | AX | b
A → XBB
B → AX | b | a
X→a
S0 → a | XA | AX | b
S → a | XA | AX | b
A → XBB
B → AX | b | a
X→a