context free languages - university of torontoazadeh/page11/page12/material/cfg.pdfidea remember...
TRANSCRIPT
IDEA
Remember ”balanced parantheses” example from structural induction?
S =�
�(S)S
S −→ (S)S | �
If we write it this way:
IDEA
Remember ”balanced parantheses” example from structural induction?
S =�
�(S)S
it is a context-free grammar generating a context-free language con-taining all balanced parantheses strings.
S −→ (S)S | �
If we write it this way:
CONTEXT-FREE GRAMMARS
A context-free grammar is a tuple G = (V, T, P, S) where
• V is finite set of variables or nonterminals.
• T is a finite set of terminals.
• V ∩ T = ∅
• S ∈ V is a unique start symbol.
• P is a finite set of productions (rules).
CONTEXT-FREE GRAMMARS
A context-free grammar is a tuple G = (V, T, P, S) where
• V is finite set of variables or nonterminals.
• T is a finite set of terminals.
• V ∩ T = ∅
• S ∈ V is a unique start symbol.
• P is a finite set of productions (rules).
A −→ α
CONTEXT-FREE GRAMMARS
A context-free grammar is a tuple G = (V, T, P, S) where
• V is finite set of variables or nonterminals.
• T is a finite set of terminals.
• V ∩ T = ∅
• S ∈ V is a unique start symbol.
• P is a finite set of productions (rules).
A −→ α A ∈ Vα ∈ (V ∪ T )∗
EXAMPLE
G = ({S}, {0, 1}, , S)
S −→ 0
S −→ 1
S −→ �
S −→ 0S0
S −→ 1S1
Generates Palindromes!
S −→ 0S0 | 1S1 | � | 1 | 0
DERIVATIONS
Let α1, α2 ∈ (V ∪ T )∗, we say:
α1 =⇒G α2
If:
∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗
such that
α1 = βAγ α2 = βλγ
DERIVATIONS
Let α1, α2 ∈ (V ∪ T )∗, we say:
α1 =⇒G α2
If:
∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗
such that
α1 = βAγ α2 = βλγ
A −→ λ
And
is in P
DERIVATIONS
Let α1, α2 ∈ (V ∪ T )∗, we say:
α1 =⇒G α2
If:
∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗
such that
α1 = βAγ α2 = βλγ
A −→ λ
And
is in P
α2 can be derived from α1 in a single production application.
LANGUAGE OF A GRAMMAR
Let =⇒∗G be reflexive and transitive closure of =⇒G.
α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)
of applications of productions in P
LANGUAGE OF A GRAMMAR
Let =⇒∗G be reflexive and transitive closure of =⇒G.
α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)
of applications of productions in P
L(G) = {w : w ∈ T ∗ and S =⇒∗G w}
LANGUAGE OF A GRAMMAR
Let =⇒∗G be reflexive and transitive closure of =⇒G.
α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)
of applications of productions in P
L(G) = {w : w ∈ T ∗ and S =⇒∗G w}
The language generated by G is all strings of terminals derivable fromthe start symbol.
LANGUAGE OF A GRAMMAR
Let =⇒∗G be reflexive and transitive closure of =⇒G.
α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)
of applications of productions in P
L(G) = {w : w ∈ T ∗ and S =⇒∗G w}
The language generated by G is all strings of terminals derivable fromthe start symbol.
Context-Free Languages = {L : ∃ grammar (CFG) G s.t. L(G) = L}