Unit 3 (Part I)
Unit 3 (Part I)
Normal Forms: Chomsky Normal Forms (CNF) and Greibach Normal Forms
TCS : UNIT 3 1
GRAMMAR
❖ It is a finite set of formal rules for generating syntactically correct sentences or meaningful correct sentences.
❖ Grammar is a standard way of representing a Language
❖ A Grammar ‘G’ is defined as quadruple ( or represented by 4 elements or tuples):
G={V,T,P,S}
➢ V : Variable represented by Capital Letter ( or finite set of non Terminal Symbols)
➢ T : Terminal used to terminate string & represented by Small Letter( or finite set of Terminal Symbols)
➢ P: Production Rule ( Finite Non-Empty Set of Production Rules)
➢ S : Start Symbol (Symbol from where we start producing our sentences or strings).
TCS : UNIT 3 2
GRAMMAR
S ➔ aSb /Є
Є , ab , a2b2, a3b3 , ……… an bn …….n≥ 0
‘abab’ can’t be generated
TCS : UNIT 3 3
REGULAR GRAMMAR
TCS : UNIT 3 4
CHOMSKY HIERARCHY
TCS : UNIT 3 5
Type 0: Unrestricted Grammar:
➢ Type-0 grammars include all formal grammar.
➢ Type 0 grammar languages are recognized by Turing Machine.
➢ These languages are also known as the Recursively Enumerable languages.
➢ Grammar Production in the form of α ➔ β
where α \alpha is ( V + T)* V ( V + T)*
V : Variables T : Terminals.
β is ( V + T )*.
➢ In type 0 there must be at least one variable on the Left side of production.
For example: *A language is called
Sab ➔ ba Recursively Enumerable if
there is a Turing Machine
A➔ S that accepts on any input
within the language.
Here, Variables are S, A, and Terminals a, b.
TCS : UNIT 3 6
Type 1: Context-Sensitive Grammar:
➢ Type-1 grammars generate context-sensitive languages.
➢ The languages generated by the grammar is recognized by the Linear Bound
Automata.
➢ In Type 1:
➔ The Left side of production can have only one variable and there is no
➢ For Example: S ➔ AB
A➔ a
B➔b
TCS : UNIT 3 8
Type 3: Regular Grammar:
➢ Type-3 grammars generate Regular languages.
➢ The languages generated by the grammar is accepted by the Finite-state Automata
➢ In Type 3 is the most restricted form of grammar.
➢ Type 3 should be in the given form only :
V ➔ VT / T (left-regular grammar) (or)
V ➔ TV /T (right-regular grammar)
For example:
S ⇢ aB S ⇢ Ba
S⇢a S⇢Є
TCS : UNIT 3 9
REGULAR GRAMMAR
Regular grammar generates regular language. They have a single non-terminal on the left-hand side
and a right-hand side consisting of a single terminal or single terminal followed by a non-terminal or
single non-terminal followed by a terminal.
The productions must be in the form:
A ⇢ xB
A⇢x
A⇢Є
A ⇢ Bx
where A, B ∈ Variable(V) and x∈T i.e. string of terminals.
TCS : UNIT 3 10
1. Right linear grammar(RLG):
In RLG, the productions are in the form
A ⇢ xB
A ⇢ x
A⇢ Є
where A,B ∈ V and x ∈ T
TCS : UNIT 3 11
DFA TO RIGHT LINEAR REGULAR GRAMMAR
TCS : UNIT 3 12
DFA TO RIGHT LINEAR REGULAR GRAMMAR
TCS : UNIT 3 13
DFA TO RIGHT LINEAR REGULAR GRAMMAR
TCS : UNIT 3 14
DFA TO RIGHT LINEAR REGULAR GRAMMAR
Soln:
Example: Give Right Linear Grammar for the DFA
shown below
ASSIGNMENT 3: Q1
TCS : UNIT 3 15
DFA TO LEFT LINEAR REGULAR GRAMMAR
STEPS:
1. Interchange starting state and the final state
2. Reverse the direction of all the transitions
3. Write the grammar from transition graph in Left Linear form
Example: Give Left Linear Grammar for the DFA shown below
Soln:
TCS : UNIT 3 16
TCS : UNIT 3 17
RIGHT LINEAR REGULAR GRAMMAR TO DFA
TCS : UNIT 3 18
Example: Convert the given Right Linear
Soln:
TCS : UNIT 3 19
Convert FA to DFA
TCS : UNIT 3 20
LEFT LINEAR REGULAR GRAMMAR TO DFA
STEPS:
1. Draw transition graph from given LLG
2. Reverse the direction of all the transitions
3. Interchange starting state and the final state
4. Perform conversion from FA to DFA
TCS : UNIT 3 21
LEFT LINEAR REGULAR GRAMMAR TO DFA
ASSIGNMENT 3: Q2
TCS : UNIT 3 22
4. Convert FA to DFA
TCS : UNIT 3 23
RIGHT LINEAR REGULAR GRAMMAR TO LEFT LINEAR REGULAR GRAMMAR
TCS : UNIT 3 24
Example: Convert the given Right Linear
TCS : UNIT 3 25
2. Interchange starting state and the
final state and Reverse the directions
of transitions
TCS : UNIT 3 26
LEFT LINEAR REGULAR GRAMMAR TO RIGHT LINEAR REGULAR GRAMMAR
TCS : UNIT 3 27
CONTEXT FREE GRAMMARS (CFG)
❖ A Context Free Grammar ‘CFG’ is defined as quadruple ( or
➔ CFG is Type 2 grammar
represented by 4 elements or tuples):
➔ Grammar Production in the form of α ➔ β
G={V,T,P,S}
➢ V : Finite set of non Terminal Symbols / Variables ➔ The Left side of production can have only one
➢ T : Finite set of Terminal Symbols
variable and there is no restriction on β .
➢ P: Finite Non-Empty Set of Production Rules
i.e. | α \alpha | = 1 and α Є V
➢ S : Start Symbol (it is a special Variable where S Є V )
RG CFG
➢ A context-free grammar is a set of recursive rules used to A ⇢ xB A ⇢ xBx
A⇢x A ⇢ xabab
generate patterns of strings. A⇢Є A⇢Є
➢ A context-free grammar can describe all regular languages A ⇢ Bx A ⇢ BB
TCS : UNIT 3 29
RIGHTMOST DERIVATION
TCS : UNIT 3 35
ASSIGNMENT 3: Q4
TCS : UNIT 3 36
PARSE TREE
TCS : UNIT 3 37
AMBIGUOUS GRAMMARS
Depending on the Number of Derivation trees, CFGs are sub-divided into 2 types:
•Ambiguous grammars
•Unambiguous grammars
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 Leftmost Derivation Tree
(LMDT) or Rightmost Derivation Tree (RMDT).
Definition: G = (V,T,P,S) is a CFG that is said to be ambiguous if and only if there exists a
string in T* that has more than one parse tree.
where V is a finite set of variables.
T is a finite set of terminals.
P is a finite set of productions of the form, A -> α, where A is a variable and α ∈ (V ∪ T)*
S is a designated variable called the start symbol.
TCS : UNIT 3 38
Example: Let us consider this grammar: E -> E E| i d create a parse tree from this grammar to obtain
a string i d i d i d The following are the 2 parse trees generated by left-most derivation:
Both the above parse trees are derived from the same grammar rules but both parse
trees are different. Hence the grammar is Ambiguous.
TCS : UNIT 3 39
For Ex : Given Grammar G : S-> a B / B a
Prove the grammar given below is
A-> a / a S/ B A A ambiguous for given input strings
B-> b / bS/ a B B
For the input strings: 1. aababb 2. aaabbabbba
Soln: PART 1: First Derivation : ( Top-Down, Leftmost Derivation)
S -> a B {Substitute B→ a B B}
S-> a a B B {Substitute B→ b }
S-> a a b B {Substitute B→ a B B }
S-> a a b a B B {Substitute B→ b }
S-> a a b a b B {Substitute B→ b }
S-> a a b a b b
TCS : UNIT 3 40
For Ex : The Grammar is : S-> a B / B a A-> a / a S/ B A A B-> b / bS/ a B B
1. aababb 2. aaabbabbba
Soln: PART 1: Second Derivation : ( Top-Down, Leftmost Derivation)
S -> a B {Substitute B→ a B B}
S-> a a B B {Substitute B→ b S }
S-> a a b S B {Substitute S→ a B }
S-> a a b a B B {Substitute B→ b }
S-> a a b a b B {Substitute B→ b }
S-> a a b a b b
TCS : UNIT 3 41
For Ex : The Grammar is : S-> a B / B a A-> a / a S/ B A A B-> b / bS/ a B B
1. aababb 2. aaabbabbba
Soln: PART 1: Derivation : ( Top-Down, Rightmost Derivation)
S -> a B {Substitute B→ a B B}
S-> a a B B {Substitute B→ a B B }
S-> a a B a B B {Substitute B→ b }
S-> a a B a B b {Substitute B→ b }
S-> a a B a b b {Substitute B→ b }
S-> a a b a b b
TCS : UNIT 3 42
For Ex : The Grammar is : S-> a B / B a A-> a / a S/ B A A B-> b / bS/ a B B
2. aaabbabbba
ASSIGNMENT 3: Q5
Soln: PART 2: Derivation : ( Top-Down, Leftmost Derivation) Prove the grammar given
S -> B a {Substitute B→ a B B} below is ambiguous for given
S-> a B B a {Substitute B→ a B B } input strings
S-> a a B B B a {Substitute B→ a B B }
S-> a a a B B B B a {Substitute B→ b }
S-> a a a b B B B a {Substitute B→ b }
S-> a a a b b B B a {Substitute B→ a B B }
S-> a a a b b a B B B a {Substitute B→ b }
S-> a a a b b a b B B a {Substitute B→ b}
S-> a a a b b a b b B a {Substitute B→ b}
S-> a a a b b a b b b a
TCS : UNIT 3 43
For Ex : The Grammar is : S-> a B / B a A-> a / a S/ B A A B-> b / bS/ a B B
2. aaabbabbba
Soln: PART 2: Derivation : ( Top-Down, Leftmost Derivation)
S -> a B {Substitute B→ a B B}
S-> a a B B {Substitute B→ a B B }
S-> a a a B B B {Substitute B→ b }
S-> a a a b B B {Substitute B→ b }
S-> a a a b b B {Substitute B→ a B B }
S-> a a a b b a B B {Substitute B→ b }
S-> a a a b b a b B {Substitute B→ b S}
S-> a a a b b a b b S {Substitute S→ B a}
S-> a a a b b a b b B a {Substitute B→ b}
S-> a a a b b a b b b a
Sketch Parse Tree for both Leftmost derivations. As there are two parse tree for LMD, it is proved that given grammar is
ambiguous TCS : UNIT 3 44
For Ex : The Grammar is : S-> a B / B a A-> a / a S/ B A A B-> b / bS/ a B B
2. aaabbabbba
Soln: PART 2: Derivation : ( Top-Down, Rightmost Derivation)
S -> a B {Substitute B→ a B B}
S-> a a B B {Substitute B→ a B B }
S-> a a B a B B {Substitute B→ b S }
S-> a a B a B b S {Substitute S→ B a }
S-> a a B a B b B a {Substitute B→ b }
S-> a a B a b b b a {Substitute B→ a B B }
S-> a a a B B a b b b a {Substitute B→ b}
S-> a a a B b a b b b a {Substitute B→ b }
S-> a a a b b a b b b a
S-> a a a b b a b b b a
TCS : UNIT 3 45