Flat 2
Flat 2
Module 2
CFLs are used by the compiler in the parsing phase as they define
the syntax of a programming language and are used in many
editors.
Formal definition
Note –
CNF is a pre processing step used in various algorithms.
For generation of string x of length ‘m’, it requires ‘2m-1’
production or steps in CNF.
Problem
Convert the following CFG into CNF
S → XY | Xn | p
X → mX | m
Y → Xn | o
Solution
Here, S does not appear on the right side of any production and
there are no unit or null productions in the production rule set.
So, we can skip Step 1 to Step 3.
Step 4
Now after replacing
X in S → XY | Xo | p
with
mX | m
we obtain
S → mXY | mY | mXo | mo | p.
And after replacing
X in Y → Xn | o
with the right side of
X → mX | m
we obtain
Y → mXn | mn | o.
Two new productions O → o and P → p are added to the
production set and then we came to the final GNF as the
following −
S → mXY | mY | mXC | mC | p
X → mX | m
Y → mXD | mD | o
O→o
P→p
Example:
Solution:
Simulation of abaaba
⊢ δ(q2, a, aZ)
Apply rule 8
⊢ δ(q2, ε, Z)
Apply rule 7
⊢ δ(q2, ε)
Apply rule 11
Accept
Here, parse trees are constructed from bottom up, not top down.
E→E+T|E-T|T
T→T*F|F
F→a|(E)
E → E+E | a
We write:
xn = xn′ ∈ Σ*
Namely they start with the same non-terminal and end at the
same terminal string and have at least two intermediate steps.
Let’s say D < D′ if the two derivations differ in only one step in
which there are 2 non-terminals, A and B, such that D replaces
the left one before the right one and D′ does the opposite.
Formally:
butS => SS =>SaSb =>Sab =>aSbab =>abab is RMD but not LMD.
Union
free.
Example
ab
Let A = { c d , m ≥ 0}. Corresponding grammar G will have P:
2
m m
2
S2 → cBb| ε
Union of A and A , A = A ∪ A = { x y } ∪ { c d }
1 2 1 2
n n m m
The corresponding grammar G will have the additional production S → S1
| S2
Concatenation
Example
Kleene Star
Example
Kleene Star L = { x y }*
1
n n
Reference books