Stack machines equiv. to CFG and CFL closure
(Using slides adapted from the book)
Stack machines equiv. to CFG and CFL closure (Using slides adapted - - PowerPoint PPT Presentation
Stack machines equiv. to CFG and CFL closure (Using slides adapted from the book) Simulating DFAs A stack machine can easily simulate any DFA Use the same input alphabet Use the states as stack symbols Use the start state
(Using slides adapted from the book)
DFA's current state
accepting state, the stack machine can end with an empty stack
δ(1,q0) = {q1}
δ(1,q1) = {q3}
δ(1,q2) = {q1}
δ(1,q3) = {q3}
equivalent to any DFA
include, at least, all the regular languages
unbounded stack we barely used
better…
terminal
generated by the given CFG
2. A move for each terminal a ∈ Σ
S → aSa | bSb | ε
S ⇒ aSa⇒ abSba ⇒ abbSbba ⇒ abbbba
(abbbba, S) ↦1 (abbbba, aSa) ↦4 (bbbba, Sa) ↦2 (bbbba, bSba) ↦5 (bbba, Sba) ↦2 (bbba, bSbba) ↦5 (bba, Sbba) ↦3 (bba, bba) ↦5 (ba, ba) ↦5 (a, a) ↦4 (ε, ε)
S → aSa | bSb | ε
powerful as CFGs for defining languages
define languages that are not CFLs?
productions
the CFG
machine
(abab, S) ↦1 (bab, 0S) ↦3 (ab, S) ↦1 (b, 0S) ↦3 (ε, S) ↦7 (ε, ε)
S ⇒1 a0S ⇒3 abS ⇒1 aba0S ⇒3 ababS ⇒7 abab
P = {(A→ωt) | A ∈ Γ, ω ∈ Σ∪{ε}, and t ∈ δ(ω,A)}
S ⇒* xy if and only if (x,S) ↦* (ε,y) for any x ∈ Σ* and y ∈ Γ* (see the next lemma)
S ⇒* x if and only if (x,S) ↦* (ε,ε)
If M = (Γ, Σ, S, δ) is any stack machine, there is context-free grammar G with L(G) = L(M).
nonterminals in the CFG
grammar must be either terminal or nonterminal, not both
can easily rename stack machine symbols to get disjoint alphabets…
simulate the executions of M
S ⇒* xy if and only if (x,S) ↦* (ε,y) for any x ∈ Σ* and y ∈ Γ*
we'll bite the bullet and fill in the details
derivation
string of nonterminals
y' ∈ Γ*…
For the construction of the proof of Lemma 13.8.1, for any x ∈ Σ* and y ∈ Γ*, S ⇒* xy if and only if (x,S) ↦* (ε,y).
(x,S) = (x'ω,S) ↦* (ω,Ay') ↦ (ε,ty') = (ε,y)
For the construction of the proof of Lemma 13.8.1, for any x ∈ Σ* and y ∈ Γ*, S ⇒* xy if and only if (x,S) ↦* (ε,y).
by induction on the number of steps in the derivation
execution For the construction of the proof of Lemma 13.8.1, for any x ∈ Σ* and y ∈ Γ*, S ⇒* xy if and only if (x,S) ↦* (ε,y).
A language is context free if and only if it is L(M) for some stack machine M.
and L(G2) = L2
because symbols could be renamed)
If L1 and L2 are any context-free languages, L1 ∪ L2 is also context free.
and L(G2) = L2
because symbols could be renamed)
If L1 and L2 are any context-free languages, L1L2 is also context free. almost the same proof!
L* = {x1x2 ... xn | n ≥ 0, with all xi ∈ L}
If L is any context-free language, L* is also context free.
machine uses Γ × Q as its stack alphabet
If L1 is any context-free language and L2 is any regular language, then L1 ∩ L2 is context free.
machine uses Γ × Q as its stack alphabet
Why doesn’t this work for intersection with a CF language L2? If L1 is any context-free language and L2 is any regular language, then L1 ∩ L2 is context free.
languages
The CFLs are not closed for intersection. S1 → A1B1 A1 → aA1b | ε B1 → cB1 | ε S2 → A2B2 A2 → aA2 | ε B2 → bB2c | ε
for complement
The CFLs are not closed for complement.