0% found this document useful (0 votes)
13 views

Unit 3 (Part I)

Uploaded by

ioterottishruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit 3 (Part I)

Uploaded by

ioterottishruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

UNIT 3 : GRAMMARS

❑ Grammars and Chomsky hierarchy

❑ Regular Grammar (RG), Equivalence of Left and Right linear grammar,

Equivalence of RG and FA.

❑ Context Free Grammars (CFG): Definition, Sentential forms, Leftmost and

Rightmost derivations, Parse tree, Ambiguity, Simplification and Applications,

Normal Forms: Chomsky Normal Forms (CNF) and Greibach Normal Forms

(GNF), Context Free language (CFL) - Pumping lemma, Closure properties.

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

According to Chomsky hierarchy,


grammar is divided into 4 types as
follows:
1.Type 0 is known as unrestricted
grammar.
2.Type 1 is known as context-
sensitive grammar.
3.Type 2 is known as a context-free
grammar.
4.Type 3 Regular Grammar.

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:

➔ First of all Type 1 grammar should be Type 0.

➔ Grammar Production in the form of α ➔ β

| α \alpha |<=| β \beta | *A linear bounded automaton


is called a multi-track non-
deterministic Turing
That is the count of symbol in α is less than or equal to β machine which has a tape of
some bounded finite length..
➔ Also β ∈ (V + T)+ i.e. β can not be ε
➢ For Example:
S --> AB AB --> abc B --> b
TCS : UNIT 3 7
Type 2: Context-Free Grammar:
➢ Type-2 grammars generate context-free languages.
➢ The languages generated by the grammar is recognized by the Pushdown Automata
➢ In Type 2:

➔ First of all Type 2 grammar should be Type 1.

➔ Grammar Production in the form of α ➔ β

➔ The Left side of production can have only one variable and there is no

restriction on β . i.e. | α \alpha | = 1

➢ 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⇢Є

The above form is called strictly regular grammar.

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.

Types of Regular Grammar:


•Right linear grammar(RLG)
•Left Linear grammar(LLG)

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

2. Left linear grammar(LLG):


In LLG, the productions are in the form
A ⇢ Bx
A ⇢ x
A⇢ Є (ba)*
where A,B ∈ V and x ∈ T

TCS : UNIT 3 11
DFA TO RIGHT LINEAR REGULAR GRAMMAR

RLG: B ⇢ aC here C is not Final State

RLG: B ⇢ aC here C is Final State


So B⇢a
Also B ⇢ aC / a

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

RLG: B ⇢ aC here C is not Final State

B ⇢ aC / a here C is Final State

A⇢b b is going to some Final State

A⇢Є it will make A to be final state

TCS : UNIT 3 18
Example: Convert the given Right Linear

Grammar into an equivalent DFA

Soln:

Prepare Transition table


for obtained NFA

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

Example: Construct DFA accepting regular language


generated by the given Left Linear Grammar

Soln: 1. Draw Transition Graph for given LLG

2. Reverse the directions of transitions


3. Interchange starting state and the final state

Prepare Transition table


for obtained NFA

TCS : UNIT 3 22
4. Convert FA to DFA

TCS : UNIT 3 23
RIGHT LINEAR REGULAR GRAMMAR TO LEFT LINEAR REGULAR GRAMMAR

RIGHT LINEAR REGULAR GRAMMAR STEPS:


1. Represent the RLG using transition graph and mark
the final state as
TRANSITION GRAPH
2. Interchange starting state and the final state
3. Reverse the direction of all the transitions
LEFT LINEAR REGULAR GRAMMAR 4. Write the LLG from transition graph

TCS : UNIT 3 24
Example: Convert the given Right Linear

Grammar into an equivalent Left Linear Grammar

Soln: 1. Draw Transition Graph for given RLG

2. Interchange starting state and the


final state and Reverse the directions
of transitions

TCS : UNIT 3 25
2. Interchange starting state and the
final state and Reverse the directions
of transitions

3. Write the LLG from transition graph in Left Linear form

TCS : UNIT 3 26
LEFT LINEAR REGULAR GRAMMAR TO RIGHT LINEAR REGULAR GRAMMAR

LEFT LINEAR REGULAR GRAMMAR STEPS:


1. Represent the LLG using transition graph and mark
the final state as
TRANSITION GRAPH
2. Interchange starting state and the final state
3. Reverse the direction of all the transitions
RIGHT LINEAR REGULAR GRAMMAR 4. Write the RLG from transition graph

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

and more, but they cannot describe all possible languages.


TCS : UNIT 3 28
SENTENTIAL FORM

➢ Sentential form of a grammar G can be defined to be any sequence of grammar


symbols (terminals or non terminals) derived in 0 or more steps from the start
symbol of G .
➢ A sentence is a sentential-form which contains only terminal symbols.
➢ A derivation of a string for a grammar is a sequence of grammar rule applications
that transform the start symbol into the string.
➢ A derivation proves that the string belongs to the grammar's language.
➢ A derivation is fully determined by giving, for each step: the rule applied in that step.
➢ A String can be derived in many ways. But we will study only following two ways
✓ Rightmost Derivation
✓ Leftmost Derivation

TCS : UNIT 3 29
RIGHTMOST DERIVATION

For Ex : Grammar is : S-> a A c B e


A->A b|b
B->d
Input string : abbcde
Soln:
Derivation : ( Top-Down, Rightmost Derivation)
S ->a A c B e { Substitute B→ d}
S->a A c d e {Substitute A→ Ab }
S->a A b c d e {Substitute A→ b }
S->a b b c d e
Input string a b b c d e is part of the language produced by given Grammar
TCS : UNIT 3 30
LEFTMOST DERIVATION

For Ex : Grammar is : S-> a A c B e


A->A b|b
B->d
Input string : abbcde
Soln:
Derivation : ( Top-Down, Leftmost Derivation)
S ->a A c B e {Substitute A→ Ab }
S->a A b c B e {Substitute A→ b }
S->a b b c B e { Substitute B→ d}
S->a b b c d e
Input string a b b c d e is part of the language produced by given Grammar
TCS : UNIT 3 31
LEFTMOST DERIVATION
For Ex : Grammar is : S-> A 1 B
Give Leftmost & Rightmost derivation of the string
A-> 0 A / Є
1001 for the grammar below
B-> 0 B / 1 B / Є
Input string : 1001
Soln:
Derivation : ( Top-Down, Leftmost Derivation)
S -> A 1 B {Substitute A→ Є }
S-> 1 B {Substitute B→ 0B }
S-> 1 0 B {Substitute B→ 0B }
S-> 1 0 0 B {Substitute B→ 1B }
S-> 1 0 0 1 B {Substitute B→ Є }
S-> 1 0 0 1
Input string 1 0 0 1 is part of the language produced by given Grammar
TCS : UNIT 3 32
RIGHTMOST DERIVATION
For Ex : Grammar is : S-> A 1 B
Give Leftmost & Rightmost derivation of the string
A-> 0 A / Є
1001 for the grammar below
B-> 0 B / 1 B / Є
Input string : 1001
Soln:
Derivation : ( Top-Down, Rightmost Derivation)
S -> A 1 B {Substitute B→ 0B }
S-> A 1 0 B {Substitute B→ 0B }
S-> A 1 0 0 B {Substitute B→ 1B }
S-> A 1 0 0 1 B {Substitute B→ Є }
S-> A 1 0 0 1 Є {Substitute A→ Є }
S-> Є 1 0 0 1 ➔ S-> 1 0 0 1
Input string 1 0 0 1 is part of the language produced by given Grammar
TCS : UNIT 3 33
For Ex : Obtain Leftmost derivation, Rightmost derivation and derivation tree for the string
“c c c b a c c b a”. The Grammar is :
ASSIGNMENT 3: Q3
S-> S S a / S S b / c
Soln: PART 1: Derivation : ( Top-Down, Leftmost Derivation)
S -> S S a {Substitute S→ S S a }
S-> S S a S a {Substitute S→ c }
S-> c S a S a {Substitute S→ S S b }
S-> c S S b a S a {Substitute S→ c }
S-> c c S b a S a {Substitute S→ c }
S-> c c c b a S a {Substitute S→ S S b }
S-> c c c b a S S b a {Substitute S→ c}
S-> c c c b a c S b a {Substitute S→ c}
S-> c c c b a c c b a
S-> c c c b a c c b a
TCS : UNIT 3 34
PART 2: Derivation : ( Top-Down, Rightmost Derivation)
Grammar: S-> S S a / S S b / c String: “c c c b a c c b a”
S -> S S a {Substitute S→ S S b }
S-> S S S b a {Substitute S→ c }
S-> S S c b a {Substitute S→ c}
S-> S c c b a {Substitute S→ S S a}
S-> S S a c c b a {Substitute S→ S S b }
S-> S S S b a c c b a {Substitute S→ c}
S-> S S c b a c c b a {Substitute S→ c}
S-> S c c b a c c b a {Substitute S→ c}
S-> c c c b a c c b a
S-> c c c b a c c b a

TCS : UNIT 3 35
ASSIGNMENT 3: Q4

Draw Parse Tree for both Leftmost & Rightmost Derivation of Q3

TCS : UNIT 3 36
PARSE TREE

❑ A set of derivations applied to generate a word/string can be represented using a tree


known as Parse Tree
❑ A Parse Tree representation gives us better understanding of Recursion & Grouping of
Symbols
❑ A Parse Tree is constructed with following condition:
➢ Root of tree is represented by Start Symbol
➢ Each Interior Node is represented by a variable belonging to V
➢ Each Leaf Node is represented by a terminal or Є
➢ A String generated by Parse tree is seen from Left to Right

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

Sketch Parse Tree for both Leftmost derivations.

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

You might also like