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
56
Embed
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 ...
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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 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 ).
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.
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.
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.
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.
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.
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.
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.
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}.
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}.
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}.
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}.
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}.
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}.
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}.
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}.
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}.
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}.
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}.
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}.
Intro Examples Formal Definitions Leftmost derivation and parse trees Proving grammars correct