2. Topics to be covered
Chomsky Hierarchy in Grammar
Finite Automata to Grammar
Language and Grammar
related problems.
Ambiguous Grammar and
Parsing Tree
Annex: Table Filling Algorithm
Day 6
3. The Chomsky Hierarchy
● Type 0 known as Unrestricted Grammar.
● Type 1 known as Context Sensitive Grammar.
● Type 2 known as Context Free Grammar.
● Type 3 known as Regular Grammar.
According to Chomsky there exist 4 types of grammars.
5. The Chomsky Hierarchy
● Type-0 grammars include all formal grammars.
● Type 0 grammar language are recognized by
turing machine.
● These languages are also known as the
Recursively Enumerable languages.
Type 0: Unrestricted Grammar:
6. The Chomsky Hierarchy
In type 0:
Grammar Production in the form of α ---> β where
α is ( V + T)* V ( V + T)* { V : Variables, T : Terminals}
β is ( V + T )*
In type 0 there must be at least one variable on Left side of
production. For example,
Sab –> ba
A –> S.
Here, Variables are S, A and Terminals a, b.
7. The Chomsky Hierarchy
● Type-1 grammars generate the context-sensitive
languages.
● The language generated by the grammar are
recognized by the Linear Bound Automata
● Type 1 grammar should be of Type 0.
Type 1: Context Sensitive Grammar
8. The Chomsky Hierarchy
In type 1:
Grammar Production in the form of
α ---> β
|α| <= |β|
Where α,β ∈ (VUT)*; It is a ε free grammar
For Example,
S –> AB
AB –> abc
B –> b
9. The Chomsky Hierarchy
● Type-2 grammars generate the context-free
languages.
● The language generated by the grammar is
recognized by a Pushdown automata.
● The grammar should be Type 1.
Type 2: Context Free Grammar:
10. The Chomsky Hierarchy
In Type 2:
Left hand side of production can have only one variable.
|α| = 1.
There is no restriction on β.
For example,
S –> AB
A –> a
B –> b
11. The Chomsky Hierarchy
● Type-3 grammars generate regular languages.
● These languages are exactly all languages that
can be accepted by a finite state automaton.
● Type 3 is most restricted form of grammar.
Type 3: Regular Grammar:
12. The Chomsky Hierarchy
In Type 3:
Type 3 should be in the given form only :
V –> VT* / T*.
(or)
V –> T*V /T*
i.e. Grammar is regular only if it is right linear or left linear.
For example,
S –> aB or S –> Ba
S –> a S –> a
13. Grammar from FA
We can convert from any Finite automata to a grammar using just
these 2 steps.
Step 1: if 𝛿(qi
,0) = qj
, then introduce transition
qi
---> 0qj
Step 2: if q ∈ F, then introduce transition
q ---> ε
31. Grammar Problems
Q. Obtain grammar for string containing not more than 3 a’s
S ---> bS | aA | ε
A ---> bA | aB | ε
B ---> bB | aC | ε
C ---> bC | ε
32. Ambiguous grammar
A CFG is said to 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 is said to be ambiguous if and only if there exist a
string in T* that has more than on 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.
33. Parse Tree
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 :
34. Ambiguity Problems
Q. Check if the following grammar is ambiguous.
S ---> aS | X
A ---> Xa | a
S => aS
=> aaS
=> aaaS
=> aaaX
=> aaaa
S => X
=> aX
=> aaX
=> aaaX
=> aaaa
Hence the grammar is ambiguous.
35. Ambiguity Problems
Q. Check if the following grammar is ambiguous.
S ---> aB | bA
A ---> aS | bAA | a
B ---> bS | aBB | b
36. S ---> aB | bA
A ---> aS | bAA | a
B ---> bS | aBB | b
Hence the grammar
is ambiguous.
Ambiguity Problems
37. End of Day 6
www.linkedin.com/in/wadkar-rushabh
@RushabhWadkar
Thank you...