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

CGF and CFL

Uploaded by

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

CGF and CFL

Uploaded by

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

CONTEXT- FREE LANGUAGES AND

SIMPLIFICATION OF CONTEXT-FREE
GRAMMAR
Context Free Grammar

• Context free grammar is a formal grammar


which is used to generate all possible strings
in a given formal language.

• Context free grammar G can be defined by


four tuples as:
G= (V, T, P, S)
• N is a set of non-terminal symbols.
• T is a set of terminals where N ∩ T = NULL.
• P is a set of rules, P: N → (N ∪ T)*, i.e., the
left-hand side of the production rule P does
have any right context or left context.
• S is the start symbol.
Example
• The grammar ({A}, {a, b, c}, P, A),
P : A → aA, A → abc.
• The grammar ({S, a, b}, {a, b}, P, S),
P: S → aSa, S → bSb, S → ε
• The grammar ({S, F}, {0, 1}, P, S),
P: S → 00S | 11F, F → 00F | ε
Exercise
Capabilities of CFG
• Context free grammar is useful to describe most of the
programming languages.

• If the grammar is properly designed then an efficient parser


can be constructed automatically.

• Using the features of associatively & precedence information,


suitable grammars for expressions can be constructed.

• Context free grammar is capable of describing nested


structures like: balanced parentheses, matching begin-end,
corresponding if-then-else's & so on.
Applications of CFG
• Context Free Grammar (CFG) is of great practical
importance. It is used for following purposes-

• For defining programming languages
• For parsing the program by constructing syntax tree
• For translation of programming languages
• For describing arithmetic expressions
• For construction of compilers
Derivation
• Derivation is a sequence of production rules. It is used to
get the input string through these production rules.
• During parsing we have to take two decisions.
• These are as follows:

• We have to decide the non-terminal which is to be


replaced.
• We have to decide the production rule by which the non-
terminal will be replaced.
• We have two options to decide which non-terminal to be
replaced with production rule.
Left-most Derivation
• In the left most derivation, the input is
scanned and replaced with the production
rule from left to right.

• So in left most derivatives we read the input


string from left to right.
Example
• Production rules:
• S=S+S
• S=S-S
• S = a | b |c
• Input:
• a-b+c
• The left-most derivation is:
• S=S+S
• S=S-S+S
• S=a-S+S
• S=a-b+S
• S=a-b+c
Right-most Derivation
• In the right most derivation, the input is
scanned and replaced with the production
rule from right to left.

• So in right most derivatives we read the input


string from right to left.
Example
• Production rules:
• S=S+S
• S=S-S
• S = a | b |c
• Input:
• a-b+c
• The right-most derivation is:
• S=S-S
• S=S-S+S
• S=S-S+c
• S=S-b+c
• S=a-b+c
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.
The parse tree follows these points:

• All leaf nodes have to be terminals.

• All interior nodes have to be non-terminals.

• In-order traversal gives original input string.


Example
• Production rules:
• S= S + S | S * S
• S = a|b|c

• Input:
• a*b+c
Step 1 Step 2
Step 3

Step 4
Step 5
POLLING QUESTIONS
1. Which of the following statement is false?

a) Context free language is the subset of context


sensitive language
b) Regular language is the subset of context
sensitive language
c) Recursively ennumerable language is the super
set of regular language
d) Context sensitive language is a subset of context
free language
Example:
• Design a CFG for the regular language
corresponding to the RE 00 ∗11 ∗ .
• The language is the concatenation of two
languages: all strings of zeroes with all strings
of ones.

• P{S → CD C → 0C | 0 D → 1D | 1}
2. Which of the following statement is correct?

a) All Regular grammar are context free but not vice


versa
b) All context free grammar are regular grammar but
not vice versa
c) Regular grammar and context free grammar are
the same entity
d) None of the mentioned
Significance of CFG
• Context free languages strike a balance between
what is easy enough for a computer to understand
and what is expressive enough for a human to use.

• Mathematical expressions as well as large chunks of


human languages can be modeled by context free
grammars.

• Therefore they are the basis of most programming


languages and human-readable data formats.
Ambiguity in CFG
• 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 for the given input string.

• If the grammar is not ambiguous then it is


called unambiguous.
• If a context free grammar G has more than
one derivation tree for some string w ∈ L(G),
it is called an ambiguous grammar.

• There exist multiple right-most or left-most


derivations for some string generated from
that grammar.
Example 1

• Problem: Check whether the grammar G with


production rules −
X → X+X | X*X |X| a
is ambiguous or not.
• Solution

• Let’s find out the derivation tree for the string


"a+a*a". It has two leftmost derivations.
• Derivation 1 −
X → X+X → a +X → a+ X*X → a+a*X → a+a*a
• Parse Tree 1 −
• Derivation 2 −
X → X*X → X+X*X → a+ X*X → a+a*X → a+a*a
• Parse Tree 2 −

Since there are two parse trees for a single string


"a+a*a", the grammar G is ambiguous.
Example 2
• P: S = aSb | SS |∈
• For the string aabb, the above grammar
generates two parse trees:
Practice Questions
1. Check whether the given grammar is ambiguous or not-
S → SS| a| b

2. Check whether the given grammar is ambiguous or not-


S → A / B, A → aAb / ab, B → abB / ∈

3. Check whether the given grammar is ambiguous or not-


S → AB / C, A → aAb / ab, B → cBd / cd, C → aCd / aDd,
D → bDc / bc
4. Check whether the given grammar is
ambiguous or not-
R → R + R / R . R / R* / a / b

5. Check whether the given grammar is


ambiguous or not-
S → aSbS / bSaS / ∈
CFL Closure Property

• Context-free languages are closed under −


• Union
• Concatenation
• Kleene Star operation
Union
• Let L1 and L2 be two context free languages. Then L1 ∪ L2 is
also context free.
• Example
• Let L1 = { anbn , n > 0}. Corresponding grammar G1 will have
P: S1 → aAb|ab
• Let L2 = { cmdm , m ≥ 0}. Corresponding grammar G2 will
have P: S2 → cBb| ε
• Union of L1 and L2, L = L1 ∪ L2 = { anbn } ∪ { cmdm }
• The corresponding grammar G will have the additional
production S → S1 | S2
Concatenation
• If L1 and L2 are context free languages, then
L1L2 is also context free.
• Example
• Union of the languages L1 and L2,
• L = L1L2 = { anbncmdm }
• The corresponding grammar G will have the
additional production S → S1 S2
Kleene Star
• If L is a context free language, then L* is also
context free.
• Example
• Let L = { anbn , n ≥ 0}. Corresponding grammar
G will have P: S → aAb| ε
• Kleene Star L1 = { anbn }*
• The corresponding grammar G1 will have
additional productions S1 → S S1 | ε
• Context-free languages are not closed under −

• Intersection − If L1 and L2 are context free languages,


then L1 ∩ L2 is not necessarily context free.

• Intersection with Regular Language − If L1 is a


regular language and L2 is a context free language,
then L1 ∩ L2 is a context free language.

• Complement − If L1 is a context free language, then


L1’ may not be context free.
CFG Simplification
• In a CFG, it may happen that all the production
rules and symbols are not needed for the
derivation of strings.

• Besides, there may be some null productions


and unit productions.

• Elimination of these productions and symbols is


called simplification of CFGs.
• Simplification essentially comprises of the
following steps −

• Reduction of CFG
• Removal of Unit Productions
• Removal of Null Productions
POLLING QUESTIONS
1. Context free language are closed under

A. union, intersection
B. union, kleene closure
C. intersection, complement
D. complement, kleene closure
2. If G = ({S}, {a}, {S -> SS), S),

then language generated by G is


A.L (G) = φ
B.L(G) = an
C.L (G) = a*
D.L (G) = anban
3. A given grammar is called ambiguous if

A. two or more productions have the same non-


terminal on the left hand side
B. a derivation tree has more than one associated
sentence
C. there is a sentence with more than one derivation
tree corresponding to it
D. brackets are not present in the grammar
4. Which of the following derivations does a top-down
parser use while parsing an input string? The input
is assumed to be scanned in left to right order
(A) Leftmost derivation

(B) Leftmost derivation traced out in reverse

(C) Rightmost derivation

(D) Rightmost derivation traced out in reverse


5. Which among the following is the root of the
parse tree?
(A) Production P

(B) Nonterminal V

(C) Terminal T

(D) Starting symbol S

You might also like