Parse Trees 2IT70 Finite Automata and Process Theory Technische - - PowerPoint PPT Presentation
Parse Trees 2IT70 Finite Automata and Process Theory Technische - - PowerPoint PPT Presentation
Parse Trees 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 26, 2014 Identifying production sequences parentheses grammar S SS ( S ) several production sequences for string ()(()) S G SS G (
Identifying production sequences
parentheses grammar S → ε ∣ SS ∣ (S) several production sequences for string ()(())
S ⇒G SS ⇒G (S)S ⇒G
()S
⇒G ()(S) ⇒G ()((S)) ⇒G ()(()) S ⇒G SS ⇒G (S)S ⇒G (S)(S) ⇒G ()(S) ⇒G ()((S)) ⇒G ()(())
swapping independent productions
2 IT70 (2014) Parse Trees 2 / 18
Identifying production sequences (cont.)
S S S ( S ) ε ( S ) ( S ) ε
2 IT70 (2014) Parse Trees 3 / 18
Identifying production sequences (once more)
S S S ( S ) ε ( S ) ( S ) ε
2 IT70 (2014) Parse Trees 4 / 18
Yield of a parse tree
CFG G = (V , T, R, S ) set PTG of all parse trees of G [X] single node tree, X ∈ V ∪ T [A → ε] two node tree, root A, leaf ε for rule A → ε ∈ R [A → PT 1,PT 2,... ,PT k] rule A → X1⋯Xk ∈ R parse trees PTi with root Xi yield function yield ∶ PTG → (V ∪ T)∗ yield([X]) = X yield([A → ε]) = ε yield([A → PT 1,... ,PT k]) = yield(PT 1) ⋅ ... ⋅ yield(PT k) parse tree PT is complete if yield(PT) ∈ T ∗
2 IT70 (2014) Parse Trees 5 / 18
A parse tree with yield ()(())
S S S ( S ) ε ( S ) ( S ) ε
2 IT70 (2014) Parse Trees 6 / 18
Another parse tree
CFG S → AB A → ε ∣ aaA B → ε ∣ Bb S A A B B a a b ε ε parse tree with yield aab
2 IT70 (2014) Parse Trees 7 / 18
Parsing
CFG G with rules S → ε ∣ aSb ∣ bSa ∣ SS aabb ∈ L(G)? S w ∈ {a,b}∗ S ε 1 ε S a S b 2 awb S b S a bwa 3 S S S 4 w1w2
2 IT70 (2014) Parse Trees 8 / 18
Parsing
CFG G with rules S → ε ∣ aSb ∣ bSa ∣ SS aabb ∈ L(G)?
S a S b ε 2.1 ab S a S b a S b 2.2 aawbb S a S b 2.3 b S a abwab S a S b S S 2.4 aw1w2b
2 IT70 (2014) Parse Trees 9 / 18
Parsing
CFG G with rules S → ε ∣ aSb ∣ bSa ∣ SS aabb ∈ L(G)?
S a S b ε a S b ε 2.2.1 aabb S a S b a S b 2.2.2 a S b aaawbbb S a S b 2.2.3 a S b b S a aabwabb S a S b a S b S S 2.2.4 aaw1w2bb
Thus aabb ∈ L(G)
2 IT70 (2014) Parse Trees 10 / 18
Clicker question L91
parsing takes at most 2∣w∣ − 1 rounds if no summand ε summands have at least one terminal or at least two variables With the parsing procedure and restrictions above, we have that
- A. Parsing is linear in the length of the string
- B. Parsing is quadratic in the length of the string
- C. Parsing is exponential in the length of the string
- D. Can’t tell
2 IT70 (2014) Parse Trees 11 / 18
generated strings of terminals vs. yields of parse trees
theorem CFG G = (V , T, R, S ) A ⇒
∗ G w implies w = yield(PT)
for parse tree PT with root A proof by induction on n: A ⇒n
G w
implies ∃PT ∈ PTG(A)∶w = yield(PT) for all A ∈ V and w ∈ T ∗
⊠
thus L(G)={w ∈ T ∗ ∣ S ⇒
∗ G w }
⊆{yield(PT) ∣ PT complete parse tree of G, root S }
2 IT70 (2014) Parse Trees 12 / 18
Clicker question L92
Suppose X
ℓ
⇒∗
G β for a CFG G.
Then it holds that
- A. αXγ
ℓ
⇒∗
G αβγ for all α ∈ T ∗ and γ ∈ T ∗
- B. αXγ
ℓ
⇒∗
G αβγ for all α ∈ T ∗ and γ ∈ (V ∪ T)∗
- C. αXγ
ℓ
⇒∗
G αβγ for all α ∈ (V ∪ T)∗ and γ ∈ T ∗
- D. αXγ
ℓ
⇒∗
G αβγ for all α ∈ (V ∪ T)∗ and γ ∈ (V ∪ T)∗
- E. Can’t tell
2 IT70 (2014) Parse Trees 13 / 18
From parse tree to leftmost production sequence
theorem CFG G for parse tree PT, root A and yield w: A
ℓ
⇒∗
G w
proof induction on the height of the parse tree PT
⊠
thus {yield(PT) ∣ PT complete parse tree of G, root S }⊆ {w ∈ T ∗ ∣ S ⇒
∗ G w }=L(G)
2 IT70 (2014) Parse Trees 14 / 18
Different parse trees (harmless)
S a S b ε a S b ε aabb S S S ε a S b ε a S b ε aabb ambiguous grammar S → ε ∣ aSb ∣ SS
2 IT70 (2014) Parse Trees 15 / 18
Different parse trees (harmful)
E → I ∣ E‘+’E ∣ E‘*’E ∣ ‘(’E‘)’ I → a ∣ b ∣ c
E E ∗ E I E + E a I I b c a*b+c E E + E E ∗ E E I I c a b a*b+c
ambigious grammar
2 IT70 (2014) Parse Trees 16 / 18
Different parse trees (harmful, cont.)
E → I ∣ E‘+’E ∣ E‘*’E ∣ ‘(’E‘)’ I → a ∣ b ∣ c
14 2 ∗ 7 2 3 + 4 a 3 4 b c 2*3+4? wrong 10 6 + 4 2 ∗ 3 4 2 3 c a b 2*3+4? right
ambigious grammar
2 IT70 (2014) Parse Trees 17 / 18
Disambiguation
E → T ∣ E‘+’T T → F ∣ T‘*’F F → I ∣ ‘(’E‘)’ I → a ∣ b ∣ c
E E + T T F T ∗ F I F I c I b a a*b+c
syntactic categories: expression, term, factor, identifier
2 IT70 (2014) Parse Trees 18 / 18