Context-Free Languages &
Grammars (CFLs & CFGs)
10/10/2022 [Link] AP(Sr. Gr.)/CSE 1
Context-Free Languages
■ A language class larger than the class of regular
languages
■ Supports recursive notation called “context-free
grammar”
■ Applications:
■ Parse trees, compilers Context-
Regular free
■ XML
(FA/RE) (PDA/CFG)
10/10/2022 [Link] AP(Sr. Gr.)/CSE 2
An Example
■ A palindrome is a word that reads identical from both
ends
■ E.g., madam, redivider, malayalam, 010010010
■ Let L = { w | w is a binary palindrome}
■ Is L regular?
■ No.
■ Proof:
■ Let w=0N10N (assuming N to be the p/l constant)
■ By Pumping lemma, w can be rewritten as xyz, such that xykz is also L
(for any k≥0)
■ But |xy|≤N and y≠ε
■ ==> y=0+
■ ==> xykz will NOT be in L for k=0
■ ==> Contradiction
10/10/2022 [Link] AP(Sr. Gr.)/CSE 3
But the language of
palindromes…
is a CFL, because it supports recursive
substitution (in the form of a CFG)
■ This is because we can construct a
“grammar” like this:
Same as:
1. A ==> ε A => 0A0 | 1A1 | 0 | 1 |
Terminal
2. A ==> 0 ε
3. A ==> 1
Variable or non-terminal
Productions 4. A ==> 0A0
5. A ==> 1A1
10/10/2022 [Link] AP(Sr. Gr.)/CSE 4
How does the CFG for
palindromes work?
An input string belongs to the language (i.e.,
accepted) iff it can be generated by the CFG
G:
■ Example: w=01110 A => 0A0 | 1A1 | 0 | 1 |
■ G can generate w as follows: ε
Generating a string from a grammar:
1. A => 0A0 1. Pick and choose a sequence
2. => 01A10 of productions that would
3. => 01110 allow us to generate the
string.
2. At every step, substitute one variable
with one of its productions.
10/10/2022 [Link] AP(Sr. Gr.)/CSE 5
Context-Free Grammar:
Definition
■ A context-free grammar G=(V,T,P,S), where:
■ V: set of variables or non-terminals
■ T: set of terminals (= alphabet U {ε})
■ P: set of productions, each of which is of the form
V ==> α1 | α2 | …
■ Where each αi is an arbitrary string of variables and
terminals
■ S ==> start variable
CFG for the language of binary palindromes:
G=({A},{0,1},P,A)
P: A ==> 0 A 0 | 1 A 1 | 0 | 1 | ε
10/10/2022 [Link] AP(Sr. Gr.)/CSE 6
More examples
■ Parenthesis matching in code
■ Syntax checking
10/10/2022 [Link] AP(Sr. Gr.)/CSE 7
Example #2
■ Language of balanced paranthesis
e.g., ()(((())))((()))….
■ CFG?
G:
S => (S) | SS | ε
10/10/2022 [Link] AP(Sr. Gr.)/CSE 8
Example #3
■ A grammar for L = {0m1n | m≥n}
■ CFG? G:
S => 0S1 | A
A => 0A | ε
10/10/2022 [Link] AP(Sr. Gr.)/CSE 9
Example #4
A program containing if-then(-else) statements
if Condition then Statement else Statement
(Or)
if Condition then Statement
CFG?
G:
S => iCtSeS | iCtS | a
C => b
10/10/2022 [Link] AP(Sr. Gr.)/CSE 10
Applications of CFLs & CFGs
■ Compilers use parsers for syntactic checking
■ Parsers can be expressed as CFGs
1. Balancing paranthesis
2. If-then-else
3. C paranthesis matching
4. Pascal begin-end matching
5. YACC (Yet Another Compiler-Compiler)
10/10/2022 [Link] AP(Sr. Gr.)/CSE 11
More applications
■ Markup languages
■ Nested Tag Matching
■ HTML
■ <html> …<p> … <a href=…> … </a> </p> … </html>
■ XML
■ <PC> … <MODEL> … </MODEL> .. <RAM> …
</RAM> … </PC>
10/10/2022 [Link] AP(Sr. Gr.)/CSE 12
Tag-Markup Languages
Roll ==> <ROLL> Class Students </ROLL>
Class ==> <CLASS> Text </CLASS>
Text ==> Char Text | Char
Char ==> a | b | … | z | A | B | .. | Z
Students ==> Student Students | ε
Student ==> <STUD> Text </STUD>
Here, the left hand side of each production denotes one non-terminals
(e.g., “Roll”, “Class”, etc.)
Those symbols on the right hand side for which no productions (i.e.,
substitutions) are defined are terminals (e.g., „a‟, „b‟, „|‟, „<„, „>‟, “ROLL”,
etc.)
10/10/2022 [Link] AP(Sr. Gr.)/CSE 13
Structure of a production
head derivation body
A =======> α 1 | α 2 | … | αk
The above is same as:
1. A ==> α1
2. A ==> α2
3. A ==> α3
…
K. A ==> αk
10/10/2022 [Link] AP(Sr. Gr.)/CSE 14
CFG conventions
■ Terminal symbols <== a, b, c…
■ Non-terminal symbols <== A,B,C, …
■ Terminal or non-terminal symbols <== X,Y,Z
■ Terminal strings <== w, x, y, z
■ Arbitrary strings of terminals and non-
terminals <== α, β, γ, ..
10/10/2022 [Link] AP(Sr. Gr.)/CSE 15
String membership
How to say if a string belong to the language
defined by a CFG?
1. Derivation
■ Head to body
Both are equivalent forms
2. Recursive inference
■ Body to head
G:
Example: A => 0A0 | 1A1 | 0 | 1 |
■ w = 01110 ε
A => 0A0
■ Is w a palindrome? => 01A10
=> 01110
10/10/2022 [Link] AP(Sr. Gr.)/CSE 16
Simple Expressions…
■ We can write a CFG for accepting simple
expressions
■ G = (V,T,P,S)
■ V = {E,F}
■ T = {0,1,a,b,+,*,(,)}
■ S = {E}
■ P:
■ E ==> E+E | E*E | (E) | F
■ F ==> aF | bF | 0F | 1F | a | b | 0 | 1
10/10/2022 [Link] AP(Sr. Gr.)/CSE 17
Generalization of derivation
■ Derivation is head ==> body
■ A==>X (A derives X in a single step)
■ A ==>*G X (A derives X in a multiple steps)
■ Transitivity:
IFA ==>*GB, and B ==>*GC, THEN A ==>*G C
10/10/2022 [Link] AP(Sr. Gr.)/CSE 18
Context-Free Language
■ The language of a CFG, G=(V,T,P,S),
denoted by L(G), is the set of terminal
strings that have a derivation from the
start variable S.
■ L(G) = { w in T* | S ==>*G w }
10/10/2022 [Link] AP(Sr. Gr.)/CSE 19
Left-most & Right-most
G:
Derivation Styles EF =>
=> E+E | E*E | (E) | F
aF | bF | 0F | 1F | ε
Derive the string a*(ab+10) from G: E =*=>G a*(ab+10)
■E ■E
■==> E * E ■==> E * E
■==> F * E ■==> E * (E)
Left-most Right-most
■==> aF * E ■==> E * (E + E)
derivation: ■==> a * E ■==> E * (E + F) derivation:
■==> a * (E) ■==> E * (E + 1F)
Always ■==> a * (E + E) ■==> E * (E + 10F) Always
substitute ■==> a * (F + E) ■==> E * (E + 10) substitute
leftmost ■==> a * (aF + E) ■==> E * (F + 10)
rightmost
variable ■==> a * (abF + E) ■==> E * (aF + 10)
variable
■==> a * (ab + E) ■==> E * (abF + 0)
■==> a * (ab + F) ■==> E * (ab + 10)
■==> a * (ab + 1F) ■==> F * (ab + 10)
■==> a * (ab + 10F) ■==> aF * (ab + 10)
■==> a * (ab + 10) ■==> a * (ab + 10)
10/10/2022 [Link] AP(Sr. Gr.)/CSE 20
Gpal:
CFG & CFL A => 0A0 | 1A1 | 0 | 1 | ε
■ Theorem: A string w in (0+1)* is in
L(Gpal), if and only if, w is a palindrome.
■ Proof:
■ Use induction
■ On string length for the IF part
■ On length of derivation for the ONLY IF part
10/10/2022 [Link] AP(Sr. Gr.)/CSE 21
Parse trees
10/10/2022 [Link] AP(Sr. Gr.)/CSE 22
Parse Trees
■ Each CFG can be represented using a parse tree:
■ Each internal node is labeled by a variable in V
■ Each leaf is terminal symbol
■ For a production, A==>X1X2…Xk, then any internal node
labeled A has k children which are labeled from X1,X2,…Xk
from left to right
Parse tree for production and all other subsequent productions:
A ==> X1..Xi..Xk A
X … X … X
1 i k
10/10/2022 [Link] AP(Sr. Gr.)/CSE 23
Examples
E
Recursive inference
A
E + E
0 A 0
F F
1 A 1
Derivatio
a 1
ε
n
Parse tree for 0110
Parse tree for a + 1
G: G:
E => E+E | E*E | (E) | F A => 0A0 | 1A1 | 0 | 1 |
F => aF | bF | 0F | 1F | 0 | 1 | a | b ε
10/10/2022 [Link] AP(Sr. Gr.)/CSE 24
Parse Trees, Derivations, and
Recursive Inferences
Production:
A ==> X1..Xi..Xk
A
Recursive
X … X … X
Derivatio
inference
1 i k
n
Left-most Parse tree
derivation
Derivation Right-most
Recursive
derivation
inference
10/10/2022 [Link] AP(Sr. Gr.)/CSE 25
Interchangeability of different
CFG representations
■ Parse tree ==> left-most derivation
■ DFS left to right
■ Parse tree ==> right-most derivation
■ DFS right to left
■ Derivation ==> Recursive inference
■ Reverse the order of productions
■ Recursive inference ==> Parse trees
■ bottom-up traversal of parse tree
10/10/2022 [Link] AP(Sr. Gr.)/CSE 26
CFLs & Regular Languages
■ A CFG is said to be right-linear if all the
productions are one of the following two
forms: A ==> wB (or) A ==> w
Where:
• A & B are variables, w is a string of terminals
■ Theorem 1: Every right-linear CFG generates
a regular language
■ Theorem 2: Every regular language has a
right-linear grammar
■ Theorem 3: Left-linear CFGs also represent
RLs
10/10/2022 [Link] AP(Sr. Gr.)/CSE 27
Ambiguity in CFGs and CFLs
10/10/2022 [Link] AP(Sr. Gr.)/CSE 28
Ambiguity in CFGs
■ A CFG is said to be ambiguous if there
exists a string which has more than one
left-most derivation / right-most
derivation / parse tree
Example: LM derivation #1: LM derivation #2:
S ==> AS | ε S => AS S => AS
=> 0A1S => A1S
A ==> A1 | 0A1 | => 0A11S
01 =>0A11S
=> 00111S => 00111S
=> 00111 => 00111
Input string: 00111 Can be derived in two ways
10/10/2022 [Link] AP(Sr. Gr.)/CSE 29
Why does ambiguity matter?
E ==> E + E | E * E | (E) | a | b | c | 0 | 1
string = a * b + c
E
• LM derivation #1:
• E => E + E => E * E + E E + E
==>* a * b + c
E * E c
a b
E
• LM derivation #2
• E => E * E => a * E => E * E
a * E + E ==>* a * b + c
a E + E
b c
10/10/2022 [Link] AP(Sr. Gr.)/CSE 30
Removing Ambiguity in
Expression Evaluations
■ It MAY be possible to remove ambiguity for
some CFLs
■ E.g.,, in a CFG for expression evaluation by
imposing rules & restrictions such as precedence
■ This would imply rewrite of the grammar
Modified unambiguous version:
■ Precedence: (), * , + E => E + T | T
T => T * F | F
Ambiguous version: F => I | (E)
I => a | b | c | 0 | 1
E ==> E + E | E * E | (E) | a | b | c | 0 | 1
10/10/2022 [Link] AP(Sr. Gr.)/CSE 31
Inherently Ambiguous CFLs
■ A CFL is said to be inherently ambiguous if
every CFG that describes it is ambiguous
Example:
■ L = { anbncmdm | n,m≥ 1} U {anbmcmdn | n,m≥ 1}
■ L is inherently ambiguous
for a language for which no unambiguous grammar is possible
is inherent ambiguous language
10/10/2022 [Link] AP(Sr. Gr.)/CSE 32
Inherently Ambiguous CFLs
10/10/2022 [Link] AP(Sr. Gr.)/CSE 33
Inherently Ambiguous CFLs
10/10/2022 [Link] AP(Sr. Gr.)/CSE 34
Summary
■ Context-free grammars
■ Context-free languages
■ Productions, derivations, recursive inference,
parse trees
■ Left-most & right-most derivations
■ Ambiguous grammars
■ Removing ambiguity
■ CFL/CFG applications
■ parsers, markup languages
10/10/2022 [Link] AP(Sr. Gr.)/CSE 35
Check whether the given
grammar is ambiguous or not
● S-> aS |Sa| ε
● E-> E +E | E*E| id
● A -> AA | (A) | a
● S -> SS|AB , A -> Aa|a , B -> Bb|b
● S -> (L) | a, L -> LS | S
● S -> AA , A -> aA , A -> b
10/10/2022 [Link] AP(Sr. Gr.)/CSE 36