270 likes | 380 Vues
This article delves into the inherent limitations of computer programs using context-free grammars (CFGs) and pushdown automata (PDAs). It begins with the definitions and constructions of languages generated by CFGs, such as those of balanced parentheses and specific string forms. The relationship between CFGs and PDAs highlights how a language's generation via CFG equates to its recognition by PDAs. With practical examples and the derivation of strings, the discussion elucidates the mechanisms of stack operations and the critical importance of these theoretical frameworks in understanding computational limits.
E N D
CSci 4011 INHERENT LIMITATIONS OF COMPUTER PROGAMS
string pop push ε,ε → $ 0,ε→ 0 1,0→ε ε,$ → ε 1,0→ε The language of P is the set of strings it accepts.
CONTEXT-FREE GRAMMARS A → 0A1 A → B B → # A 0A1 00A11 00B11 00#11 A derives 00#11 in 4 steps. The language of G is the set of strings derived by S.
GIVE A CFG FOR… L1 = { strings of balanced parens } L2 = { aibjak | i, j, k ≥ 0 and k = i + j }
VERY INTERESTINGCFGs… http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html http://docs.python.org/reference/grammar.html
COMPILER MODULES LEXER PARSER SEMANTIC ANALYZER TRANSLATOR/INTERPRETER
PARSE TREES A A A B 0 0 # 1 1 A 0A1 00A11 00B11 00#11
Suppose L is generated by a CFG G = (V, Σ, R, S) Construct P = (Q, Σ, Γ, , q, F) that recognizes L A Language is generated by a CFG It is recognized by a PDA Idea: P will derive w ∈L on its stack.
(1) Place the marker symbol $ and the start variable on the stack Suppose L is generated by a CFG G = (V, Σ, R, S) Construct P = (Q, Σ, Γ, , q, F) that recognizes L (2) Repeat forever: (a) If v is in the stack, and (v → s) ∈ R, push s on the stack (b) If stack is a string, goto (3) (3) Loop until stack is empty: (a) if top of stack matches input, pop. (b) on (ε,$), accept.
Suppose L is generated by a CFG G = (V, Σ, R, S) Construct P = (Q, Σ, Γ, , q, F) that recognizes L (qstart) Push S$ and go to qloop (qloop) Repeat the following steps forever: (a) On (ε,v) where (v → s) ∈ R, push s and go to qloop (b) On (,), pop and go to qloop (c) On (ε,$) go to qaccept (else) get stuck!
ε,ε → S$ ε,A → w for rule A → w a,a → ε for terminal a ε,$ → ε
S → aTb T → Ta | ε ε,ε → $ ε,ε → T ε,S → b ε,ε → T ε,ε → S ε,T → a ε,$ → ε ε,ε → a ε,T → ε a,a → ε b,b → ε
A Language is generated by a CFG It is recognized by a PDA
A Language is generated by a CFG It is recognized by a PDA Given PDA P = (Q, Σ, Γ, , q, F) Construct a CFG G = (V, Σ, R, S) such that L(G)=L(P) First, simplify P so that: (1) It has a single accept state, qaccept (2) It empties the stack before accepting (3) Each transition either pushes a symbol or pops a symbol, but not both
SIMPLIFY ε,ε → $ 0,ε→ 0 q1 q0 1,0→ε ε,ε → 0 ε,ε → ε ε,$ → ε q2 q3 1,0→ε ε,ε → 0 ε,ε → ε q4 ε,0 → ε q5
Idea: for each pair of states p and q in P, the grammar will have a variable Apq that generates all strings that that can take P from p to q without changing the stack* V = {Apq | p,qQ } S = Aq0qaccept *starting from any stack S in p, P has stack S at q.
ε,ε → $ 0,ε→ 0 q1 q0 1,0→ε ε,ε → 0 ε,$ → ε q2 q3 1,0→ε ε,ε → 0 q4 ε,0 → ε q5 none What strings does Aq0q1 generate? {0n1n | n > 0} What strings does Aq1q2 generate? What strings does Aq1q3 generate? none
Apq generates all strings that take P from p to q without changing the stack Let x be such a string • P’s first move on x must be a push • P’s last move on x must be a pop Consider the stack while reading x. Either: 1. The first repeat comes at the end of x 2. The stack repeats before the end of x
1. The first repeat is at the end of x: stack height r s a b input string p q Apq→ aArsb
2. The stack repeats before the end of x: stack height input string p r q Apq→ AprArq
Formally: V = {Apq | p,qQ } S = Aq0qaccept For each p,q,r,s Q, t Γ and a,b Σε If (r,t) (p,a,ε) and (q, ε) (s,b,t) Then add the rule Apq→ aArsb For each p,q,r Q, add the rule Apq→ AprArq For each p Q, add the rule App→ ε
ε,ε → $ 0,ε→ 0 q1 q0 1,0→ε ε,ε → 0 ε,$ → ε q2 q3 1,0→ε ε,ε → 0 q4 ε,0 → ε q5 Aqq→ ε Apq→ AprArq Aq0q3 → εAq1q2ε Aq1q2 → 0Aq1q21 Aq1q2 → 0Aq1q11 none What strings does Aq0q1 generate? {0n1n | n > 0} What strings does Aq1q2 generate? What strings does Aq1q3 generate? none
Apq generates x x can bring P from p to q without changing the stack Proof (by induction on the number of steps in the derivation of x from Apq): Inductive Step: Base Case: The derivation has 1 step: Appε Assume true for derivations of length at most k and prove true for derivations of length k+1 Apq x in k+1 steps Apq → AprArq Apq → aArsb (r,t) (p,a,ε) and (q, ε) (s,b,t) state push state alphabet pop
Apq generates x x can bring P from p to q without changing the stack Proof (by induction on the number of steps in the computation of P on input x): Base Case: The computation has 0 steps We must show that App x But it must be that x = ε, so we are done
Inductive Step: Assume true for computations of length at most k and prove true for computations of length k+1 Suppose that P has a computation wherein x brings p to q in k+1 steps Two cases: 1. The stack repeats only at the end of this computation 2. The stack repeats somewhere in the middle of the computation
A Language is generated by a CFG It is recognized by a PDA