Download presentation
Presentation is loading. Please wait.
Published byAlfonso Gardener Modified over 10 years ago
1
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011
2
ε,ε → $ 0,ε → 0 1,0 → ε ε,$ → ε stringpoppush The language of P is the set of strings it accepts.
3
A → 0A1 A → B B → # 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.
4
VERY INTERESTING CFGs… http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html http://docs.python.org/reference/grammar.html
5
PARSE TREES A 0A1 00A11 00B11 00#11 A B 01# A A 01
6
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 w i ∈ Σ 4. For each node with label v ∈ V and children with labels r i ∈ (V ∪ Σ), (v→r) ∈ R. 3. The non-leaf nodes have labels v ∈ V
7
→ + → x → ( ) → a Build a parse tree for a + a x a ax+a a a+xa a
8
COMPILER MODULES LEXER PARSER SEMANTIC ANALYZER TRANSLATOR/INTERPRETER
9
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.
10
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.
11
Suppose L is generated by a CFG G = (V, Σ, R, S) Construct P = (Q, Σ, Γ, , q, F) that recognizes L (q start ) Push S$ and go to q loop (q loop ) Repeat the following steps forever: (a) On (ε,v) where (v → s) ∈ R, push s and go to q loop (b) On ( , ), pop and go to q loop (c) On (ε,$) go to q accept (else) get stuck!
12
ε,ε → S$ ε,$ → ε ε,A → w for rule A → w a,a → ε for terminal a
13
S → aTb T → Ta | ε ε,ε → $ ε,$ → ε ε,ε → S ε,S → b ε,ε → T ε,T → a ε,ε → a ε,ε → T ε,T → ε a,a → ε b,b → ε
14
A Language is generated by a CFG It is recognized by a PDA
15
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, q accept (2) It empties the stack before accepting (3) Each transition either pushes a symbol or pops a symbol, but not both
16
ε,ε → $ 0,ε → 0 1,0 → ε ε,$ → ε SIMPLIFY q0q0 q1q1 q2q2 q3q3 ε,ε → ε ε,0 → ε ε,ε → 0 q4q4 q5q5
17
Idea: for each pair of states p and q in P, the grammar will have a variable A pq that generates all strings that that can take P from p to q without changing the stack* V = {A pq | p,q Q } S = A q 0 q accept *starting from any stack S in p, P has stack S at q.
18
ε,ε → $ 0,ε → 0 1,0 → ε ε,$ → ε q0q0 q1q1 q2q2 q3q3 ε,0 → ε ε,ε → 0 q4q4 q5q5 What strings does A q 0 q 1 generate? What strings does A q 1 q 2 generate? What strings does A q 1 q 3 generate? none {0 n 1 n | n > 0} none
19
A pq 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
20
stack height input string pq A pq → aA rs b rs 1. The first repeat is at the end of x: ba
21
stack height input string prq A pq → A pr A rq 2. The stack repeats before the end of x:
22
V = {A pq | p,q Q } S = A q 0 q accept 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 A pq → aA rs b For each p,q,r Q, add the rule A pq → A pr A rq For each p Q, add the rule A pp → ε
23
ε,ε → $ 0,ε → 0 1,0 → ε ε,$ → ε q0q0 q1q1 q2q2 q3q3 ε,0 → ε ε,ε → 0 q4q4 q5q5 What strings does A q 0 q 1 generate? What strings does A q 1 q 2 generate? What strings does A q 1 q 3 generate? A q 0 q 3 → εA q 1 q 2 ε A qq → ε A pq → A pr A rq A q 1 q 2 → 0A q 1 q 2 1 A q 1 q 2 → 0A q 1 q 1 1 none {0 n 1 n | n > 0} none
24
A Language is generated by a CFG It is recognized by a PDA
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.