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

Chapter 4

For Read

Uploaded by

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

Chapter 4

For Read

Uploaded by

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

Chapter 4: Context free languages

Context free languages

Context-Free Language (CFL) is a language generated by a Context-Free Grammar (CFG). CFG is a set
of rules for deriving (or generating) strings (or sentences) in a language.

A grammar G = (N, T, S, P) is said to be context-free if all productions in P have the form


A → α where A is a Non-terminal [A ∈ N] and α is any string containing Non-terminals and/or
terminals. [α ∈ (N ∪ T)*].

Definition: Let L be a language. Then L is a context-free language if and only if there exists a
context-free grammar G such that L = L(G).

Every regular grammar is context-free, so a regular language is also a context- free one, so we
see that the family of regular languages is a proper subset of the family of context-free
languages. Context- free grammars derive their name from the fact that the substitution of the
variable on the left of a production can be made any time such a variable appears in a sentential
form. It does not depend on the symbols in the rest of the sentential form (the context). This
feature is the consequence of allowing only a single variable on the left side of the production.

Examples of Context-Free languages:


The grammar G = ({S}, {a, b}, S, P) with productions
S → aSa
S → bSb
S→ε
is context-free. A typical derivation in this grammar is
S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa
This makes it clear that
L(G) = {wwR : w ∈ {a, b}*}.
The language is context-free

Parsing and derivation tree

Definition: Let G = (V, T, P, S) be a CFG. A tree is a derivation (or parse) tree if:
– Every vertex has a label from V U T U {ε}
– The label of the root is S
– If a vertex with label A has children with labels X1, X2,…, Xn, from left to right,
then
A –> X1, X2,…, Xn
must be a production in P
If a vertex has label ε, then that vertex is a leaf and the only child of its’ parent
More generally, a derivation tree can be defined with any non-terminal as the root.

Example

Page | 1
For generating a language that generates equal number of a’s and b’s in the form
anbn . the context free grammar is defined as G= {(S, A), (a, b), (S aAb, A
aAb/ ε)}
SaAb
 aaAbb (AaAb)
 aaaAbbb (A aAb)
 aaabb (A ε)
 aaabbb  a3b3  anbn

Page | 2
For example:
Consider the grammar with productions
S → aAB A → bBb B → A | ε
Then,
S ⇒aAB⇒abBbB⇒abAbB⇒abbBbbB⇒abbbbB⇒ abbbb is a leftmost derivation of the string
abbbb. A rightmost
derivation of the same string is
S ⇒ aAB ⇒ aA ⇒ abBb ⇒ abAb ⇒ abbBbb ⇒ abbbb.

Context Free Grammar


Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules in
a quadruple (N, T, P, S) where

Page | 3
 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.

Page | 4
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 | ε

Derivation Order
Consider the following example, grammar with 5 productions:

Derivation Trees
Consider the same example grammar:

S  AB A  aaA |  B  Bb | 
Page | 5
Page | 6
Ambiguity in Grammars and Languages:

A context free grammar G is said to be ambiguous if there exists some w ∈ L(G) which has at least two
distinct derivation trees. Alternatively ambiguity implies the existence of two or more leftmost or
rightmost derivations.

For example consider the grammar


S → SS
S → aSb
S→ε
The sentence aabb has two derivation trees as shown in the figures below:

Hence the above grammar is an Ambiguous grammar.


Ambiguity is a common feature of natural languages, where it is tolerated and dealt with in a
variety of ways. In programming languages, where there should be only one interpretation of

Page | 7
each statement, ambiguity must be removed when possible. Often we can achieve this by
rewriting the grammar in an equivalent, unambiguous form.

Let us demonstrate this with an example. Consider the grammar G = (N, T, E, P)


with
N = {E, I}
T = {a, b, c, +, *, (, )}
and productions
E→E+E
E→E*E
E → (E)
E→I
I→a|b|c

The grammar is ambiguous, for the reason, the string a+b*c


has two different derivation trees, as shown in the figures
below:

And in the other side:

Page | 8
No other derivation tree is possible for this string: the grammar is unambiguous.

Page | 9
Removing Useless Productions
Let G = (N, T, S, P) be a context free grammar. A variable A ∈ N is said to be useful if and only if there
is at least one w ∈ L(G) such that S ⇒αAβ ⇒w where α and β are any strings. A is a Non-
terminal and w is a sentence
In words, a variable is useful if and only if it occurs in at least one derivation. A variable or non-terminal
that is not useful is called Useless. A production is useless if it involves any useless variable.
For example, consider the context free grammar
G = (N, T, S, P)
with N = {S, A, B}, T = {a, b} and, P having the productions
S→A
A → aA | ε
B → bA
In this case the non-terminal or variable B is useless and so is the production B → bA. Although B can
derive a terminal string, there is no way we can achieve S ⇒ xBy , because B cannot be reached from
the start symbol. A method to identify and eliminate the non-terminals or variables that cannot be reached
from the start symbol is by constructing a dependency graph for the non-terminals. Dependency graphs
are a way of visualizing complex relationships and are found in many applications. For context-free
grammars, a dependency graph has its vertices labeled with non-terminals or variables, with an edge
between the vertices C and D if and only if there is a production rule
C → xDy
where C and D are non-terminals x and y are terminals.
The dependency graph for the above context-free grammar is shown in the figure below:

Page | 10
A non-terminal or variable is useful only if there is a path from the vertex of the start symbol to the vertex
of that non-terminal or variable. In our case the vertex labeled with the non-terminal or variable B cannot
be reached from the vertex of the start symbol labeled S. Removing it and the corresponding production
rule we obtain the resulting grammar
G = ( N , T , S , P)
where N = {S, A}
T = {a}
P = { S → A, A → aA | ε }

Page | 11

You might also like