SlideShare a Scribd company logo
Theory of Computation
CSE 2233
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG – Context-free Grammar
2
A Context-free Grammar is a 4 tuple (V, 𝛴, R, S), where
▸ V is a finite set called the variables
▸ Σ is a finite set, disjoint from V, called the terminals
▸ R is a finite set of rules, with each rule being a variable and a string of variables and terminals
▸ S ϵ V is the start variable
Example:
Grammar, G1 = ( { S }, { a, b }, R, S )
V = { S }
Σ = { a, b }
R is,
S → aSb | SS | ε
Example:
Grammar, G2 = ( V, Σ, R, <EXPR> )
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R is,
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFL – Context-free Language
3
Context-free Language:
– The collection of languages generated by some context-free grammars are called Context-free Languages.
– They include all the regular languages and many additional(recursive structure) languages.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Terminologies – yields, derives, derivation
4
▸ If u, v, w are strings of variables and terminals, and A → w is a rule of the grammar, we say that
uAv ⟹ uwv i.e. uAv yields uwv
▸ We say u derives v, written as
- if u = v or
- if a sequence u1, u2, u3, … … … , uk exists for k >= 0 and u ⟹ u1 ⟹ u2 ⟹ u3 ⟹ … … ⟹ uk ⟹ v
▸ A word is a string of terminals and the language of the grammar is { w ∈ 𝛴* | S =*> w }
▸ A derivation of a word w from a CFG G = ( V, 𝛴, R, S ) is a sequence of strings over V U 𝛴 ,
S = s0 => s1 => s2 => … … … => sk = w
where
- s0 = S is the start variable of G
- For each 1 <= i <= k,
si is obtained by activating a single production rule of G on one of the variables of si-1
vu
*

Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Derivation
5
Derivation – you can apply rules to any variables, no order is maintained. [single rules at a time]
For example, a + a x a
<EXPR>
⇒ <EXPR> + <TERM>
⇒ <TERM> + <TERM>
⇒ <TERM> + <TERM> x <FACTOR>
⇒ <TERM> + <TERM> x a
⇒ <FACTOR>+ <TERM> x a
⇒ a + <TERM> x a
⇒ a + <FACTOR> x a
⇒ a + a x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
CFG >> Leftmost Derivation
6
Leftmost Derivation – A derivation of a string w in a grammar G is a leftmost derivation
if at every step the leftmost remaining variable is the one replaced.
For example, a + a x a
<EXPR>
⇒ <EXPR> + <TERM>
⇒ <TERM> + <TERM>
⇒ <FACTOR> + <TERM>
⇒ a + <TERM>
⇒ a + <TERM> x <FACTOR>
⇒ a + <FACTOR> x <FACTOR>
⇒ a + a x <FACTOR>
⇒ a + a x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
CFG >> Rightmost Derivation
7
Rightmost Derivation – A derivation of a string w in a grammar G is a rightmost derivation
if at every step the rightmost remaining variable is the one replaced.
For example, a + a x a
<EXPR>
⇒ <EXPR> + <TERM>
⇒ <EXPR> + <TERM> x <FACTOR>
⇒ <EXPR> + <TERM> x a
⇒ <EXPR> + <FACTOR> x a
⇒ <EXPR> + a X a
⇒ <TERM> + a X a
⇒ <FACTOR> + a X a
⇒ a + a X a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
CFG >> Leftmost vs Rightmost Derivation
8
G = ( V, Σ, R, S )
Where,
V = { S }
Σ = { (, ) }
R={
S → SS | ( S ) | 𝜀
}
Leftmost derivation of “( ) ( )”: S ⇒ SS ⇒ ( S ) S ⇒ ( ) S ⇒ ( ) ( S ) ⇒ ( ) ( )
Rightmost derivation of “( ) ( )”: S => SS => S ( S ) => S ( ) => ( S ) ( ) => ( ) ( )
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Parse Tree
9
Root – must be labeled by the start variable
Leaves – labeled by a terminal or 𝜀
Interior nodes – labeled by a variable
Yield – the concatenation of the labels of the leaves in left to right order
For example, S → SS | ( S ) | ( )
yields ( ( ) ) ( )
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Example 1
10
▸ Let, CFG G = ( V, Σ, R, <EXPR> )
where V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
▸ Derive the following strings
- a + a x a
- ( a + a ) x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> “a+ a x a” Leftmost Derivation
11
<EXPR>
⇒ <EXPR> + <TERM>
⇒ <TERM> + <TERM>
⇒ <FACTOR> + <TERM>
⇒ a + <TERM>
⇒ a + <TERM> x <FACTOR>
⇒ a + <FACTOR> x <FACTOR>
⇒ a + a x <FACTOR>
⇒ a + a x a
So, <EXPR> derives a + a x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Practice the rightmost derivation.
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
CFG >> “a+ a x a” Parse Tree
12
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
Is there any leftmost/rightmost version of parse tree??
CFG >> “( a+ a ) x a” Leftmost Derivation
13
<EXPR>
⇒ <TERM>
⇒ <TERM> x <FACTOR>
⇒ <FACTOR> x <FACTOR>
⇒ ( <EXPR> ) x <FACTOR>
⇒ ( <EXPR> + <TERM> ) x <FACTOR>
⇒ ( <TERM> + <TERM> ) x <FACTOR>
⇒ ( <FACTOR> + <TERM> ) x <FACTOR>
⇒ ( a + <TERM> ) x <FACTOR>
⇒ ( a + <FACTOR> ) x <FACTOR>
⇒ ( a + a ) x <FACTOR>
⇒ ( a + a ) x a
So, <EXPR> derives ( a + a ) x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
CFG >> “( a+ a ) x a” Parse Tree
14
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
}
CFG >> Practice 1
15
▸ Let, CFG G = ( V, Σ, R, <EXPR> )
where V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, b, +, x, (, ) }
R={
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a | b
}
▸ Derive the following strings:
- ( a + b ) x a
- a + b x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Practice 2
16
▸ Let us consider a CFG with the following production rules,
<SENTENCE> → <NOUN-PHRASE> <VERB-PHRASE>
<NOUN-PHRASE> → <CMPLX-NOUN> | <CMPLX-NOUN> <PREP-PHRASE>
<VERB-PHRASE> → <CMPLX-VERB> | <CMPLX-VERB> <PREP-PHRASE>
<PREP-PHRASE> → <PREP> <CMPLX-NOUN>
<CMPLX-NOUN> → <ARTICLE> <NOUN>
<CMPLX-VERB> → <VERB> | <VERB> <NOUN-PHRASE>
<ARTICLE> → a | the
<NOUN> → boy | girl | flower
<VERB> → touches | likes | sees
<PREP> → with
▸ Now derive the following strings:
- a boy sees
- The boy sees a flower
- a girl with a flower likes the boy
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Ambiguity
17
▸ Grammar G is ambiguous if it generates some string ambiguously.
▸ A string w is derived ambiguously in context-free grammar G if it has two or more different leftmost derivations.
▸ A Context Free Grammar G = ( V, 𝛴, R, S ) is ambiguous if there is some string w ∈ 𝛴* such that there are two distinct parse
trees T1 and T2 having S at the root and having yield w.
[ Note: Two parse trees are equal if they are equal as trees and if all productions corresponding to inner nodes are also equal ]
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Ambiguity – Example 1
18
CFG G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR> }
Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <EXPR> | <EXPR> X <EXPR> | ( <EXPR> ) | a
}
Now derive the string : a + a x a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Ambiguity – a + a x a Derivation
19
CFG G = ( V, Σ, R, <EXPR> ) where, V = { <EXPR> } and Σ = { a, +, x, (, ) }
R={
<EXPR> → <EXPR> + <EXPR> | <EXPR> X <EXPR> | ( <EXPR> ) | a
}
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Parse Tree 1 Parse Tree 2
CFG >> Ambiguity – Practice 1
20
CFG G = ( V, Σ, R, <EXPR> )
Where,
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, b, +, x }
R={
<EXPR> → <EXPR> + <EXPR> | <EXPR> X <EXPR> | a | b
}
Does the grammar generate the string “a + b x a” ambiguously???
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Ambiguity – Practice 2
21
CFG G = ( V, Σ, R, S )
Where,
V = { S }
Σ = { (, ) }
R={
S → SS | ( S ) | ( )
}
Does the grammar generate the string “( )( )( )” ambiguously???
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Ambiguity – Practice 3
22
CFG G = ( V, Σ, R, S )
Where,
V = { S, A, B, C, D }
Σ = { 0, 1, 2 }
R={
S → AB | CD
A → 0A1 | 01
B → 2B | 2
C → 0C | 0
D → 1D2 | 12
}
Does the grammar generate the string “012” or, “001122” ambiguously???
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
a)
S → AB | C
A → aAb | ab
B → cBd | cd
C → aCd | aD
D → bDc | bc
b)
S → ABA
A → Aa | eps
B → bB | eps
CFG >> Ambiguity – Practice 4
23
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
▪ Can the string ‘aabbccdd’ be derived from the above CFG? If yes, show the
leftmost and rightmost derivation.
▪ Is the leftmost derivation unique? If not, show the other leftmost derivation.
▪ Is the grammar ambiguous? Justify your answer with a suitable example.
a)
E → E + T | T
T → T x F | F
F → (E) | a
b)
S → S + S | S * S | A | B
A → aA | 1
B → bB | 2
CFG >> Ambiguity – Practice 5
24
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Now give parse trees and elft-most derivations for each of the following:
▪ a + a
▪ a x a + a
▪ ((a))
▪ Show a leftmost derivation of the string: aa1 + bb2 * a1
▪ Show whether the string, bbb2 + aa1 + b2, makes the grammar ambiguous
a)
E → E + E | E – E | ( E ) | V
V → x | y | z | A
A → A * A | A % A | C
C → 0 | 1
b)
E → E + E | E * E | ( E ) | N
N → 0N | 1N | 0 | 1
CFG >> Ambiguity – Practice 6
25
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
▪ Find out if the following grammar is ambiguous or not, using the string
x+(0*1%0).
▪ If the grammar is ambiguous, show two different parse trees for this string.
▪ Show a leftmost derivation and rightmost derivation for the string:
( 1 + 10 ) * 1
a)
E → E + E | E * E | ( E ) | a | b | c
b)
S → WX | Y
W → aWb | ab
X → cXd | cd
Y → aYd | aZc
Z → aZb | bb
CFG >> Ambiguity – Practice 7
26
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
▪ Show the rightmost derivation and the leftmost derivation for the input string
a + ( b + c ) * c
▪ Determine whether the following CFG is ambiguous or not with the help of
following input string ‘aabbcd’
27
References:
Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser
THANKS!
Any questions?
You can find me at imam@cse.uiu.ac.bd

More Related Content

What's hot

TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
Mohammad Imam Hossain
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
Rushabh2428
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
Marina Santini
 
Fuzzy Set
Fuzzy SetFuzzy Set
Fuzzy Set
Ehsan Hamzei
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
MdImamHasan1
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
Mohammad Imam Hossain
 
Fuzzy logic-introduction
Fuzzy logic-introductionFuzzy logic-introduction
Fuzzy logic-introduction
WBUTTUTORIALS
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
Abhimanyu Mishra
 
Newtons Divided Difference Formulation
Newtons Divided Difference FormulationNewtons Divided Difference Formulation
Newtons Divided Difference Formulation
Sohaib H. Khan
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
Rabia Khalid
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
Samita Mukesh
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
Mukesh Tekwani
 
Fuzzy sets
Fuzzy sets Fuzzy sets
Fuzzy sets
ABSARQURESHI
 
Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1
Abhimanyu Mishra
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
Rajendran
 
Pda
PdaPda
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
Mukesh Tekwani
 
Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)
saithirumalg
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 

What's hot (20)

TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Fuzzy Set
Fuzzy SetFuzzy Set
Fuzzy Set
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
 
Fuzzy logic-introduction
Fuzzy logic-introductionFuzzy logic-introduction
Fuzzy logic-introduction
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
Newtons Divided Difference Formulation
Newtons Divided Difference FormulationNewtons Divided Difference Formulation
Newtons Divided Difference Formulation
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
combinatorics
combinatoricscombinatorics
combinatorics
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Fuzzy sets
Fuzzy sets Fuzzy sets
Fuzzy sets
 
Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Pda
PdaPda
Pda
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 

Similar to TOC 8 | Derivation, Parse Tree & Ambiguity Check

Using Cauchy Inequality to Find Function Extremums
Using Cauchy Inequality to Find Function ExtremumsUsing Cauchy Inequality to Find Function Extremums
Using Cauchy Inequality to Find Function Extremums
YogeshIJTSRD
 
Han Liu MedicReS World Congress 2015
Han Liu MedicReS World Congress 2015Han Liu MedicReS World Congress 2015
Han Liu MedicReS World Congress 2015
MedicReS
 
Delhi math sample paper set 1
Delhi math sample paper set 1Delhi math sample paper set 1
Delhi math sample paper set 1
Kartik Fulara
 
Fractional integration and fractional differentiation of the product of m ser...
Fractional integration and fractional differentiation of the product of m ser...Fractional integration and fractional differentiation of the product of m ser...
Fractional integration and fractional differentiation of the product of m ser...
Alexander Decker
 
On uniformly continuous uniform space
On uniformly continuous uniform spaceOn uniformly continuous uniform space
On uniformly continuous uniform space
theijes
 
AI 10 | Naive Bayes Classifier
AI 10 | Naive Bayes ClassifierAI 10 | Naive Bayes Classifier
AI 10 | Naive Bayes Classifier
Mohammad Imam Hossain
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
G2018ChoudhariNikita
 
Metrics for generativemodels
Metrics for generativemodelsMetrics for generativemodels
Metrics for generativemodels
Dai-Hai Nguyen
 
2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp
2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp
2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp
Pratima Nayak ,Kendriya Vidyalaya Sangathan
 
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear EquationsTenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
QUESTJOURNAL
 
Interpolation In Numerical Methods.
 Interpolation In Numerical Methods. Interpolation In Numerical Methods.
Interpolation In Numerical Methods.
Abu Kaisar
 
On the Construction of Cantor like Sets
On the Construction of Cantor like SetsOn the Construction of Cantor like Sets
On the Construction of Cantor like Sets
theijes
 
On the Construction of Cantor like Sets
	On the Construction of Cantor like Sets	On the Construction of Cantor like Sets
On the Construction of Cantor like Sets
theijes
 
Typesetting Mathematics with LaTeX - Day 2
Typesetting Mathematics with LaTeX - Day 2Typesetting Mathematics with LaTeX - Day 2
Typesetting Mathematics with LaTeX - Day 2
Suddhasheel GHOSH, PhD
 
Probability
ProbabilityProbability
Probability
KareemSalem23
 
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
IJMER
 
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ssusere0a682
 

Similar to TOC 8 | Derivation, Parse Tree & Ambiguity Check (17)

Using Cauchy Inequality to Find Function Extremums
Using Cauchy Inequality to Find Function ExtremumsUsing Cauchy Inequality to Find Function Extremums
Using Cauchy Inequality to Find Function Extremums
 
Han Liu MedicReS World Congress 2015
Han Liu MedicReS World Congress 2015Han Liu MedicReS World Congress 2015
Han Liu MedicReS World Congress 2015
 
Delhi math sample paper set 1
Delhi math sample paper set 1Delhi math sample paper set 1
Delhi math sample paper set 1
 
Fractional integration and fractional differentiation of the product of m ser...
Fractional integration and fractional differentiation of the product of m ser...Fractional integration and fractional differentiation of the product of m ser...
Fractional integration and fractional differentiation of the product of m ser...
 
On uniformly continuous uniform space
On uniformly continuous uniform spaceOn uniformly continuous uniform space
On uniformly continuous uniform space
 
AI 10 | Naive Bayes Classifier
AI 10 | Naive Bayes ClassifierAI 10 | Naive Bayes Classifier
AI 10 | Naive Bayes Classifier
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
 
Metrics for generativemodels
Metrics for generativemodelsMetrics for generativemodels
Metrics for generativemodels
 
2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp
2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp
2015 12 lyp_mathematics_trivandrum_chennai_set1_outside_qp
 
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear EquationsTenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
 
Interpolation In Numerical Methods.
 Interpolation In Numerical Methods. Interpolation In Numerical Methods.
Interpolation In Numerical Methods.
 
On the Construction of Cantor like Sets
On the Construction of Cantor like SetsOn the Construction of Cantor like Sets
On the Construction of Cantor like Sets
 
On the Construction of Cantor like Sets
	On the Construction of Cantor like Sets	On the Construction of Cantor like Sets
On the Construction of Cantor like Sets
 
Typesetting Mathematics with LaTeX - Day 2
Typesetting Mathematics with LaTeX - Day 2Typesetting Mathematics with LaTeX - Day 2
Typesetting Mathematics with LaTeX - Day 2
 
Probability
ProbabilityProbability
Probability
 
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
 
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
ゲーム理論BASIC 第42回 -仁に関する定理の証明2-
 

More from Mohammad Imam Hossain

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
Mohammad Imam Hossain
 
TOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of ComputationTOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of Computation
Mohammad Imam Hossain
 
DBMS 9 | Extendible Hashing
DBMS 9 | Extendible HashingDBMS 9 | Extendible Hashing
DBMS 9 | Extendible Hashing
Mohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
 
TOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of ComputationTOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of Computation
 
DBMS 9 | Extendible Hashing
DBMS 9 | Extendible HashingDBMS 9 | Extendible Hashing
DBMS 9 | Extendible Hashing
 

Recently uploaded

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 

TOC 8 | Derivation, Parse Tree & Ambiguity Check

  • 1. Theory of Computation CSE 2233 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 2. CFG – Context-free Grammar 2 A Context-free Grammar is a 4 tuple (V, 𝛴, R, S), where ▸ V is a finite set called the variables ▸ Σ is a finite set, disjoint from V, called the terminals ▸ R is a finite set of rules, with each rule being a variable and a string of variables and terminals ▸ S ϵ V is the start variable Example: Grammar, G1 = ( { S }, { a, b }, R, S ) V = { S } Σ = { a, b } R is, S → aSb | SS | ε Example: Grammar, G2 = ( V, Σ, R, <EXPR> ) V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R is, <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 3. CFL – Context-free Language 3 Context-free Language: – The collection of languages generated by some context-free grammars are called Context-free Languages. – They include all the regular languages and many additional(recursive structure) languages. Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 4. CFG >> Terminologies – yields, derives, derivation 4 ▸ If u, v, w are strings of variables and terminals, and A → w is a rule of the grammar, we say that uAv ⟹ uwv i.e. uAv yields uwv ▸ We say u derives v, written as - if u = v or - if a sequence u1, u2, u3, … … … , uk exists for k >= 0 and u ⟹ u1 ⟹ u2 ⟹ u3 ⟹ … … ⟹ uk ⟹ v ▸ A word is a string of terminals and the language of the grammar is { w ∈ 𝛴* | S =*> w } ▸ A derivation of a word w from a CFG G = ( V, 𝛴, R, S ) is a sequence of strings over V U 𝛴 , S = s0 => s1 => s2 => … … … => sk = w where - s0 = S is the start variable of G - For each 1 <= i <= k, si is obtained by activating a single production rule of G on one of the variables of si-1 vu *  Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 5. CFG >> Derivation 5 Derivation – you can apply rules to any variables, no order is maintained. [single rules at a time] For example, a + a x a <EXPR> ⇒ <EXPR> + <TERM> ⇒ <TERM> + <TERM> ⇒ <TERM> + <TERM> x <FACTOR> ⇒ <TERM> + <TERM> x a ⇒ <FACTOR>+ <TERM> x a ⇒ a + <TERM> x a ⇒ a + <FACTOR> x a ⇒ a + a x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a }
  • 6. CFG >> Leftmost Derivation 6 Leftmost Derivation – A derivation of a string w in a grammar G is a leftmost derivation if at every step the leftmost remaining variable is the one replaced. For example, a + a x a <EXPR> ⇒ <EXPR> + <TERM> ⇒ <TERM> + <TERM> ⇒ <FACTOR> + <TERM> ⇒ a + <TERM> ⇒ a + <TERM> x <FACTOR> ⇒ a + <FACTOR> x <FACTOR> ⇒ a + a x <FACTOR> ⇒ a + a x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a }
  • 7. CFG >> Rightmost Derivation 7 Rightmost Derivation – A derivation of a string w in a grammar G is a rightmost derivation if at every step the rightmost remaining variable is the one replaced. For example, a + a x a <EXPR> ⇒ <EXPR> + <TERM> ⇒ <EXPR> + <TERM> x <FACTOR> ⇒ <EXPR> + <TERM> x a ⇒ <EXPR> + <FACTOR> x a ⇒ <EXPR> + a X a ⇒ <TERM> + a X a ⇒ <FACTOR> + a X a ⇒ a + a X a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a }
  • 8. CFG >> Leftmost vs Rightmost Derivation 8 G = ( V, Σ, R, S ) Where, V = { S } Σ = { (, ) } R={ S → SS | ( S ) | 𝜀 } Leftmost derivation of “( ) ( )”: S ⇒ SS ⇒ ( S ) S ⇒ ( ) S ⇒ ( ) ( S ) ⇒ ( ) ( ) Rightmost derivation of “( ) ( )”: S => SS => S ( S ) => S ( ) => ( S ) ( ) => ( ) ( ) Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 9. CFG >> Parse Tree 9 Root – must be labeled by the start variable Leaves – labeled by a terminal or 𝜀 Interior nodes – labeled by a variable Yield – the concatenation of the labels of the leaves in left to right order For example, S → SS | ( S ) | ( ) yields ( ( ) ) ( ) Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 10. CFG >> Example 1 10 ▸ Let, CFG G = ( V, Σ, R, <EXPR> ) where V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a } ▸ Derive the following strings - a + a x a - ( a + a ) x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 11. CFG >> “a+ a x a” Leftmost Derivation 11 <EXPR> ⇒ <EXPR> + <TERM> ⇒ <TERM> + <TERM> ⇒ <FACTOR> + <TERM> ⇒ a + <TERM> ⇒ a + <TERM> x <FACTOR> ⇒ a + <FACTOR> x <FACTOR> ⇒ a + a x <FACTOR> ⇒ a + a x a So, <EXPR> derives a + a x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Practice the rightmost derivation. G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a }
  • 12. CFG >> “a+ a x a” Parse Tree 12 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a } Is there any leftmost/rightmost version of parse tree??
  • 13. CFG >> “( a+ a ) x a” Leftmost Derivation 13 <EXPR> ⇒ <TERM> ⇒ <TERM> x <FACTOR> ⇒ <FACTOR> x <FACTOR> ⇒ ( <EXPR> ) x <FACTOR> ⇒ ( <EXPR> + <TERM> ) x <FACTOR> ⇒ ( <TERM> + <TERM> ) x <FACTOR> ⇒ ( <FACTOR> + <TERM> ) x <FACTOR> ⇒ ( a + <TERM> ) x <FACTOR> ⇒ ( a + <FACTOR> ) x <FACTOR> ⇒ ( a + a ) x <FACTOR> ⇒ ( a + a ) x a So, <EXPR> derives ( a + a ) x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a }
  • 14. CFG >> “( a+ a ) x a” Parse Tree 14 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a }
  • 15. CFG >> Practice 1 15 ▸ Let, CFG G = ( V, Σ, R, <EXPR> ) where V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, b, +, x, (, ) } R={ <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a | b } ▸ Derive the following strings: - ( a + b ) x a - a + b x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 16. CFG >> Practice 2 16 ▸ Let us consider a CFG with the following production rules, <SENTENCE> → <NOUN-PHRASE> <VERB-PHRASE> <NOUN-PHRASE> → <CMPLX-NOUN> | <CMPLX-NOUN> <PREP-PHRASE> <VERB-PHRASE> → <CMPLX-VERB> | <CMPLX-VERB> <PREP-PHRASE> <PREP-PHRASE> → <PREP> <CMPLX-NOUN> <CMPLX-NOUN> → <ARTICLE> <NOUN> <CMPLX-VERB> → <VERB> | <VERB> <NOUN-PHRASE> <ARTICLE> → a | the <NOUN> → boy | girl | flower <VERB> → touches | likes | sees <PREP> → with ▸ Now derive the following strings: - a boy sees - The boy sees a flower - a girl with a flower likes the boy Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 17. CFG >> Ambiguity 17 ▸ Grammar G is ambiguous if it generates some string ambiguously. ▸ A string w is derived ambiguously in context-free grammar G if it has two or more different leftmost derivations. ▸ A Context Free Grammar G = ( V, 𝛴, R, S ) is ambiguous if there is some string w ∈ 𝛴* such that there are two distinct parse trees T1 and T2 having S at the root and having yield w. [ Note: Two parse trees are equal if they are equal as trees and if all productions corresponding to inner nodes are also equal ] Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 18. CFG >> Ambiguity – Example 1 18 CFG G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR> } Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <EXPR> | <EXPR> X <EXPR> | ( <EXPR> ) | a } Now derive the string : a + a x a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 19. CFG >> Ambiguity – a + a x a Derivation 19 CFG G = ( V, Σ, R, <EXPR> ) where, V = { <EXPR> } and Σ = { a, +, x, (, ) } R={ <EXPR> → <EXPR> + <EXPR> | <EXPR> X <EXPR> | ( <EXPR> ) | a } Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Parse Tree 1 Parse Tree 2
  • 20. CFG >> Ambiguity – Practice 1 20 CFG G = ( V, Σ, R, <EXPR> ) Where, V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, b, +, x } R={ <EXPR> → <EXPR> + <EXPR> | <EXPR> X <EXPR> | a | b } Does the grammar generate the string “a + b x a” ambiguously??? Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 21. CFG >> Ambiguity – Practice 2 21 CFG G = ( V, Σ, R, S ) Where, V = { S } Σ = { (, ) } R={ S → SS | ( S ) | ( ) } Does the grammar generate the string “( )( )( )” ambiguously??? Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 22. CFG >> Ambiguity – Practice 3 22 CFG G = ( V, Σ, R, S ) Where, V = { S, A, B, C, D } Σ = { 0, 1, 2 } R={ S → AB | CD A → 0A1 | 01 B → 2B | 2 C → 0C | 0 D → 1D2 | 12 } Does the grammar generate the string “012” or, “001122” ambiguously??? Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 23. a) S → AB | C A → aAb | ab B → cBd | cd C → aCd | aD D → bDc | bc b) S → ABA A → Aa | eps B → bB | eps CFG >> Ambiguity – Practice 4 23 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU ▪ Can the string ‘aabbccdd’ be derived from the above CFG? If yes, show the leftmost and rightmost derivation. ▪ Is the leftmost derivation unique? If not, show the other leftmost derivation. ▪ Is the grammar ambiguous? Justify your answer with a suitable example.
  • 24. a) E → E + T | T T → T x F | F F → (E) | a b) S → S + S | S * S | A | B A → aA | 1 B → bB | 2 CFG >> Ambiguity – Practice 5 24 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Now give parse trees and elft-most derivations for each of the following: ▪ a + a ▪ a x a + a ▪ ((a)) ▪ Show a leftmost derivation of the string: aa1 + bb2 * a1 ▪ Show whether the string, bbb2 + aa1 + b2, makes the grammar ambiguous
  • 25. a) E → E + E | E – E | ( E ) | V V → x | y | z | A A → A * A | A % A | C C → 0 | 1 b) E → E + E | E * E | ( E ) | N N → 0N | 1N | 0 | 1 CFG >> Ambiguity – Practice 6 25 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU ▪ Find out if the following grammar is ambiguous or not, using the string x+(0*1%0). ▪ If the grammar is ambiguous, show two different parse trees for this string. ▪ Show a leftmost derivation and rightmost derivation for the string: ( 1 + 10 ) * 1
  • 26. a) E → E + E | E * E | ( E ) | a | b | c b) S → WX | Y W → aWb | ab X → cXd | cd Y → aYd | aZc Z → aZb | bb CFG >> Ambiguity – Practice 7 26 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU ▪ Show the rightmost derivation and the leftmost derivation for the input string a + ( b + c ) * c ▪ Determine whether the following CFG is ambiguous or not with the help of following input string ‘aabbcd’
  • 27. 27 References: Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser THANKS! Any questions? You can find me at imam@cse.uiu.ac.bd