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

Introduction To Context-Free Grammars: Deepak D'Souza

The document provides an introduction to context-free grammars. It outlines the topics to be covered, which include examples of context-free grammars, formal definitions, leftmost derivations and parse trees, and proving grammars correct. It then gives examples of three context-free grammars and derives sample strings to illustrate how they work.

Uploaded by

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

Introduction To Context-Free Grammars: Deepak D'Souza

The document provides an introduction to context-free grammars. It outlines the topics to be covered, which include examples of context-free grammars, formal definitions, leftmost derivations and parse trees, and proving grammars correct. It then gives examples of three context-free grammars and derives sample strings to illustrate how they work.

Uploaded by

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

Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Introduction to Context-Free Grammars

Deepak D’Souza

Department of Computer Science and Automation


Indian Institute of Science, Bangalore.

18 September 2019
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Outline

1 Intro

2 Examples

3 Formal Definitions

4 Leftmost derivation and parse trees

5 Proving grammars correct


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Why study Context-Free Grammars?

Arise naturally in syntax of programming languages, parsing,


compiling.
Characterize languages accepted by Pushdown automata.
Pushdown automata are important class of system models:
They can model programs with procedure calls
Can model other infinite-state systems.
Easier to prove properties of Pushdown languages using
CFG’s:
Pumping lemma
Ultimate periodicity
PDA = PDA without -transitions.
Parsing algo leads to solution to “CFL reachability” problem:
Given a finite A-labelled graph, a CFG G , are two given
vertices u and v connected by a path whose label is in L(G ).
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S ⇒ aX
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S ⇒ aX ⇒ abX
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminal


strings that can be generated by G .
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminal


strings that can be generated by G .
What is the language defined by G1 above?
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 1


CFG G1
S → aX
X → aX
X → bX
X → b

Derivation of a string: Begin with S and keep rewriting the current


string by replacing a non-terminal by its RHS in a production of
the grammar.
Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminal


strings that can be generated by G .
What is the language defined by G1 above? a(a + b)∗ b.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 2

CFG G2
S → aSb
S → .

Example derivation:

S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 2

CFG G2
S → aSb
S → .

Example derivation:

S ⇒ aSb
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 2

CFG G2
S → aSb
S → .

Example derivation:

S ⇒ aSb ⇒ aaSbb
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 2

CFG G2
S → aSb
S → .

Example derivation:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 2

CFG G2
S → aSb
S → .

Example derivation:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

What is the language defined by G2 above?


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 2

CFG G2
S → aSb
S → .

Example derivation:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

What is the language defined by G2 above? {an b n | n ≥ 0}.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 3

CFG G3
S → aSa | bSb | a | b | .

Example derivation:

S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 3

CFG G3
S → aSa | bSb | a | b | .

Example derivation:

S ⇒ aSa
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 3

CFG G3
S → aSa | bSb | a | b | .

Example derivation:

S ⇒ aSa ⇒ abSba
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 3

CFG G3
S → aSa | bSb | a | b | .

Example derivation:

S ⇒ aSa ⇒ abSba ⇒ abbSbba ⇒ abbbba.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 3

CFG G3
S → aSa | bSb | a | b | .

Example derivation:

S ⇒ aSa ⇒ abSba ⇒ abbSbba ⇒ abbbba.

What is the language defined by G3 above?


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 3

CFG G3
S → aSa | bSb | a | b | .

Example derivation:

S ⇒ aSa ⇒ abSba ⇒ abbSbba ⇒ abbbba.

What is the language defined by G3 above? Palindromes:


{w ∈ {a, b}∗ | w = w R }.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
⇒ ((()())(S)S)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
⇒ ((()())(S)S)
⇒ ((()())()S)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
⇒ ((()())(S)S)
⇒ ((()())()S)
⇒ ((()())()(S))
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
⇒ ((()())(S)S)
⇒ ((()())()S)
⇒ ((()())()(S))
⇒ ((()())()())
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
⇒ ((()())(S)S)
⇒ ((()())()S)
⇒ ((()())()(S))
⇒ ((()())()())

What is the language defined by G4 above?


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Context-Free Grammars: Example 4


CFG G4
S → (S) | SS | .

Exercise: Derive “((()())()())”.


S ⇒ (S)
⇒ (SS)
⇒ (SSS)
⇒ ((S)SS)
⇒ ((SS)SS)
⇒ (((S)S)SS)
⇒ ((()S)SS)
⇒ ((()(S))SS)
⇒ ((()())SS)
⇒ ((()())(S)S)
⇒ ((()())()S)
⇒ ((()())()(S))
⇒ ((()())()())

What is the language defined by G4 above?


Balanced Parenthesis: w ∈ {(, )}∗ such that
#( (w ) = #) (w ), and
for each prefix u of w , # (u) ≥ # (u).
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Visualizing balanced parenthesis

Balanced Parenthesis: w ∈ {(, )}∗ such that


#( (w ) = #) (w ), and
for each prefix u of w , #( (u) ≥ #) (u).

#( (u) − #) (u)

 ( ( ( ) ( ) ) ( ) ( ) )
u prefix of w −→
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

CFG’s more formally

A Context-Free Grammar (CFG) is of the form

G = (N, A, S, P)

where
N is a finite set of non-terminal symbols
A is a finite set of terminal symbols.
S ∈ N is the start non-terminal symbol.
P is a finite subset of N × (N ∪ A)∗ , called the set of
productions or rules. Productions are written as

X → α.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Derivations, language etc.

“α derives β in 0 or more steps”: α ⇒∗G β.


n
First define α ⇒ β inductively:
1
α ⇒ β iff α is of the form α1 X α2 and X → γ is a production
in P, and β = α1 γα2 .
n+1 n 1
α ⇒ β iff there exists γ such that α ⇒ γ and γ ⇒ β.
Sentential form of G : any α ∈ (N ∪ A)∗ such that S ⇒∗G α.
Language defined by G :

L(G ) = {w ∈ A∗ | S ⇒∗G w }.

L ⊆ A∗ is called a Context-Free Language (CFL) if there is a


CFG G such that L = L(G ).
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S ⇒ (S)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S ⇒ (S)
⇒ (SS)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S ⇒ (S)
⇒ (SS)
⇒ ((S)S)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S ⇒ (S)
⇒ (SS)
⇒ ((S)S)
⇒ (()S)
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S ⇒ (S)
⇒ (SS)
⇒ ((S)S)
⇒ (()S)
⇒ (()(S))
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Leftmost derivations

A leftmost derivation in G is a derivation sequence in which


at each step the leftmost non-terminal in the sentential form
is re-written.
Example:
S ⇒ (S)
⇒ (SS)
⇒ ((S)S)
⇒ (()S)
⇒ (()(S))
⇒ (()())
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Parse trees

Derivation represented as parse tree:


S
S ⇒ (S)
⇒ (SS)
( S )
⇒ ((S)S)
⇒ (()S)
S S
⇒ (()(S))
⇒ (()())
( S ) ( S )

 

Sentential form can be read off from the leaves of the parse
tree in a left-to-right manner.
Leftmost derivations and parse trees represent eachother.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Proving that a CFG accepts a certain language


CFG G1
S → aX
X → aX
X → bX
X → b

Prove that L(G1 ) = a(a + b)∗ b.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Proving that a CFG accepts a certain language


CFG G1
S → aX
X → aX
X → bX
X → b

Prove that L(G1 ) = a(a + b)∗ b.


Show that L(G1 ) ⊆ L(a(a + b)∗ b), and L(a(a + b)∗ b) ⊆ L(G1 ).
Use induction statement that talks about sentential forms
rather than just terminal strings.
n
Eg: “P(n): If S ⇒G1 α then α is of the form S, auX , or aub.”
Follows that all terminal sentential forms are of the form
“aub” ∈ L(a(a + b)∗ b).
For L(a(a + b)∗ b) ⊆ L(G1 ) use induction statement “If
|u| = n then S ⇒∗G1 auX .”
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Proving that a CFG accepts a certain language

CFG G2
S → aSb
S → .

Prove that L(G2 ) = {an b n | n ≥ 0}.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Proving that a CFG accepts a certain language

CFG G4
S → (S) | SS | .

Prove that L(G4 ) = BP.


Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct

Visualizing balanced parenthesis

Balanced Parenthesis: w ∈ {(, )}∗ such that


#( (w ) = #) (w ), and
for each prefix u of w , #( (u) ≥ #) (u).

#( (u) − #) (u)

 ( ( ( ) ( ) ) ( ) ( ) )
u prefix of w −→

You might also like