INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
-
Upload
alfonso-gardener -
Category
Documents
-
view
228 -
download
4
Transcript of INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGAMS
CSci 4011
ε,ε → $0,ε → 0
1,0 → ε
1,0 → εε,$ → ε
string pop push
The language of P is the set of strings it accepts.
A → 0A1A → BB → #
CONTEXT-FREE GRAMMARS
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.
VERY INTERESTING CFGs…
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
http://docs.python.org/reference/grammar.html
PARSE TREES
A 0A1 00A11 00B11 00#11
A
B
0 1#
A
A
0 1
Definition. T is a parse tree for the derivation S ⇒* w under grammar G = (V,Σ,R,S) if
1. The root of T has label S
2. The leaves of T have labels wi Σ ∈
4. For each node with label v V and children ∈with labels ri (V Σ), (v→r) R.∈ ∪ ∈
3. The non-leaf nodes have labels v V∈
<EXPR> → <EXPR> + <EXPR>
<EXPR> → <EXPR> x <EXPR>
<EXPR> → ( <EXPR> )
<EXPR> → a
Build a parse tree for a + a x a
<EXPR>
a x+ a
<EXPR><EXPR>
a
<EXPR> <EXPR>
<EXPR>
a+ xa
<EXPR><EXPR>
a
<EXPR><EXPR>
COMPILER MODULES
LEXER
PARSER
SEMANTIC ANALYZER
TRANSLATOR/INTERPRETER
A Language is generated by a CFG
It is recognized by a PDA
Suppose L is generated by a CFG G = (V, Σ, R, S)
Construct P = (Q, Σ, Γ, , q, F) that recognizes L
Idea: P will derive w ∈ L on its stack.
Suppose L is generated by a CFG G = (V, Σ, R, S)
Construct P = (Q, Σ, Γ, , q, F) that recognizes L
(1) Place the marker symbol $ and the start variable on the stack
(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 | ε
ε,ε → $
ε,$ → ε
ε,ε → Sε,S
→ b
ε,ε → T
ε,T → a
ε,ε → a
ε,ε → T
ε,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
ε,ε → $0,ε → 0
1,0 → ε
1,0 → εε,$ → ε
SIMPLIFY
q0 q1
q2q3
ε,ε → ε
ε,ε → ε
ε,0 → ε
ε,ε → 0
ε,ε → 0
q4
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
1,0 → ε
1,0 → εε,$ → ε
q0 q1
q2q3
ε,0 → ε
ε,ε → 0
ε,ε → 0
q4
q5
What strings does Aq0q1 generate?
What strings does Aq1q2 generate?
What strings does Aq1q3 generate?
none
{0n1n | n > 0}
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
stackheight
inputstring p q
Apq → aArsb
r s
1. The first repeat is at the end of x:
ba
stackheight
inputstring p r q
Apq → AprArq
2. The stack repeats before the end of x:
V = {Apq | p,qQ }
S = Aq0qaccept
Formally:
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
1,0 → ε
1,0 → εε,$ → ε
q0 q1
q2q3
ε,0 → ε
ε,ε → 0
ε,ε → 0
q4
q5
What strings does Aq0q1 generate?
What strings does Aq1q2 generate?
What strings does Aq1q3 generate?
Aq0q3 → εAq1q2ε
Aqq → ε
Apq → AprArq
Aq1q2 → 0Aq1q21
Aq1q2 → 0Aq1q11
none
{0n1n | n > 0}
none
A Language is generated by a CFG
It is recognized by a PDA