Parse Trees 2IT70 Finite Automata and Process Theory Technische - - PowerPoint PPT Presentation

parse trees 2it70 finite automata and process theory
SMART_READER_LITE
LIVE PREVIEW

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 (


slide-1
SLIDE 1

Parse Trees 2IT70 Finite Automata and Process Theory

Technische Universiteit Eindhoven May 26, 2014

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Identifying production sequences (cont.)

S S S ( S ) ε ( S ) ( S ) ε

2 IT70 (2014) Parse Trees 3 / 18

slide-4
SLIDE 4

Identifying production sequences (once more)

S S S ( S ) ε ( S ) ( S ) ε

2 IT70 (2014) Parse Trees 4 / 18

slide-5
SLIDE 5

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

slide-6
SLIDE 6

A parse tree with yield ()(())

S S S ( S ) ε ( S ) ( S ) ε

2 IT70 (2014) Parse Trees 6 / 18

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 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