Introduction To Context-Free Grammars: Deepak D'Souza
Introduction To Context-Free Grammars: Deepak D'Souza
Deepak D’Souza
18 September 2019
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
Outline
1 Intro
2 Examples
3 Formal Definitions
S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
S ⇒ aX
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
S ⇒ aX ⇒ abX
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
S ⇒ aX ⇒ abX ⇒ abb.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
S ⇒ aX ⇒ abX ⇒ abb.
S ⇒ aX ⇒ abX ⇒ abb.
S ⇒ aX ⇒ abX ⇒ abb.
CFG G2
S → aSb
S → .
Example derivation:
S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
CFG G2
S → aSb
S → .
Example derivation:
S ⇒ aSb
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
CFG G2
S → aSb
S → .
Example derivation:
S ⇒ aSb ⇒ aaSbb
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
CFG G2
S → aSb
S → .
Example derivation:
CFG G2
S → aSb
S → .
Example derivation:
CFG G2
S → aSb
S → .
Example derivation:
CFG G3
S → aSa | bSb | a | b | .
Example derivation:
S
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
CFG G3
S → aSa | bSb | a | b | .
Example derivation:
S ⇒ aSa
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
CFG G3
S → aSa | bSb | a | b | .
Example derivation:
S ⇒ aSa ⇒ abSba
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
CFG G3
S → aSa | bSb | a | b | .
Example derivation:
CFG G3
S → aSa | bSb | a | b | .
Example derivation:
CFG G3
S → aSa | bSb | a | b | .
Example derivation:
#( (u) − #) (u)
( ( ( ) ( ) ) ( ) ( ) )
u prefix of w −→
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct
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
L(G ) = {w ∈ A∗ | S ⇒∗G w }.
Leftmost derivations
Leftmost derivations
Leftmost derivations
Leftmost derivations
Leftmost derivations
Leftmost derivations
Leftmost derivations
Parse trees
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
CFG G2
S → aSb
S → .
CFG G4
S → (S) | SS | .
#( (u) − #) (u)
( ( ( ) ( ) ) ( ) ( ) )
u prefix of w −→