RG CFG AMbiguity
RG CFG AMbiguity
DFA:-
DFA stands for Deterministic Finite Automata. Deterministic refers to the uniqueness of the
computation. In DFA, the input character goes to one state only. DFA doesn't accept the null
move that means the DFA cannot change state without any input character.
NDFA:-
NDFA refer to the Non Deterministic Finite Automata. It is used to transit the any number of
states for a particular input. NDFA accepts the NULL move that means it can change state
without reading the symbols.
NDFA also has five states same as DFA. But NDFA has different transition function.
Transition function of NDFA can be defined as δ: Q x ∑ →2Q
Regular expression
Union: If L and M are two regular languages then their union L U M is also a union.
L U M = {s | s is in L or s is in M}
Intersection: If L and M are two regular languages then their intersection is also an
intersection.
L ⋂ M = {st | s is in L and t is in M}
Kleene closure: If L is a regular language then its kleene closure L1* will also be a regular
language.
Solution: The string of language L starts with "a" followed by atleast three b's. Itcontains
atleast one "a" or one "b" that is string are like abbba, abbbbbba, abbbbbbbb, abbbb.....a
Context free grammar is a formal grammar which is used to generate all possible strings in
a given formal language.
Context free grammar G can be defined by four tuples as:
G= (V, T, P, S)
Where,
In CFG, the start symbol is used to derive the string. You can derive the string by
repeatedly replacing a non-terminal by the right hand side of the production, until all non-
terminal have been replaced by terminal symbols.
Example:
Production rules:
1. S → aSa
2. S → bSb
3. S → c
Now check that abbcbba string can be derived from the given CFG.
1. S ⇒ aSa
2. S ⇒ abSba
3. S ⇒ abbSbba
4. S ⇒ abbcbba
By applying the production S → aSa, S → bSb recursively and finally applying the
production S → c, we get the string abbcbba
Capabilities of CFG:-
Derivation
Derivation is a sequence of production rules. It is used to get the input string through these
production rules. During parsing we have to take two decisions. These are as follows:
We have two options to decide which non-terminal to be replaced with production rule.
Left-most Derivation
In the left most derivation, the input is scanned and replaced with the production rule from
left to right. So in left most derivatives we read the input string from left to right.
Example:
Production rules:
1. S S + S
2. S S - S
3. S a | b |c
Input:
a - b + c
1. S S+S
2. S S-S+S
3. S a-S+S
4. S a-b+S
5. S a-b+c
Right-most Derivation
In the right most derivation, the input is scanned and replaced with the production rule from
right to left. So in right most derivatives we read the input string from right to left.
Example:
1. S S + S
2. S S - S
3. Sa | b |c
Input: a - b + c
1. S S-S
2. S S-S+S
3. S S-S+c
4. S S-b+c
5. S a-b+c
Parse tree:-
T a|b|c
Input: a * b + c
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Ambiguity:-
A grammar is said to be ambiguous if there exists more than one leftmost derivation or
more than one rightmost derivative or more than one parse tree for the given input string.
If the grammar is not ambiguous then it is called unambiguous.
Example:
S aSb | SS
S∈
For the string aabb, the above grammar generates two parse trees:
If the grammar has ambiguity then it is not good for a compiler construction. No method
can automatically detect and remove the ambiguity but you can remove ambiguity by re-
writing the whole grammar without ambiguity.
A grammar is left recursion if it has a non-terminal A such that there is a derivation A → Aa/b for some
string a.
A→ bA'
A'→ αA'| ε
Left Factoring:-
If A αβ1|αβ2 are two A-productions and the input begins with a non empty string derived from α, we
do not know whether to expand A to αβ1 or to αβ2. We may defer decision by expanding A to αA'. Then
after seeing the input derived from a we expand A' to β1or to β2
AαA'
A'β1| β2
In a more standard way Aαβ1| αβ2|..............| αβn|γ , may be left factored as:-
AαA'|γ