introduction to context-free grammarsdeepakd/atc-2019/cfg-intro.pdf · 2019. 9. 18. ·...

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

Upload: others

Post on 31-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 AutomationIndian Institute of Science, Bangalore.

18 September 2019

Page 2: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Page 3: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 callsCan model other infinite-state systems.

Easier to prove properties of Pushdown languages usingCFG’s:

Pumping lemmaUltimate periodicityPDA = PDA without ε-transitions.

Parsing algo leads to solution to “CFL reachability” problem:Given a finite A-labelled graph, a CFG G , are two givenvertices u and v connected by a path whose label is in L(G ).

Page 4: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S

⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .What is the language defined by G1 above? a(a + b)∗b.

Page 5: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S ⇒ aX

⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .What is the language defined by G1 above? a(a + b)∗b.

Page 6: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S ⇒ aX ⇒ abX

⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .What is the language defined by G1 above? a(a + b)∗b.

Page 7: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .What is the language defined by G1 above? a(a + b)∗b.

Page 8: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .

What is the language defined by G1 above? a(a + b)∗b.

Page 9: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .What is the language defined by G1 above?

a(a + b)∗b.

Page 10: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 1

CFG G1

S → aXX → aXX → bXX → b

Derivation of a string: Begin with S and keep rewriting the currentstring by replacing a non-terminal by its RHS in a production ofthe grammar.Example derivation:

S ⇒ aX ⇒ abX ⇒ abb.

Language defined by G , written L(G ), is the set of all terminalstrings that can be generated by G .What is the language defined by G1 above? a(a + b)∗b.

Page 11: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 2

CFG G2

S → aSbS → ε.

Example derivation:

S

⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

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

Page 12: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 2

CFG G2

S → aSbS → ε.

Example derivation:

S ⇒ aSb

⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

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

Page 13: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 2

CFG G2

S → aSbS → ε.

Example derivation:

S ⇒ aSb ⇒ aaSbb

⇒ aaaSbbb ⇒ aaabbb.

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

Page 14: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 2

CFG G2

S → aSbS → ε.

Example derivation:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

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

Page 15: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 2

CFG G2

S → aSbS → ε.

Example derivation:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

What is the language defined by G2 above?

{anbn | n ≥ 0}.

Page 16: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Context-Free Grammars: Example 2

CFG G2

S → aSbS → ε.

Example derivation:

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb.

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

Page 17: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 = wR}.

Page 18: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 = wR}.

Page 19: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 = wR}.

Page 20: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 = wR}.

Page 21: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 = wR}.

Page 22: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 = wR}.

Page 23: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 24: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 25: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 26: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 27: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 28: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 29: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 30: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 31: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 32: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 33: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 34: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 35: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 36: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 37: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 38: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 39: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 40: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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), andfor each prefix u of w , #((u) ≥ #)(u).

Page 41: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 prefix of w −→

#((u)−#)(u)

ε

Page 42: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 ofproductions or rules. Productions are written as

X → α.

Page 43: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Derivations, language etc.

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

First define αn⇒ β inductively:

α1⇒ β iff α is of the form α1Xα2 and X → γ is a production

in P, and β = α1γα2.

αn+1⇒ β iff there exists γ such that α

n⇒ γ and γ1⇒ β.

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 aCFG G such that L = L(G ).

Page 44: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S

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

Page 45: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S ⇒ (S)

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

Page 46: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S ⇒ (S)⇒ (SS)

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

Page 47: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S ⇒ (S)⇒ (SS)⇒ ((S)S)

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

Page 48: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S ⇒ (S)⇒ (SS)⇒ ((S)S)⇒ (()S)

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

Page 49: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S ⇒ (S)⇒ (SS)⇒ ((S)S)⇒ (()S)⇒ (()(S))

⇒ (()())

Page 50: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Leftmost derivations

A leftmost derivation in G is a derivation sequence in whichat each step the leftmost non-terminal in the sentential formis re-written.

Example:S ⇒ (S)⇒ (SS)⇒ ((S)S)⇒ (()S)⇒ (()(S))⇒ (()())

Page 51: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Parse trees

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

Derivation represented as parse tree:

S

S( )

( )S ( )S

S S

ε ε

Sentential form can be read off from the leaves of the parsetree in a left-to-right manner.

Leftmost derivations and parse trees represent eachother.

Page 52: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Proving that a CFG accepts a certain language

CFG G1

S → aXX → aXX → bXX → 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 formsrather than just terminal strings.

Eg: “P(n): If Sn⇒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 ⇒∗

G1auX .”

Page 53: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Proving that a CFG accepts a certain language

CFG G1

S → aXX → aXX → bXX → 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 formsrather than just terminal strings.

Eg: “P(n): If Sn⇒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 ⇒∗

G1auX .”

Page 54: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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

Proving that a CFG accepts a certain language

CFG G2

S → aSbS → ε.

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

Page 55: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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.

Page 56: Introduction to Context-Free Grammarsdeepakd/atc-2019/CFG-intro.pdf · 2019. 9. 18. · Introduction to Context-Free Grammars Deepak D’Souza Department of Computer Science and

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 prefix of w −→

#((u)−#)(u)

ε