ATCD UT3 Material
ATCD UT3 Material
This is a hierarchy. Therefore every language of type 3 is also of type 2, 1 and 0. Similarly, every
language of type 2 is also of type 1 and type 0, etc.
1
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
1. Type 0 Grammar:
Type 0 grammar is known as Unrestricted grammar. There is no restriction on the grammar rules
of these types of languages. These languages are also known as the Recursively Enumerable
languages. These languages can be efficiently modeled by Turing machines.
Example:
bAa → aa
S→s
2. Type 1 Grammar:
Type 1 grammar is known as Context Sensitive Grammar. The context sensitive grammar is used
to represent context sensitive language. The language generated by the grammar is recognized by
the Linear Bound Automata. The context sensitive grammar follows the following rules:
The context sensitive grammar may have more than one symbol on the left hand side of
their production rules.
The number of symbols on the left-hand side must not exceed the number of symbols on
the right-hand side.
The rule of the form A → ε is not allowed unless A is a start symbol. It does not occur on
the right-hand side of any rule.
The Type 1 grammar should be Type 0. In type 1, Production is in the form of α → β,
where |α| ≤ |β|.
Example:
S → AT
T → xy
A→a
3. Type 2 Grammar:
Type 2 Grammar is known as Context Free Grammar. Type2 grammars generate context free
languages. The language generated by the grammar is recognized by pushdown automata. Type
2 should be type 1. The production rule is of the form: α → β, where |α| ≤ |β|, αϵV, |α| = 1.
Example:
A → aBb
2
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
A→b
B→a
4. Type 3 Grammar:
Type 3 Grammar is known as Regular Grammar. Regular languages are those languages which
can be described using regular expressions. These languages can be modeled by NFA or DFA.
Type 3 is most restricted form of grammar. The Type 3 grammar should be Type 2 and Type 1.
Type 3 should be in the form of α → β.
Example:
Definition − A context-free grammar (CFG) is a formal grammar which is used to generate all
possible strings in a given formal language.
G= (V, T, P, S)
Where,
Example:
Production rules:
3
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
S → aSa
S → bSb
S→c
Now check that abbcbba string can be derived from the given CFG.
S ⇒ aSa
S ⇒ abSba
S ⇒ abbSbba
S ⇒ abbcbba
By applying the production S → aSa, S → bSb recursively and finally applying the production
2. The rule should be always in the form of L.H.S → R.H.S. Where R.H.S may be the
combination of non-terminal and terminal symbols.
4. One of the non-terminals should be start symbol and conventionally we should write the rules
for this non-terminal.
Example:
4
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
A partial derivation tree is a sub-tree of a derivation tree/parse tree such that either all of its
children are in the sub-tree or none of them are in the sub-tree.
Example:
If a partial derivation tree contains the root S, it is called a sentential form. The above sub-tree is
also in sentential form.
5
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
3.2.1 Leftmost and Rightmost Derivation of a String –
Example:
Obtain the leftmost and rightmost derivation for the string "a+a*a".
X → X+X
X → a+X
X → a + X*X
X → a+a*X
X → a+a*a
Step1:
Step2:
6
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
Step3:
Step4:
Step5:
X → X*X
X → X*a
X → X+X*a
X → X+a*a
7
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
X → a+a*a
Step1:
Step2:
Step4:
8
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
Exercise Problem 1: Consider the grammar
S → (L) | a
L → L, S | a
i) (a, a)
S → 0A | 1B | 0 | 1
A → 0S | 1B | 1
B → 0A | 1S
Construct leftmost derivations and parse tree for the following sentences
9
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
3.2.2 Parse Tree:
Parse tree is the graphical representation of symbol. The symbol can be terminal or non-
terminal.
In parsing, the string is derived using the start symbol. The root of the parse tree is that
start symbol.
It is the graphical representation of symbol that can be terminals or non-terminals.
Parse tree follows the precedence of operators. The deepest sub-tree traversed first. So,
the operator in the parent node has less precedence over the operator in the sub-tree.
Example:
Production rules:
S→S+S|S*S
S → a|b|c
Input:
a*b+c
Step1:
Step2:
10
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
Step3:
Step4:
Step5:
11
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
3.3 Ambiguity in context free grammars and removing it:
3.3.1 Ambiguous Grammar:
A grammar is said to be ambiguous if there exists more than one leftmost derivation or more
than one rightmost derivative or more than one parse tree/derivation tree for the given input
string. If the grammar is not ambiguous then it is called unambiguous.
Example:
S → aSb | SS
S→ε
For the string aabb, the above grammar generates two parse trees:
If the grammar has ambiguity then it is not good for a compiler construction. No method can
automatically detect and remove the ambiguity but you can remove ambiguity by re-writing the
whole grammar without ambiguity.
1. If the left associative operators (+, -, *, /) are used in the production rule, then apply left
recursion in the production rule. Left recursion means that the leftmost symbol on the right side
is the same as the non-terminal on the left side. For example,
X → Xa
2. If the right associative operates(^) is used in the production rule then apply right recursion in
the production rule. Right recursion means that the rightmost symbol on the left side is the same
as the non-terminal on the right side. For example,
X → aX
12
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
Example 1:
Show that the given grammar is ambiguous. Also, find an equivalent unambiguous grammar.
E→E+E
E→E*E
E → id
Solution:
As there are two different parse tree for deriving the same string, the given grammar is
ambiguous.
E→E+T
E→T
T→T*F
T→F
F → id
13
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
Example 2:
S → AB | aaB
A → a | Aa
B→b
Solution:
As there are two different parse tree for deriving the same string, the given grammar is
ambiguous.
S → AB
A → Aa | a
B→b
Example 3:
Check that the given grammar is ambiguous or not. Also, find an equivalent unambiguous
grammar.
S→S+S
14
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
S→S*S
S→S^S
S→a
Solution:
The given grammar is ambiguous because the derivation of string aab can be represented by the
following string:
S→S+A|
A→A*B|B
B→C^B|C
C→a
15
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
3.4 Applications and Properties of CFL:
a) Union: If L1 and L2 are two context free languages, their union L1 ∪ L2 will also be context
free.
Example:
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.
b) Concatenation: If L1 and If L2 are two context free languages, their concatenation L1.L2
will also be context free.
Example:
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.
c) Kleene Closure: If L1 is context free, its Kleene closure L1* will also be context free.
Example:
16
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
L1 = { anbn | n >= 0 }
Intersection − If L1 and L2 are context free languages, then L1 ∩ L2 is not necessarily context
free.
Complement − If L1 is a context free language, then L1’ may not be context free.
A → A α |β.
The above Grammar is left recursive because the left most non-terminal on the right side of the
production is same as left side of the non-terminal. It can eliminate left recursion by re-writing
the production rule as:
A → βA′
A′ → αA′
A′ → ϵ
In Left Recursive Grammar, expansion of A will generate Aα, Aαα, Aααα at each step, causing it
to enter into an infinite loop. This causes major problem in top-down parsing and therefore
elimination of left recursion is a must.
17
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
This type of recursion is also called Immediate Left Recursion.
E → E + T|T
T → T * F|F
F → (E)|id
Solution:
E → TE′
E′ → +TE′| ∈
T → FT′
T′ →∗ FT′| ∈
F → (E)|id
A → ABd|Aa|a
B → Be|b
A → αβ1 | αβ2 | αβ3 | …… | αβn | γ i.e the productions start with the same terminal (or set of
terminals). On seeing the input α we cannot immediately tell which production to choose to
expand A.
Left factoring is a grammar transformation that is useful for producing grammar suitable for
predictive or top-down parsing. When the choice between two alternative A-productions is not
clear, we may be able to rewrite the productions to defer the decision until enough of the input
has been seen to make the right choice.
A′ → β1 | β2 | β3 | …… | βn
Example1:
S → iEtS | iEtSeS | a
E→b
Solution:
S → iEtSS′ | a
S′ → eS | ε
Example2:
A → Aab | aA | a
B → Bb | b
Following is the Context Free Grammar for HTML (with limited tags):
Char -? a | A | . . .
Text → λ | Char Text
Doc → λ | Element Doc
Element → Text | < EM > Doc < /EM >|< P > Doc |< OL > List < /OL >
List → λ | ListItem List
19
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.
ListItem → < li > Doc
All finite set of strings are Regular Languages. All Regular Language is a Context Free
Language. Hence, all Programming Languages are Context Free Languages / can be represented
by a Context Free Grammar.
For example, this is the rules of a Context Free Grammar for syntactically correct Infix
expression using 3 variables (x, y, z):
S → empty
S → (S)
S→x
S→y
S→z
S→S+S
S →S – S
S→S*S
S→S/S
20
Mr. K. Leela Prasad, Asst. Prof., CSE Dept.