Professional Documents
Culture Documents
P: ( {S → AB, A → a, B → b})
Productions, P : S → AB, A → a, B → b
Example:
Production rules:
S → aSa
S → bSb
S→c
Conti…
Now check that abbcbba string can be derived from the given
CFG.
S ⇒ aSa
S ⇒ abSba
S ⇒ abbSbba
S ⇒ abbcbba
The following table shows how they differ from each other:
Conti…
Conti…
Type – 3 (Regular) Grammar
•Type-3 grammars generate regular languages. These languages
are exactly all languages that can be decided by finite state
automaton. Type 3 is most restricted form of grammar.
VVT* | T* [LLG] or
VT*V | T* [RLG].
Example:
X → a | aY
Y→b
X→ε
Type-2 (Context Free) grammars
Type - 2 Grammar generate context-free languages.
Example: S → Xa
X→a
X → aX
X → abc
X→ε
Type-1 [Context Sensitive] grammars
Type - 1 Grammars generate context-sensitive languages.
The grammar productions must be in the form: α →β, |α | ≤ |β| i.e. count of symbols
in α is less than or equal to β, and α, β Ɛ V* & T*.
S → abc/aAbc
Ab → bA
Ac → Bbcc
bB → Bb
aB → aa/aaA
What is the language generated by this grammar?
Type-0 (Unrestricted or Recursively Enumerable ) Grammar
•Type – 0 Grammar include all formal grammars.
•Type 0 grammar generate recursively enumerable languages. The
productions have no restrictions.
•They generate the languages that are recognized by a Turing
Machine.
•The productions can be in the form of α → β where α is
(V+T)*V(V+T)* and β is (V+T)*.
Example:
S ABaC,
Ba aaB,
BC DC | E,
aD Da,
AD AB,
aE Ea,
AE λ.
Introduction to Context-Free Grammar
Context free grammar: It is a formal grammar which is used to
generate all possible strings in a given formal language.
P:
A → aA,
A → abc.
P:
S → aSa, S → bSb, S → ε
•Example 1:
•Construct the CFG for the language having any number of a's
over the set ∑= {a}.
Solution:
2. S → 0S | 1S
3. S → ε
The rules are in the combination of 0's and 1's with the start
symbol. Since (0+1)* indicates {ε, 0, 1, 01, 10, 00, 11, ....}. In
this set, ε is a string, so in the rule, we can set the rule S → ε.
Cont…
Example 3: Construct a CFG for a language L = {wcwR | where
w Ɛ (a, b)*}.
Solution: The string that can be generated for a given language is
{aacaa, bcb, abcba, bacab, abbcbba, ....}
The grammar could be:
1.S → aSa rule 1
2.S → bSb rule 2
3.S → c rule 3 ፣ Now if we want to derive a string
"abbcbba", we can start with start symbols.
4.S → aSa
5.S → abSba from rule 2
6.S → abbSbba from rule 2
7.S → abbcbba from rule 3 , Thus any of this kind of string
can be derived from the given production rules.
Cont…
Example 4:
• Construct a CFG for the language L = anb2n where n>=1.
Solution: The string that can be generated for a given language is
{abb, aabbbb, aaabbbbbb....}.
The grammar could be:
1.S → aSbb | abb
Now if we want to derive a string "aabbbb", we can start with
start symbols.
1.S → aSbb
2.S → aabbbb
Difference:
2. 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:
In the left most derivation, the input is scanned and replaced with
the production rule from left to right (left most variable). So in
left most derivatives we read the input string from left to right.
Example:
Production rules:
S=S+S
S=S-S
S = a | b |c
Input: a - b + c
The left-most derivation is:
Cont…
S=S+S
S=S-S+S
S=a-S+S
S=a-b+S
S=a-b+c
b) Right-Most Derivation
S=S+S
S=S-S
S = a | b |c
Input: a - b + c
S=S-S
S=S-S+S
S=S+S
S=S-S
S = a | b |c
S = S - S (given)
S = S - S + S (RM)
S = a - S + S (LM)
S = a - S + c (RM)
S=a-b+c
Cont…
d) Derivation Tree: It is called parse 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:
Example:
Production rules:
• T= T + T | T * T
• T = a|b|c
• Input: a * b + c
Conti…
Conti…
Conti…
There are two different approaches to draw a derivation tree:
Every Regular and Linear grammars are clearly CFG, but a CFG
may not necessarily Linear or regular.
Conti…
•The rightmost derivation for the above string "a+a*a" may be:
Recursive Grammars
1) SSaS | Sb The language(set of strings) generated by the above grammar is :{b, bab,
2) SAa, AAb|c The language generated by the above grammar is :{ca, cba, cbba …}, which is
infinite.
Non-Recursive Grammars
SAa, Ab|c The language generated by the above grammar is :{ba, ca}, which is finite.
Types of Recursive Grammars: Based on the nature of the recursion in a recursive grammar, a
•If there are more than 1 derivation tree, then the CFG is
ambiguous, i.e. Ambiguous grammar: A CFG is said to be
ambiguous if there exists more than one derivation tree for the
given input string i.e., more than one Left Most Derivation Tree
(LMDT) or Right Most Derivation Tree (RMDT).
3. Parsing and Ambiguity
Ambiguity in Context free Grammar and Context free Languages
Note:
•If a context free grammar G is ambiguous, language generated by grammar
L(G) may or may not be ambiguous.
1.S → AB | aaB
2.A → a | Aa
3.B → b
S aaB aab
•s there are two different parse tree for deriving the same string, the given grammar is
ambiguous. Unambiguous grammar will be:
1.S → AB
2.A → Aa | a
3.B → b
Unambiguous Grammar
Example 2: Show that the given grammar is ambiguous. Also,
find an equivalent unambiguous grammar.
1. S → ABA
2. A → aA | ε
3. B → bB | ε
We need no more than |W| rounds for finding ‘W’ string.
This method is not used for efficient parsing, rather used for
efficiency is secondary issue.
Cont…
Example: Consider the following grammar.
Procedures:
Round 1:Gives us
SSS, selected
SaSb, selected
S λ, not selected
Here the last two productions can be removed without further consideration.
Cont…
Round Two: Gives us the Sentential forms
SSSSSS, selected
SSSaSbS, selected
SSSbSaS, not selected
SSSS, not selected already evaluated at round 1
which are obtained by replacing the left most S in sentential forms with all
applicable substitutions. Similarly, from sentential form 2 we get the
additional sentential forms.
SaSbaSSb selected
SaSb,aaSbb selected
SaSbabSab not selected
SaSbab, not selected
Here again several of these can be removed from Sentential.
Cont…
Round Three: Gives us Sentential forms:
SSSSSSSSSS selected
SSSSSS aSbSS selected
SSSSSSbSaSS not selected
SSSSSSSS not selected, already evaluated
……………………………………………………….
SSSaSbSaSSbS selected
SSSaSbSaaSbbS selected
SSSaSbSabSabS not selected
SSSaSbSabS not selected
Cont…
--------------------------------------------------
SaSbaSSbaSSSb selected
SaSbaSSbaaSbSb selected
SaSbaSSbabsaSb not selected
SaSbaSSbaSb not selected already evaluated before
-------------------------------------------------------------
SaSb,aaSbbaaSSbb selected
SaSb,aaSbbaaaSbSbb not selected
SaSb,aaSbbaabSabb not selected
SaSb,aaSbbaabb selected[Parsing is Over with success]
Now Exhaustive parsing is success and the input is syntactically correct.
Individual Assignment 10%:
Apply Exhaustive Parsing for W=aaabbb and w=aabbab, show steps clearly and
neatly?
Cont…
Individual Assignment 10%:
Apply Exhaustive Parsing for W=aaabbb and w=aabbab, show steps clearly and neatly?
Solution: w=aaabbb
Round 1: Gives us
SSS, selected
SaSb, selected
SbSa, not selected
S λ, not selected
Here the last two productions can be removed without further consideration.
Round 2:
SSSsss
SSSaSbS
SSSbSaS
SSSS
SaSb
Simple Grammar (S-Grammar)
A context free grammar G = (V,T,S,P) is said to be simple grammar or
While S-grammars are quite restrictive, they are of some interest. Many
Grammar.
Simple Grammar (S-Grammar)
Exercise: Find an S-grammar for L(aaa*b+b)?
G=({S, A, B},{a,b},S,P),
P: SaA|b
AaB
BaB|b
a) L= {anbn+1:n ≥ 0},
a) L= {anbn+1:n ≥ 1},
a) L= {anbn+1:n ≥ 0},
a) L= {anbn+1:n ≥ 1},
a) L= {anbn+1:n ≥ 2},
1.SaACaaXBCaabBC aabbCaabbb
2.SaACaaXBC aaaXBBC aaabBBC aaabbBC aaabbbC aaabbbb
a) L= {anbn+1:n ≥ 2},
1.SaACaaXBCaabBC aabbCaabbb
2.SaACaaXBC aaaXBBC aaabBBC aaabbBC aaabbbC aaabbbb
X → X*X
X → X| a , is ambiguous or not.
Solution: Let’s find out the derivation tree for the random string
w="a+a*a". It has two leftmost derivations.
Derivation 1: X → X+X → a +X → a+ X*X → a+a*X → a+a*a
Parse tree 1:
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", the
grammar G is ambiguous.
Cont…
Problem 2:
Let us consider this grammar : E -> E+E|id
We can create 2 parse tree from this grammar to obtain a
string id+id+id , The above are the 2 parse trees generated by
left most derivation:
Cont…
•Both the above parse trees are derived from same grammar rules
but both parse trees are different. Hence the grammar is
ambiguous.
Problem 3: Let us now consider the following grammar: Set of
alphabets ∑ = {0,…,9, +, *, (, )}
E -> I
E -> E + E
E -> E * E
E -> (E)
I -> ε | 0 | 1 | … | 9
Cont…
From the above grammar String 3*2+5 can be derived in 2 ways:
I) First leftmost derivation
E=>E*E =>I*E =>3*E+E =>3*I+E =>3*2+E =>3*2+I =>3*2+5
II) Second leftmost derivation
E=>E+E =>E*E+E =>I*E+E =>3*E+E =>3*I+E =>3*2+I =>3*2+5
HW:3*(2+5), parse this string?
Following are some examples of ambiguous grammars:
S-> aS |Sa|Є
E-> E +E | E*E|id
A -> AA | (A) |a
S -> SS|AB , A -> Aa|a , B ->Bb|b
Where as following grammars are unambiguous:
S -> (L) | a, L -> LS | S
S -> AA , A -> aA , A -> b
HW:
1. Show that the following grammar is ambiguous?
SAB/aaB
Aa/Aa
Bb
2. EI|E+E|E*E|(E),
Ia|b|c,
p: S -> aS | Sa | a
Now for string ‘aaa’ we will have 4 parse trees, hence ambiguous.
Cont…
Inherently Ambiguous Language
Let L be a Context Free Language (CFL), If every Context Free
Grammar G with Language L = L(G) is ambiguous, then L is
said to be inherently ambiguous Language. Ambiguity is a
property of grammar not languages. Ambiguous grammar is
unlikely to be useful for a programming language, because two
parse trees structure(or more) for the same input string (program)
implies two different meanings (executable programs) for the
program.
L1 says number of a’s should be equal to number of b’s and L2 says number of
b’s should be equal to number of c’s.
•Their union says either of two conditions to be true. So it is also context free
language.
L1 says number of a’s should be equal to number of b’s and L2 says number of
c’s should be equal to number of d’s. Their concatenation says first number of
a’s should be equal to number of b’s, then number of c’s should be equal to
number of d’s. So, we can create a PDA which will first push for a’s, pop for
b’s, push for c’s then pop for d’s. So it can be accepted by pushdown automata,
hence context free.
Example:
language.
P: SAB | Ɛ, A aA | Ɛ, B bBc | Ɛ.
P: SAB | Ɛ, A aA | Ɛ, B aB | Ɛ.
L1 ∩ L2 = {an | n>=0 }.
P: S aS | Ɛ
Cont…
3) Complement − Similarly, complementation of context free
Note :
CFL are not closed under Reverse, difference, Intersection and Complementation.
Reading Assignment:
Deterministic Context-free Languages
Deterministic CFL are subset of CFL which can be recognized
by Deterministic PDA. Deterministic PDA has only one move
from a given state and input symbol, i.e., it do not have choice.
For a language to be DCFL it should be clear when to Push or
POP.
Deterministic Context-free Languages
For example, L1 = { anbncm | m >= 0 and n >= 0} is a DCFL because for a’s,
we can push on stack and for b’s we can pop. It can be recognized by
Deterministic PDA.
Exercise:
1.SaSb|λ|A, AaA,
Once the set VN has been found, we are ready to construct new
Production rules p’. To do so, we look at all productions in p of
the form AX1X2…Xn, n >=1, where each Xi ε VUT. For each
such productions of p, we put into p’ that production as well as all
those generated by replacing nullable variables with λ in all
possible considerations.
Cont…
After finding all the nullable variables, we can now start to
construct the null production free grammar.
•If all the variables on the RHS of the production are nullable ,
then we do not add ‘A λ’ to the new grammar. See no. (2)
We apply the same rule to line (2),(3),(4): The new grammar now
becomes:
A BC | B | C, B bB | b, C cC | c
Cont…
Exercise:
Find a CFG without λ productions equivalent to the grammar
defined below.
A) SaS1b,S1aS1b|λ
B) SABaC, ABc, Bb|λ, CD|λ, Dd.
C) S → XYX
X → 0X | ε
Y → 1Y | ε
S XYX | XY | YX | XX | X | Y
X 0X|0
Y 1Y|1
Cont…
3.Unit Productions Removal: The productions of type A B,
where A, B ε V is called unit productions. To remove unit
productions, we use the useful substitution rule.
SAa | A | B , B A | bb, A a | bc | B.
B a | bc, BA
Cont…
To obtain the equivalent grammar G,
S a | bc | bb | Aa
A a | bb | bc
B a | bb | bc
Note: Useful substitution may create useless productions.
So in the above example the removal of unit production has made B and
associated productions useless.
Generally, removing λ-production may create new unit production and
removing of unit production may create useless productions. So we have to
follow the following sequence of steps.
1. Removal of λ-productions
2. Removal of unit productions
3. Removal of useless productions
Cont…
Example:
S → 0A | 1B | C
A → 0S | 00
B→1|A
C → 01
Worksheet
1.Use Exhaustive parsing method to parse the string w = abbbbbb with the
grammar S aAB, A bBb, BA/λ
2.Remove useless productions:
a) P: S → AC | B, A → a, C → c | BC, E → aA | e
b) P: Sa|aA|B|C, AaB| λ, BAa, CcCD, Dddd.
3. Removal null production from the following:
a) P: S → ASA | aB | b, A → B, B → b | ∈
b) P: SAaB|aaB, A-λ,BbbA|λ
c) SaS1b,S1aS1b|λ
d) SABaC, ABc, Bb|λ, CD|λ, Dd.
4. Remove unit production from the following:
a) S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
b) S -> Aa |B, A -> b | B, B -> A | a,
c) SabAB|ba, Aaaa, BaA|bb,
d) SaA|aBB, AaaA|λ, BbB|bbC, CB
Question number 2b,3a&c,4b&d are Group assignment work 10% to be submitted!!