Context Free
Grammar
and
Languages
UNIT III
BY DR.T.P.LATCHOUMI
Introduction
A context-free grammar provides a simple and mathematically precise mechanism for describing the methods by which phrases in some
natural language are built from smaller blocks, capturing the "block structure" of sentences in a natural way. A grammar consists of one or
more variables that represent classes of strings (i.e., languages)
There are rules that say how the strings in each class are constructed.
The construction can use
1. Symbols of the alphabet
2. Strings that are already known to be in one of the classes
3. Or both
DR.T.P.LATCHOUMI 1/11/2022
Types of
Grammar
DR.T.P.LATCHOUMI 1/11/2022
Depending on the production rules
1. Type 0 grammars (unrestricted grammar) with no restriction on rewriting rules;
2. Type 1 grammars (Context-sensitive grammars) have the rules of the form αAβ → αγβ
where A is a nonterminal, α, β, γ are strings of terminals and nonterminals, and γ is non-empty.
3. Type 2 grammars (Context-free grammars) have the rules of the form A → γ
where A is a nonterminal, and γ is a string (potentially empty) of terminals and nonterminals.
4. Type 3 grammars (Regular grammars) have the rules of the form A → aB or A → a
where A, B are nonterminals, and a is a string (potentially empty) of terminals.
DR.T.P.LATCHOUMI 1/11/2022
Depending on
the derivation
tree
Ambiguous grammar
DR.T.P.LATCHOUMI 1/11/2022
Cont…
UnAmbiguous grammar
DR.T.P.LATCHOUMI 1/11/2022
Depending
on the basis
of strings
DR.T.P.LATCHOUMI 1/11/2022
Cont...
DR.T.P.LATCHOUMI 1/11/2022
Cont…
Example 2 A->Bx or A->x
Example 3
Construct the regular grammar for regular expression r=0(10)*
Solution
S->S10|0
DR.T.P.LATCHOUMI 1/11/2022
Right recursive grammar
DR.T.P.LATCHOUMI 1/11/2022
Cont…
Example 2
A->xB or A->x
Example 3
Construct the regular grammar for regular expression r=0(10)*
Solution
S->0A
A->10A| ε
DR.T.P.LATCHOUMI 1/11/2022
DR.T.P.LATCHOUMI 1/11/2022
Definition of Context Free Grammar
A GFG (or just a grammar) G is a tuple G = (V, T, P, S) where
1. V is the (finite) set of variables (or nonterminal or syntactic categories). Each variable represents a language,
i.e., a set of strings
2. T is a finite set of terminals, i.e., the symbols that form the strings of the language being defined
3. P is a set of production rules that represent the recursive definition of the language.
4. S is the start symbol that represents the language being defined.
Other variables represent auxiliary classes of strings that are used to help define the language of the start symbol.
DR.T.P.LATCHOUMI 1/11/2022
Production Rules
Each production rule consists of:
1. A variable that is being (partially) defined by the production. This variable is often called the head of the production.
2. The production symbol →.
3. A string of zero or more terminals and variables. This string, called the body of the production, represents one way to form
strings in the language of the variable of the head.
In doing so, we leave terminals unchanged and substitute for each variable of the body any string that is known to be in the
language of that variable
DR.T.P.LATCHOUMI 1/11/2022
Compact Notation for Productions
We often refer to the production whose head is A as “productions for A” or “A-productions”.
Moreover, the productions
can be replaced by the notation
DR.T.P.LATCHOUMI 1/11/2022
Rules
The first three rules form the basis.
 They tell us that the class of palindromes includes the strings ε, 0
and 1.
 None of the right sides of these rules contains a variable, which is
why they form a basis for the definition.
The last two rules form the inductive part of the definition.
For instance, rule 4 says that if we take any string w from the class P,
then 0w0 is also in class P.
DR.T.P.LATCHOUMI 1/11/2022
Examples
1. The grammar ({A}, {a, b, c}, P, A),
P: A → aA, A → abc.
2. The grammar ({S, a, b}, {a, b}, P, S),
P: S → aSa, S → bSb, S → ε
3. The grammar ({S, F}, {0, 1}, P, S),
P: S → 00S | 11F, F → 00F | ε
DR.T.P.LATCHOUMI 1/11/2022
Cond….
Consider the grammar
S → AB (1)
A → C (2)
CB → Cb (3)
C → a (4)
where {a, b} are terminals, and {S, A, B, C} are non-terminals
DR.T.P.LATCHOUMI 1/11/2022
Derivations
using a
grammar
We apply the productions of a
CFG to infer that certain strings are
in the language of a certain variable
Two inference approaches
1. Recursive inference, using
productions from body to head
DR.T.P.LATCHOUMI 1/11/2022
Derivations
Using productions from head to
body (or) we derive strings in the
language of a CGG by starting with
the start symbol and repeatedly
replacing some variables A by the
right side of one of its productions.
The generation of language
using specific rules called
derivations.
DR.T.P.LATCHOUMI 1/11/2022
Examples
DR.T.P.LATCHOUMI 1/11/2022
Leftmost
derivation
A leftmost derivation is
obtained by applying
production to the leftmost
variable in each step.
DR.T.P.LATCHOUMI 1/11/2022
Example 2
Derive the string "abb" for leftmost derivation using a CFG
given by,
S → AB | ε
A → aB
B → Sb
DR.T.P.LATCHOUMI 1/11/2022
Example 4
Derive the string "00101" for
leftmost derivation using a
CFG given by,
S → A1B
A → 0A | ε
B → 0B | 1B | ε
DR.T.P.LATCHOUMI 1/11/2022
Rightmost
derivation
A rightmost derivation is
obtained by applying
production to the rightmost
variable in each step.
DR.T.P.LATCHOUMI 1/11/2022
Example 2
Derive the string "abb" for rightmost derivation using a CFG
given by,
S → AB | ε
A → aB
B → Sb
DR.T.P.LATCHOUMI 1/11/2022
Example 3
Derive the string "aabbabba" for rightmost derivation using a
CFG given by,
S → aB | bA
S → a | aS | bAA
S → b | aS | aBB
DR.T.P.LATCHOUMI 1/11/2022
Derivation tree or parse tree
A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic
information a string derived from a context-free grammar.
Beginning with the start symbol, we derive terminal strings by repeatedly replacing a variable by the body
of some production with that variable in the head.
DR.T.P.LATCHOUMI 1/11/2022
Representation technique
Root vertex − Must be labeled by the start symbol.
Vertex − Labeled by a non-terminal symbol.
Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows −
DR.T.P.LATCHOUMI 1/11/2022
Properties of Parse tree
 Root node of a parse tree is the start symbol of the grammar.
 Each leaf node of a parse tree represents a terminal symbol.
 Each interior node of a parse tree represents a non-terminal symbol.
 Parse tree is independent of the order in which the productions are used during derivations.
DR.T.P.LATCHOUMI 1/11/2022
Derivation or yield of a tree
 The derivation or the yield of a parse tree is the final string obtained by concatenating the labels of the
leaves of the tree from left to right, ignoring the Nulls. However, if all the leaves are Null, derivation is
Null.
 Concatenating the leaves of a parse tree from the left produces a string of terminals.
 This string of terminals is called as yield of a parse tree.
DR.T.P.LATCHOUMI 1/11/2022
Example
Let a CFG {N,T,P,S} be
N = {S}, T = {a, b}, Starting
symbol = S, P = S → SS | aSb | ε
One derivation from the above
CFG is “abaabb”
S → SS → aSbS → abS → abaSb
→ abaaSbb → abaabb
DR.T.P.LATCHOUMI 1/11/2022
Leftmost derivation tree
The process of deriving a string by expanding the leftmost non-terminal at each step is called as leftmost
derivation.
The geometrical representation of leftmost derivation is called as a leftmost derivation tree.
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Example 2
S → aB / bA
S → aS / bAA / a
B → bS / aBB / b
DR.T.P.LATCHOUMI 1/11/2022
Rightmost derivation tree
The process of deriving a string by expanding the rightmost non-terminal at each step is called
as rightmost derivation.
The geometrical representation of rightmost derivation is called as a rightmost derivation tree.
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Example 2
S → aB / bA
S → aS / bAA / a
B → bS / aBB / b
DR.T.P.LATCHOUMI 1/11/2022
Context Free
Language
The language generated by
grammar G is denoted by L(G).
Where L is called Context
Free Language (CFL).
DR.T.P.LATCHOUMI 1/11/2022
Example 1
Construct a CFG to
generate set of palindromes
over alphabet {a,b}.
DR.T.P.LATCHOUMI 1/11/2022
Examples
Example 2
Design CFG for a given language L(G)={ aibi | i ≥ 0}
Solution: L={ ,ab,aabb,aaabbb,…}
S  aSb | 
Example 3
Design CFG for a given language L(G)={aibi | i > 0}
Solution: L={ab,aabb,aaabbb,…}
S  aSb | ab
DR.T.P.LATCHOUMI 1/11/2022
Theorem 1
DR.T.P.LATCHOUMI 1/11/2022
Theorem 2
DR.T.P.LATCHOUMI 1/11/2022
Applications of Context Free Grammar
 For defining programming languages. In most programming languages opening and closing of braces,
curly brackets is taken care. It mainly tracks it and if any closing bracket is not there, it will throw error.
 For parsing the program by constructing syntax tree
 For translation of programming languages
 For describing arithmetic expressions
 For construction of compilers.
DR.T.P.LATCHOUMI 1/11/2022
Cond…
 In a newer application, they are used in an essential part of the Extensible Markup Language (XML) called the
Document Type Definition.
 Context-free grammars are used as basis for compiler design and implementation
 Context-free grammars are used as specification mechanisms for programming languages
 Designers of compilers use such grammars to implement compiler‟s components, such as scanners, parsers,
code generators, code synthesizers
 The implementation of almost any programming languages is preceded by a context-free grammar that specifies
it.
DR.T.P.LATCHOUMI 1/11/2022
Ambiguous
Grammar
A CFG is ambiguous if one or
more terminal strings have
multiple leftmost derivations or
multiple rightmost derivations
or multiple parse trees from the
start symbol.
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Difference
between
Ambiguous and
unambiguous
grammar
DR.T.P.LATCHOUMI 1/11/2022
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 −
1. Removal of Useless Symbols
2. Removal of Unit Productions
3. Removal of Null Productions
4. 1,11,48,58
DR.T.P.LATCHOUMI 1/11/2022
Removal of
useless
symbols
DR.T.P.LATCHOUMI 1/11/2022
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Removal of unit
productions
The productions of the form AB where A and B
are both non-terminals is said to a unit production
and can be eliminated.
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Removal of null productions
 Productions are set of rules in context free grammar that define the language.
 A production of the form A  ε is called as epsilon production, or null production.
 Using this rule, the appearance of A in the RHS of any production can be nullified.
 Epsilon productions are very useful to express many grammars in a compact way.
Productions of the form (Aε) is said to be a ε-production that generates empty string are said to be ε-
productions. These productions can be eliminated
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Normal forms for CFG
In a Context Free Grammar, the right-hand side of the production can be any string of variables and terminals.
When productions in G satisfy certain restrictions, then G is said to be in a Normal Form.
 By reducing the grammar, the grammar gets minimized but does not gets standardized.
 This is because the RHS of productions have no specific format.
 To standardize the grammar, normalization is performed using normal forms.
There are two widely useful Normal forms of CFG. They are
 Chomsky Normal Form (CNF)
 Greibach Normal Form (GNF)
DR.T.P.LATCHOUMI 1/11/2022
Chomsky Normal Form
Definition: A context-free grammar G is in Chomsky normal form if any production is of the form:
A  BC or A  a
where „a‟ is a terminal, A,B,C are non-terminals, and B,C may not be the start variable (the axiom)
Note:
1. In CNF number of symbols on right side of production strictly limited.
2. The rule S, where S is the start variable, is not excluded from a CFG in Chomsky normal form.
DR.T.P.LATCHOUMI 1/11/2022
Conversion to
CNF
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Greibach Normal Form (GNF)
A CFG G = (V, T, P, S) is said to be in GNF if every production is of the form A  aα, where a € T and α € V*, i.e., α is a string of zero or more variables.
Definition: A production U € R is said to be in the form left recursion, if U: A  Aα for some A € V.
o Left recursion in R can be eliminated by the following scheme:
o If A  Aα1|Aα2| . . . |Aαr|β1| β2| . . . | βs, then replace the above rules by
 A  βi| βiZ, 1≤ i ≤ s and
 Z αi|αiZ, 1≤ i≤ r
o If G = (V, T, P, S) is a CFG, then we can construct another CFG G1 = (V1, T, P1, S) in Greibach Normal Form (GNF) such that L(G1) = L(G) − {}.
DR.T.P.LATCHOUMI 1/11/2022
Algorithm
DR.T.P.LATCHOUMI 1/11/2022
Example 1
Convert the following grammar G
into Greibach Normal Form (GNF).
S  XA|BB
B  b|SB
X  b
A  a
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Example 2
1. S → XB | AA
2. A → a | SA
3. B → b
4. X → a
DR.T.P.LATCHOUMI 1/11/2022
Cont…
DR.T.P.LATCHOUMI 1/11/2022
Difference between CNF
and GNF
DR.T.P.LATCHOUMI 1/11/2022
Pumping Lemma for CFL
Pumping lemma can be used to check whether the language is context free or not
o Let L be a CFL. Then there exists a constant „n‟ such that for every string „z‟ in L such that |z|≥n.‟z‟ can
be broken into three substrings z=uvwxy such that:
(i) |vx|≥1
(ii) |vwx|≤n and
(iii)for all i≥0 uvi wxiy is in L
DR.T.P.LATCHOUMI 1/11/2022
Example 1
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Closure properties for CFL
Context-free languages are closed under −
 Union
 Concatenation
 Kleene Star operation
DR.T.P.LATCHOUMI 1/11/2022
Union
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Concatenation
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
Kleen Star Operation
DR.T.P.LATCHOUMI 1/11/2022
Example 2
DR.T.P.LATCHOUMI 1/11/2022
DR.T.P.LATCHOUMI 1/11/2022

Unit iii

  • 1.
  • 2.
    Introduction A context-free grammarprovides a simple and mathematically precise mechanism for describing the methods by which phrases in some natural language are built from smaller blocks, capturing the "block structure" of sentences in a natural way. A grammar consists of one or more variables that represent classes of strings (i.e., languages) There are rules that say how the strings in each class are constructed. The construction can use 1. Symbols of the alphabet 2. Strings that are already known to be in one of the classes 3. Or both DR.T.P.LATCHOUMI 1/11/2022
  • 3.
  • 4.
    Depending on theproduction rules 1. Type 0 grammars (unrestricted grammar) with no restriction on rewriting rules; 2. Type 1 grammars (Context-sensitive grammars) have the rules of the form αAβ → αγβ where A is a nonterminal, α, β, γ are strings of terminals and nonterminals, and γ is non-empty. 3. Type 2 grammars (Context-free grammars) have the rules of the form A → γ where A is a nonterminal, and γ is a string (potentially empty) of terminals and nonterminals. 4. Type 3 grammars (Regular grammars) have the rules of the form A → aB or A → a where A, B are nonterminals, and a is a string (potentially empty) of terminals. DR.T.P.LATCHOUMI 1/11/2022
  • 5.
    Depending on the derivation tree Ambiguousgrammar DR.T.P.LATCHOUMI 1/11/2022
  • 6.
  • 7.
    Depending on the basis ofstrings DR.T.P.LATCHOUMI 1/11/2022
  • 8.
  • 9.
    Cont… Example 2 A->Bxor A->x Example 3 Construct the regular grammar for regular expression r=0(10)* Solution S->S10|0 DR.T.P.LATCHOUMI 1/11/2022
  • 10.
  • 11.
    Cont… Example 2 A->xB orA->x Example 3 Construct the regular grammar for regular expression r=0(10)* Solution S->0A A->10A| ε DR.T.P.LATCHOUMI 1/11/2022
  • 12.
  • 13.
    Definition of ContextFree Grammar A GFG (or just a grammar) G is a tuple G = (V, T, P, S) where 1. V is the (finite) set of variables (or nonterminal or syntactic categories). Each variable represents a language, i.e., a set of strings 2. T is a finite set of terminals, i.e., the symbols that form the strings of the language being defined 3. P is a set of production rules that represent the recursive definition of the language. 4. S is the start symbol that represents the language being defined. Other variables represent auxiliary classes of strings that are used to help define the language of the start symbol. DR.T.P.LATCHOUMI 1/11/2022
  • 14.
    Production Rules Each productionrule consists of: 1. A variable that is being (partially) defined by the production. This variable is often called the head of the production. 2. The production symbol →. 3. A string of zero or more terminals and variables. This string, called the body of the production, represents one way to form strings in the language of the variable of the head. In doing so, we leave terminals unchanged and substitute for each variable of the body any string that is known to be in the language of that variable DR.T.P.LATCHOUMI 1/11/2022
  • 15.
    Compact Notation forProductions We often refer to the production whose head is A as “productions for A” or “A-productions”. Moreover, the productions can be replaced by the notation DR.T.P.LATCHOUMI 1/11/2022
  • 16.
    Rules The first threerules form the basis.  They tell us that the class of palindromes includes the strings ε, 0 and 1.  None of the right sides of these rules contains a variable, which is why they form a basis for the definition. The last two rules form the inductive part of the definition. For instance, rule 4 says that if we take any string w from the class P, then 0w0 is also in class P. DR.T.P.LATCHOUMI 1/11/2022
  • 17.
    Examples 1. The grammar({A}, {a, b, c}, P, A), P: A → aA, A → abc. 2. The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε 3. The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε DR.T.P.LATCHOUMI 1/11/2022
  • 18.
    Cond…. Consider the grammar S→ AB (1) A → C (2) CB → Cb (3) C → a (4) where {a, b} are terminals, and {S, A, B, C} are non-terminals DR.T.P.LATCHOUMI 1/11/2022
  • 19.
    Derivations using a grammar We applythe productions of a CFG to infer that certain strings are in the language of a certain variable Two inference approaches 1. Recursive inference, using productions from body to head DR.T.P.LATCHOUMI 1/11/2022
  • 20.
    Derivations Using productions fromhead to body (or) we derive strings in the language of a CGG by starting with the start symbol and repeatedly replacing some variables A by the right side of one of its productions. The generation of language using specific rules called derivations. DR.T.P.LATCHOUMI 1/11/2022
  • 21.
  • 22.
    Leftmost derivation A leftmost derivationis obtained by applying production to the leftmost variable in each step. DR.T.P.LATCHOUMI 1/11/2022
  • 23.
    Example 2 Derive thestring "abb" for leftmost derivation using a CFG given by, S → AB | ε A → aB B → Sb DR.T.P.LATCHOUMI 1/11/2022
  • 24.
    Example 4 Derive thestring "00101" for leftmost derivation using a CFG given by, S → A1B A → 0A | ε B → 0B | 1B | ε DR.T.P.LATCHOUMI 1/11/2022
  • 25.
    Rightmost derivation A rightmost derivationis obtained by applying production to the rightmost variable in each step. DR.T.P.LATCHOUMI 1/11/2022
  • 26.
    Example 2 Derive thestring "abb" for rightmost derivation using a CFG given by, S → AB | ε A → aB B → Sb DR.T.P.LATCHOUMI 1/11/2022
  • 27.
    Example 3 Derive thestring "aabbabba" for rightmost derivation using a CFG given by, S → aB | bA S → a | aS | bAA S → b | aS | aBB DR.T.P.LATCHOUMI 1/11/2022
  • 28.
    Derivation tree orparse tree A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information a string derived from a context-free grammar. Beginning with the start symbol, we derive terminal strings by repeatedly replacing a variable by the body of some production with that variable in the head. DR.T.P.LATCHOUMI 1/11/2022
  • 29.
    Representation technique Root vertex− Must be labeled by the start symbol. Vertex − Labeled by a non-terminal symbol. Leaves − Labeled by a terminal symbol or ε. If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows − DR.T.P.LATCHOUMI 1/11/2022
  • 30.
    Properties of Parsetree  Root node of a parse tree is the start symbol of the grammar.  Each leaf node of a parse tree represents a terminal symbol.  Each interior node of a parse tree represents a non-terminal symbol.  Parse tree is independent of the order in which the productions are used during derivations. DR.T.P.LATCHOUMI 1/11/2022
  • 31.
    Derivation or yieldof a tree  The derivation or the yield of a parse tree is the final string obtained by concatenating the labels of the leaves of the tree from left to right, ignoring the Nulls. However, if all the leaves are Null, derivation is Null.  Concatenating the leaves of a parse tree from the left produces a string of terminals.  This string of terminals is called as yield of a parse tree. DR.T.P.LATCHOUMI 1/11/2022
  • 32.
    Example Let a CFG{N,T,P,S} be N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε One derivation from the above CFG is “abaabb” S → SS → aSbS → abS → abaSb → abaaSbb → abaabb DR.T.P.LATCHOUMI 1/11/2022
  • 33.
    Leftmost derivation tree Theprocess of deriving a string by expanding the leftmost non-terminal at each step is called as leftmost derivation. The geometrical representation of leftmost derivation is called as a leftmost derivation tree. DR.T.P.LATCHOUMI 1/11/2022
  • 34.
  • 35.
    Example 2 S →aB / bA S → aS / bAA / a B → bS / aBB / b DR.T.P.LATCHOUMI 1/11/2022
  • 36.
    Rightmost derivation tree Theprocess of deriving a string by expanding the rightmost non-terminal at each step is called as rightmost derivation. The geometrical representation of rightmost derivation is called as a rightmost derivation tree. DR.T.P.LATCHOUMI 1/11/2022
  • 37.
  • 38.
    Example 2 S →aB / bA S → aS / bAA / a B → bS / aBB / b DR.T.P.LATCHOUMI 1/11/2022
  • 39.
    Context Free Language The languagegenerated by grammar G is denoted by L(G). Where L is called Context Free Language (CFL). DR.T.P.LATCHOUMI 1/11/2022
  • 40.
    Example 1 Construct aCFG to generate set of palindromes over alphabet {a,b}. DR.T.P.LATCHOUMI 1/11/2022
  • 41.
    Examples Example 2 Design CFGfor a given language L(G)={ aibi | i ≥ 0} Solution: L={ ,ab,aabb,aaabbb,…} S  aSb |  Example 3 Design CFG for a given language L(G)={aibi | i > 0} Solution: L={ab,aabb,aaabbb,…} S  aSb | ab DR.T.P.LATCHOUMI 1/11/2022
  • 42.
  • 43.
  • 44.
    Applications of ContextFree Grammar  For defining programming languages. In most programming languages opening and closing of braces, curly brackets is taken care. It mainly tracks it and if any closing bracket is not there, it will throw error.  For parsing the program by constructing syntax tree  For translation of programming languages  For describing arithmetic expressions  For construction of compilers. DR.T.P.LATCHOUMI 1/11/2022
  • 45.
    Cond…  In anewer application, they are used in an essential part of the Extensible Markup Language (XML) called the Document Type Definition.  Context-free grammars are used as basis for compiler design and implementation  Context-free grammars are used as specification mechanisms for programming languages  Designers of compilers use such grammars to implement compiler‟s components, such as scanners, parsers, code generators, code synthesizers  The implementation of almost any programming languages is preceded by a context-free grammar that specifies it. DR.T.P.LATCHOUMI 1/11/2022
  • 46.
    Ambiguous Grammar A CFG isambiguous if one or more terminal strings have multiple leftmost derivations or multiple rightmost derivations or multiple parse trees from the start symbol. DR.T.P.LATCHOUMI 1/11/2022
  • 47.
  • 48.
  • 49.
    CFG Simplification In aCFG, 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 − 1. Removal of Useless Symbols 2. Removal of Unit Productions 3. Removal of Null Productions 4. 1,11,48,58 DR.T.P.LATCHOUMI 1/11/2022
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
    Removal of unit productions Theproductions of the form AB where A and B are both non-terminals is said to a unit production and can be eliminated. DR.T.P.LATCHOUMI 1/11/2022
  • 55.
  • 56.
  • 57.
    Removal of nullproductions  Productions are set of rules in context free grammar that define the language.  A production of the form A  ε is called as epsilon production, or null production.  Using this rule, the appearance of A in the RHS of any production can be nullified.  Epsilon productions are very useful to express many grammars in a compact way. Productions of the form (Aε) is said to be a ε-production that generates empty string are said to be ε- productions. These productions can be eliminated DR.T.P.LATCHOUMI 1/11/2022
  • 58.
  • 59.
  • 60.
  • 61.
    Normal forms forCFG In a Context Free Grammar, the right-hand side of the production can be any string of variables and terminals. When productions in G satisfy certain restrictions, then G is said to be in a Normal Form.  By reducing the grammar, the grammar gets minimized but does not gets standardized.  This is because the RHS of productions have no specific format.  To standardize the grammar, normalization is performed using normal forms. There are two widely useful Normal forms of CFG. They are  Chomsky Normal Form (CNF)  Greibach Normal Form (GNF) DR.T.P.LATCHOUMI 1/11/2022
  • 62.
    Chomsky Normal Form Definition:A context-free grammar G is in Chomsky normal form if any production is of the form: A  BC or A  a where „a‟ is a terminal, A,B,C are non-terminals, and B,C may not be the start variable (the axiom) Note: 1. In CNF number of symbols on right side of production strictly limited. 2. The rule S, where S is the start variable, is not excluded from a CFG in Chomsky normal form. DR.T.P.LATCHOUMI 1/11/2022
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
    Greibach Normal Form(GNF) A CFG G = (V, T, P, S) is said to be in GNF if every production is of the form A  aα, where a € T and α € V*, i.e., α is a string of zero or more variables. Definition: A production U € R is said to be in the form left recursion, if U: A  Aα for some A € V. o Left recursion in R can be eliminated by the following scheme: o If A  Aα1|Aα2| . . . |Aαr|β1| β2| . . . | βs, then replace the above rules by  A  βi| βiZ, 1≤ i ≤ s and  Z αi|αiZ, 1≤ i≤ r o If G = (V, T, P, S) is a CFG, then we can construct another CFG G1 = (V1, T, P1, S) in Greibach Normal Form (GNF) such that L(G1) = L(G) − {}. DR.T.P.LATCHOUMI 1/11/2022
  • 70.
  • 71.
    Example 1 Convert thefollowing grammar G into Greibach Normal Form (GNF). S  XA|BB B  b|SB X  b A  a DR.T.P.LATCHOUMI 1/11/2022
  • 72.
  • 73.
  • 74.
  • 75.
    Example 2 1. S→ XB | AA 2. A → a | SA 3. B → b 4. X → a DR.T.P.LATCHOUMI 1/11/2022
  • 76.
  • 77.
    Difference between CNF andGNF DR.T.P.LATCHOUMI 1/11/2022
  • 78.
    Pumping Lemma forCFL Pumping lemma can be used to check whether the language is context free or not o Let L be a CFL. Then there exists a constant „n‟ such that for every string „z‟ in L such that |z|≥n.‟z‟ can be broken into three substrings z=uvwxy such that: (i) |vx|≥1 (ii) |vwx|≤n and (iii)for all i≥0 uvi wxiy is in L DR.T.P.LATCHOUMI 1/11/2022
  • 79.
  • 80.
  • 81.
    Closure properties forCFL Context-free languages are closed under −  Union  Concatenation  Kleene Star operation DR.T.P.LATCHOUMI 1/11/2022
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.