Chapter 4: Bottom-up Analysis 106 / 338
|
|
- Aubrie Taylor
- 5 years ago
- Views:
Transcription
1 Syntactic Analysis Chapter 4: Bottom-up Analysis 106 / 338
2 Bottom-up Analysis Attention: Many grammars are not LL(k)! A reason for that is: Definition Grammar G is called left-recursive, if A + A β for an A N, β (T N) 107 / 338
3 Bottom-up Analysis Attention: Many grammars are not LL(k)! A reason for that is: Definition Grammar G is called left-recursive, if A + A β for an A N, β (T N) Example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 name is left-recursive 107 / 338
4 Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A A β α P and A be reachable from S Assumption: G is LL(k) 108 / 338
5 Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A A β α P and A be reachable from S Assumption: G is LL(k) First k (α β n γ) First k (α β n+1 γ) = 108 / 338
6 Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A A β α P and A be reachable from S A S Assumption: G is LL(k) First k (α β n γ) First k (α β n+1 γ) = n A A β n β γ First k ( ) 108 / 338
7 Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A A β α P and A be reachable from S A S Assumption: G is LL(k) First k (α β n γ) First k (α β n+1 γ) = n A A β n β γ α First k ( ) 108 / 338
8 Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A A β α P and A be reachable from S A S Assumption: G is LL(k) First k (α β n γ) First k (α β n+1 γ) = A n A β n β γ A β α First k ( ) 108 / 338
9 Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A A β α P and A be reachable from S Assumption: G is LL(k) First k (α β n γ) First k (α β n+1 γ) = Case 1: β ɛ Contradiction!!! Case 2: β w ɛ == First k (α β k γ) First k (α β k+1 γ) 108 / 338
10 Shift-Reduce Parser Idea: We delay the decision whether to reduce until we know, whether the input matches the right-hand-side of a rule! Donald Knuth Construction: Shift-Reduce parser M R G The input is shifted successively to the pushdown. Is there a complete right-hand side (a handle) atop the pushdown, it is replaced (reduced) by the corresponding left-hand side 109 / 338
11 Shift-Reduce Parser Example: The pushdown automaton: S A B A a B b States: q 0, f, a, b, A, B, S; Start state: q 0 End state: f q 0 a q 0 a a ɛ A A b A b b ɛ B A B ɛ S q 0 S ɛ f 110 / 338
12 Shift-Reduce Parser Construction: In general, we create an automaton M R G = (Q, T, δ, q 0, F) with: Q = T N {q 0, f } (q 0, f fresh); F = {f }; Transitions: δ = {(q, x, q x) q Q, x T} // Shift-transitions {(q α, ɛ, q A) q Q, A α P} // Reduce-transitions {(q 0 S, ɛ, f )} // finish 111 / 338
13 Shift-Reduce Parser Construction: In general, we create an automaton M R G = (Q, T, δ, q 0, F) with: Q = T N {q 0, f } (q 0, f fresh); F = {f }; Transitions: δ = {(q, x, q x) q Q, x T} // Shift-transitions {(q α, ɛ, q A) q Q, A α P} // Reduce-transitions {(q 0 S, ɛ, f )} // finish Example-computation: (q 0, a b) (q 0 a, b) (q 0 A, b) (q 0 A b, ɛ) (q 0 A B, ɛ) (q 0 S, ɛ) (f, ɛ) 111 / 338
14 Shift-Reduce Parser Observation: The sequence of reductions corresponds to a reverse rightmost-derivation for the input To prove correctnes, we have to prove: (ɛ, w) (A, ɛ) iff A w The shift-reduce pushdown automaton M R G is in general also non-deterministic For a deterministic parsing-algorithm, we have to identify computation-states for reduction == LR-Parsing 112 / 338
15 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: counter E 1 E 0 + T 1 Pushdown: ( q 0 ) T 0 F 2 T 1 F 2 F 1 name 113 / 338
16 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 name ) T 0 F 2 T 1 F 2 F 1 name 113 / 338
17 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 F ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
18 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 T ) T 0 F 2 T 1 F 2 F 1 name 113 / 338
19 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 T ) T 0 F 2 T 1 F 2 F 1 name 113 / 338
20 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: + 40 E 1 E 0 + T 1 Pushdown: ( q 0 T ) T 0 F 2 T 1 F 2 F 1 name 113 / 338
21 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: + 40 E 1 E 0 + T 1 Pushdown: ( q 0 T F ) T 0 F 2 T 1 F 2 F 1 name 113 / 338
22 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: + 40 E 1 E 0 + T 1 Pushdown: ( q 0 T ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
23 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: + 40 E 1 E 0 + T 1 Pushdown: ( q 0 E ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
24 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: 40 E 1 E 0 + T 1 Pushdown: ( q 0 E + ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
25 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 E + ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
26 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 E + F ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
27 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 E + T ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
28 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 E ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
29 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( f ) T 0 T 1 F 2 F 2 F 1 name 113 / 338
30 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: + 40 E 1 E 0 + T 1 Pushdown: ( q 0 T F ) T 0 F 2 T 1 F 2 F 1 Generic Observation: In a sequence of configurations of M R G name (q 0 α γ, v) (q 0 α B, v) (q 0 S, ɛ) we call α γ a viable prefix for the complete item [B γ ]. 113 / 338
31 Reverse Rightmost Derivations in Shift-Reduce-Parsers Idea: Observe reverse rightmost-derivations of M R G! Input: E 1 E 0 + T 1 Pushdown: ( q 0 E + F ) T 0 T 1 F 2 F 2 F 1 Generic Observation: In a sequence of configurations of M R G name (q 0 α γ, v) (q 0 α B, v) (q 0 S, ɛ) we call α γ a viable prefix for the complete item [B γ ]. 113 / 338
32 Bottom-up Analysis: Viable Prefix α γ is viable for [B γ ] iff S R α B v A 0 i 0 α 1 A 1 i 1 α 2 A 2 i 2 α m B i γ... with α = α 1... α m 114 / 338
33 Bottom-up Analysis: Viable Prefix α γ is viable for [B γ ] iff S R α B v A 0 i 0 α 1 A 1 i 1 α 2 A 2 i 2 α m B i γ... with α = α 1... α m Conversely, for an arbitrary valid word α we can determine the set of all later on possibly matching rules / 338
34 Bottom-up Analysis: Admissible Items The item [B γ β] is called admissible for α iff S R α B v with α = α γ : A 0 i 0 α 1 A 1 i 1 α 2 A 2 i 2 α m B i γ β... with α = α 1... α m 115 / 338
35 Characteristic Automaton Observation: The set of viable prefixes from (N T) for (admissible) items can be computed from the content of the shift-reduce parser s pushdown with the help of a finite automaton: States: Items Start state: [S S] Final states: {[B γ ] B γ P} Transitions: (1) ([A α X β],x,[a α X β]), X (N T), A α X β P; (2) ([A α B β],ɛ, [B γ]), A α B β, B γ P; The automaton c(g) is called characteristic automaton for G. 116 / 338
36 Characteristic Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 S E E S E E E+ T E + T E E + T E E+ T E E+ T E T T E T T T F T F T T F T T F T T F T F F T F ( E ) F ( E ) F ( E ) F ( E ) F ( E ) F F 117 / 338
37 Characteristic Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 S E E S E E E+ T E + T E E + T E E+ T E E+ T E T T E T T T F T F T T F T T F T T F T F F T F ( E ) F ( E ) F ( E ) F ( E ) F ( E ) F F 117 / 338
38 Canonical LR(0)-Automaton The canonical LR(0)-automaton LR(G) is created from c(g) by: 1 performing arbitrarily many ɛ-transitions after every consuming transition 2 performing the powerset construction... for example: 0 T E F ( ( F 5 T 2 + F ( ( 6 E + 8 * ) T * F / 338
39 Canonical LR(0)-Automaton Example: E E + T 0 T 1 Therefore we determine: T T F 0 F 1 F ( E ) / 338
40 Canonical LR(0)-Automaton Example: E E + T 0 T 1 Therefore we determine: T T F 0 F 1 F ( E ) 0 2 q 0 = {[S E], q 1 = δ(q 0, E) = {[S E ], {[E E + T], {[E E + T]} {[E T], {[T T F]} q 2 = δ(q 0, T) = {[E T ], {[T F], {[T T F]} {[F ( E ) ], {[F ]} q 3 = δ(q 0, F) = {[T F ]} q 4 = δ(q 0, ) = {[F ]} 119 / 338
41 Canonical LR(0)-Automaton q 5 = δ(q 0, ( ) = {[F ( E ) ], q 7 = δ(q 2, ) = {[T T F], {[E E + T], {[F ( E ) ], {[E T], {[F ]} {[T T F], {[T F], q 8 = δ(q 5, E) = {[F ( E ) ]} {[F ( E ) ], {[E E + T]} {[F ]} q 9 = δ(q 6, T) = {[E E + T ], q 6 = δ(q 1, +) = {[E E + T], {[T T F]} {[T T F], {[T F], q 10 = δ(q 7, F) = {[T T F ]} {[F ( E ) ], {[F ]} q 11 = δ(q 8, ) ) = {[F ( E ) ]} 120 / 338
42 Canonical LR(0)-Automaton Observation: The canonical LR(0)-automaton can be created directly from the grammar. Therefore we need a helper function δɛ (ɛ-closure) We define: δ ɛ (q) = q {[B γ] [A α B β ] q, β (N T) : B B β} States: Sets of items; Start state: δ ɛ {[S S]} Final states: {q A α P : [A α ] q} Transitions: δ(q, X) = δ ɛ {[A α X β] [A α X β] q} 121 / 338
43 LR(0)-Parser Idea for a parser: The parser manages a viable prefix α = X 1... X m on the pushdown and uses LR(G), to identify reduction spots. It can reduce with A γ, if [A γ ] is admissible for α Optimization: We push the states instead of the X i in order not to process the pushdown s content with the automaton anew all the time. Reduction with A γ leads to popping the uppermost γ states and continue with the state on top of the stack and input A. Attention: This parser is only deterministic, if each final state of the canonical LR(0)-automaton is conflict free. 122 / 338
44 LR(0)-Parser... for example: q 1 = {[S E ], {[E E + T]} q 2 = {[E T ], q 9 = {[E E + T ], {[T T F]} {[T T F]} q 3 = {[T F ]} q 10 = {[T T F ]} q 4 = {[F ]} q 11 = {[F ( E ) ]} The final states q 1, q 2, q 9 contain more then one admissible item non deterministic! 123 / 338
45 LR(0)-Parser The construction of the LR(0)-parser: States: Q {f } (f fresh) Start state: q 0 Final state: f Transitions: Shift: (p, a, p q) if q = δ(p, a) Reduce: (p q 1... q m, ɛ, p q) if [A X 1... X m ] q m, q = δ(p, A) Finish: (q 0 p, ɛ, f ) if [S S ] p with LR(G) = (Q, T, δ, q 0, F). 124 / 338
46 LR(0)-Parser Correctness: we show: The accepting computations of an LR(0)-parser are one-to-one related to those of a shift-reduce parser M R G. we conclude: The accepted language is exactly L(G) The sequence of reductions of an accepting computation for a word w T yields a reverse rightmost derivation of G for w 125 / 338
47 LR(0)-Parser Attention: Unfortunately, the LR(0)-parser is in general non-deterministic. We identify two reasons: Reduce-Reduce-Conflict: [A γ ], [A γ ] q with A A γ γ Shift-Reduce-Conflict: [A γ ], [A α a β] q with a T for a state q Q. Those states are called LR(0)-unsuited. 126 / 338
48 Revisiting the Conflicts of the LR(0)-Automaton What differenciates the particular Reductions and Shifts? Input: E E 1 + T 1 Pushdown: ( q 0 T ) E 1?? T 0 F 2 T 1 F 2 F 1 name E E + T 0 T 1 T T F 0 F 1 F ( E ) / 338
49 Revisiting the Conflicts of the LR(0)-Automaton What differenciates the particular Reductions and Shifts? Input: + 40 T 0? E 1 E 0 + T 1 Pushdown: ( q 0 T )? T 0 F 2 T 1 F 2 F 1 name E E + T 0 T 1 T T F 0 F 1 F ( E ) / 338
50 Revisiting the Conflicts of the LR(0)-Automaton Idea: Matching lookahead with right context matters! Input: E E 1 + T 1 Pushdown: ( q 0 T ) E 1?? T 0 F 2 T 1 F 2 F 1 name E E + T 0 T 1 T T F 0 F 1 F ( E ) / 338
51 Revisiting the Conflicts of the LR(0)-Automaton Idea: Input: Pushdown: ( q 0 T ) Matching lookahead with right context matters! + 40 T 0? E 1? T 0 E 0 + T 1 F 2 T 1 F 2 F 1 name E E + T 0 T 1 T T F 0 F 1 F ( E ) / 338
52 LR(k)-Grammars Idea: Consider k-lookahead in conflict situations. Definition: The reduced contextfree grammar G is called LR(k)-grammar, if for First k (w) = First k (x) with: } S R α A w α β w S R α A w follows: α = α A = A w = x α β x 128 / 338
53 LR(k)-Grammars Idea: Consider k-lookahead in conflict situations. Definition: The reduced contextfree grammar G is called LR(k)-grammar, if for First k (w) = First k (x) with: } S R α A w α β w S R α A w follows: α = α A = A w = x α β x Strategy for testing Grammars for LR(k)-property 1 Focus iteratively on all rightmost derivations S R α X w α β w 2 Identify handle α β in sentence forms α β w 3 Determine minimal k, such that First k (w) associates β with a unique X β for non-prefixfree α βs 128 / 338
54 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b / 338
55 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k : Let S R α X w α β w. Then α β is of one of these forms: 129 / 338
56 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k : Let S R α X w α β w. Then α β is of one of these forms: A, B, a n a A b, a n a B b b, a n 0, a n 1 (n 0) 129 / 338
57 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k but LR(0): Let S R α X w α β w. Then α β is of one of these forms: A, B, a n a A b, a n a B b b, a n 0, a n 1 (n 0) 129 / 338
58 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k but LR(0): Let S R α X w α β w. Then α β is of one of these forms: A, B, a n a A b, a n a B b b, a n 0, a n 1 (n 0) (2) S a A c A A b b b 129 / 338
59 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k but LR(0): Let S R α X w α β w. Then α β is of one of these forms: A, B, a n a A b, a n a B b b, a n 0, a n 1 (n 0) (2) S a A c A A b b b... is also not LL(k) for any k : Let S R α X w α β w. Then α β is of one of these forms: 129 / 338
60 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k but LR(0): Let S R α X w α β w. Then α β is of one of these forms: A, B, a n a A b, a n a B b b, a n 0, a n 1 (n 0) (2) S a A c A A b b b... is also not LL(k) for any k : Let S R α X w α β w. Then α β is of one of these forms: a b, a A b b, a A c 129 / 338
61 LR(k)-Grammars for example: (1) S A B A a A b 0 B a B b b 1... is not LL(k) for any k but LR(0): Let S R α X w α β w. Then α β is of one of these forms: A, B, a n a A b, a n a B b b, a n 0, a n 1 (n 0) (2) S a A c A A b b b... is also not LL(k) for any k but again LR(0): Let S R α X w α β w. Then α β is of one of these forms: a b, a A b b, a A c 129 / 338
62 LR(k)-Grammars for example: (3) S a A c A b b A b 130 / 338
63 LR(k)-Grammars for example: (3) S a A c A b b A b Let S R α X w α β w with {y} = First k (w) then α β y is of one of these forms: 130 / 338
64 LR(k)-Grammars for example: (3) S a A c A b b A b Let S R α X w α β w with {y} = First k (w) then α β y is of one of these forms: a b 2n b c, a b 2n b b A c, a A c 130 / 338
65 LR(k)-Grammars for example: (3) S a A c A b b A b... is not LR(0), but LR(1): Let S R α X w α β w with {y} = First k (w) then α β y is of one of these forms: a b 2n b c, a b 2n b b A c, a A c 130 / 338
66 LR(k)-Grammars for example: (3) S a A c A b b A b... is not LR(0), but LR(1): Let S R α X w α β w with {y} = First k (w) then α β y is of one of these forms: a b 2n b c, a b 2n b b A c, a A c (4) S a A c A b A b b 130 / 338
67 LR(k)-Grammars for example: (3) S a A c A b b A b... is not LR(0), but LR(1): Let S R α X w α β w with {y} = First k (w) then α β y is of one of these forms: a b 2n b c, a b 2n b b A c, a A c (4) S a A c A b A b b Consider the rightmost derivations: S R a b n A b n c a b n b b n c 130 / 338
68 LR(k)-Grammars for example: (3) S a A c A b b A b... is not LR(0), but LR(1): Let S R α X w α β w with {y} = First k (w) then α β y is of one of these forms: a b 2n b c, a b 2n b b A c, a A c (4) S a A c A b A b b... is not LR(k) for any k 0: Consider the rightmost derivations: S R a b n A b n c a b n b b n c 130 / 338
69 LR(1)-Parsing Idea: Let s equip items with 1-lookahead Definition LR(1)-Item An LR(1)-item is a pair [B α β, x] with x Follow 1 (B) = {First 1 (ν) S µ B ν} 131 / 338
70 Admissible LR(1)-Items The item [B α β, x] is admissable for γ α if: S R γ B w with {x} = First 1 (w) S i 0 γ 0 A 1 i 1 γ 1 A m i m γ m B i α β x w... with γ 0... γ m = γ 132 / 338
71 The Characteristic LR(1)-Automaton The set of admissible LR(1)-items for viable prefixes is again computed with the help of the finite automaton c(g, 1). The automaton c(g, 1): States: LR(1)-items Start state: [S S, ɛ] Final states: {[B γ, x] B γ P, x Follow 1 (B)} Transitions: (1) ([A α X β, x],x,[a α X β, x]), X (N T) (2) ([A α B β, x],ɛ, [B γ, x ]), A α B β, B γ P, x First 1 (β) {x}; 133 / 338
72 The Characteristic LR(1)-Automaton The set of admissible LR(1)-items for viable prefixes is again computed with the help of the finite automaton c(g, 1). The automaton c(g, 1): States: LR(1)-items Start state: [S S, ɛ] Final states: {[B γ, x] B γ P, x Follow 1 (B)} Transitions: (1) ([A α X β, x],x,[a α X β, x]), X (N T) (2) ([A α B β, x],ɛ, [B γ, x ]), A α B β, B γ P, x First 1 (β) {x}; This automaton works like c(g) but additionally manages a 1-prefix from Follow 1 of the left-hand sides. 133 / 338
73 The Canonical LR(1)-Automaton The canonical LR(1)-automaton LR(G, 1) is created from c(g, 1), by performing arbitrarily many ɛ-transitions and then making the resulting automaton deterministic / 338
74 The Canonical LR(1)-Automaton The canonical LR(1)-automaton LR(G, 1) is created from c(g, 1), by performing arbitrarily many ɛ-transitions and then making the resulting automaton deterministic... But again, it can be constructed directly from the grammar; analoguously to LR(0), we need the ɛ-closure δ ɛ as a helper function: δɛ (q) = q {[C γ, x] [A α B β, x ] q, β (N T) : B C β x First 1 (β β ) {x }} 134 / 338
75 The Canonical LR(1)-Automaton The canonical LR(1)-automaton LR(G, 1) is created from c(g, 1), by performing arbitrarily many ɛ-transitions and then making the resulting automaton deterministic... But again, it can be constructed directly from the grammar; analoguously to LR(0), we need the ɛ-closure δ ɛ as a helper function: δɛ (q) = q {[C γ, x] [A α B β, x ] q, β (N T) : B C β x First 1 (β β ) {x }} Then, we define: States: Sets of LR(1)-items; Start state: δ ɛ {[S S, ɛ]} Final states: {q A α P : [A α, x] q} Transitions: δ(q, X) = δ ɛ {[A α X β, x] [A α X β, x] q} 134 / 338
76 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 0 = {[S E ], q 3 = δ(q 0, F) = {[T F ]} {[E E + T ], {[E T ], q 4 = δ(q 0, ) {[F ]} {[T T F ], {[T F ], q 5 = δ(q 0, ( ) = {[F ( E ) ], {[F ( E ) ], {[E E + T ], {[F ]} {[E T ], {[T T F ], q 1 = δ(q 0, E) = {[S E ], {[T F ], {[E E + T ]} {[F ( E ) ], {[F ]} q 2 = δ(q 0, T) = {[E T ], {[T T F ]} 135 / 338
77 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 0 = {[S E, {ɛ}], q 3 = δ(q 0, F) = {[T F ]} {[E E + T, {ɛ, +}], {[E T, {ɛ, +}], q 4 = δ(q 0, ) {[F ]} {[T T F, {ɛ, +, }], {[T F, {ɛ, +, }], q 5 = δ(q 0, ( ) = {[F ( E ) ], {[F ( E ), {ɛ, +, }], {[E E + T ], {[F, {ɛ, +, }]} {[E T ], {[T T F ], q 1 = δ(q 0, E) = {[S E ], {[T F ], {[E E + T ]} {[F ( E ) ], {[F ]} q 2 = δ(q 0, T) = {[E T ], {[T T F ]} 135 / 338
78 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 0 = {[S E, {ɛ}], q 3 = δ(q 0, F) = {[T F, {ɛ, +, }]} {[E E + T, {ɛ, +}], {[E T, {ɛ, +}], q 4 = δ(q 0, ) {[F, {ɛ, +, }]} {[T T F, {ɛ, +, }], {[T F, {ɛ, +, }], q 5 = δ(q 0, ( ) = {[F ( E ) ], {[F ( E ), {ɛ, +, }], {[E E + T ], {[F, {ɛ, +, }]} {[E T ], {[T T F ], q 1 = δ(q 0, E) = {[S E, {ɛ}], {[T F ], {[E E + T, {ɛ, +}]} {[F ( E ) ], {[F ]} q 2 = δ(q 0, T) = {[E T, {ɛ, +}], {[T T F, {ɛ, +, }]} 135 / 338
79 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 0 = {[S E, {ɛ}], q 3 = δ(q 0, F) = {[T F, {ɛ, +, }]} {[E E + T, {ɛ, +}], {[E T, {ɛ, +}], q 4 = δ(q 0, ) {[F, {ɛ, +, }]} {[T T F, {ɛ, +, }], {[T F, {ɛ, +, }], q 5 = δ(q 0, ( ) = {[F ( E ), {ɛ, +, }], {[F ( E ), {ɛ, +, }], {[E E + T, { ), +}], {[F, {ɛ, +, }]} {[E T, { ), +}], {[T T F, { ), +, }], q 1 = δ(q 0, E) = {[S E, {ɛ}], {[T F, { ), +, }], {[E E + T, {ɛ, +}]} q 2 = δ(q 0, T) = {[E T, {ɛ, +}], {[T T F, {ɛ, +, }]} {[F ( E ), { ), +, }], {[F, { ), +, }]} 135 / 338
80 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 5 = δ(q 5, ( ) = {[F ( E ) ], q 7 = δ(q 2, ) = {[T T F ], {[E E + T ], {[F ( E ) ], {[E T ], {[F ]} {[T T F ], {[T F ], q 8 = δ(q 5, E) = {[F ( E ) ]} {[F ( E ) ], {[E E + T ]} {[F ]} q 9 = δ(q 6, T) = {[E E + T ], q 6 = δ(q 1, +) = {[E E + T ], {[T T F ]} {[T T F ], {[T F ], q 10 = δ(q 7, F) = {[T T F ]} {[F ( E ) ], {[F ]} q 11 = δ(q 8, ) ) = {[F ( E ) ]} 136 / 338
81 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 5 = δ(q 5, ( ) = {[F ( E ), { ), +, }], q 7 = δ(q 2, ) = {[T T F ], {[E E + T, { ), +}], {[F ( E ) ], {[E T, { ), +}], {[F ]} {[T T F, { ), +, }], {[T F, { ), +, }], q 8 = δ(q 5, E) = {[F ( E ) ]} {[F ( E ), { ), +, }], {[E E + T ]} {[F, { ), +, }]} q 9 = δ(q 6, T) = {[E E + T ], q 6 = δ(q 1, +) = {[E E + T ], {[T T F ]} {[T T F ], {[T F ], q 10 = δ(q 7, F) = {[T T F ]} {[F ( E ) ], {[F ]} q 11 = δ(q 8, ) ) = {[F ( E ) ]} 136 / 338
82 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 5 = δ(q 5, ( ) = {[F ( E ), { ), +, }], q 7 = δ(q 2, ) = {[T T F ], {[E E + T, { ), +}], {[F ( E ) ], {[E T, { ), +}], {[F ]} {[T T F, { ), +, }], {[T F, { ), +, }], q 8 = δ(q 5, E) = {[F ( E ) ]} {[F ( E ), { ), +, }], {[E E + T ]} {[F, { ), +, }]} q 9 = δ(q 6, T) = {[E E + T ], q 6 = δ(q 1, +) = {[E E + T, {ɛ, +}], {[T T F ]} {[T T F, {ɛ, +, }], {[T F, {ɛ, +, }], q 10 = δ(q 7, F) = {[T T F ]} {[F ( E ), {ɛ, +, }], {[F, {ɛ, +, }]} q 11 = δ(q 8, ) ) = {[F ( E ) ]} 136 / 338
83 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 5 = δ(q 5, ( ) = {[F ( E ), { ), +, }], q 7 = δ(q 2, ) = {[T T F, {ɛ, +, }], {[E E + T, { ), +}], {[F ( E ), {ɛ, +, }], {[E T, { ), +}], {[F, {ɛ, +, }]} {[T T F, { ), +, }], {[T F, { ), +, }], q 8 = δ(q 5, E) = {[F ( E ), {ɛ, +, }]} {[F ( E ), { ), +, }], {[E E + T, { ), +}]} {[F, { ), +, }]} q 9 = δ(q 6, T) = {[E E + T, {ɛ, +}], q 6 = δ(q 1, +) = {[E E + T, {ɛ, +}], {[T T F, {ɛ, +, }]} {[T T F, {ɛ, +, }], {[T F, {ɛ, +, }], q 10 = δ(q 7, F) = {[T T F, {ɛ, +, }]} {[F ( E ), {ɛ, +, }], {[F, {ɛ, +, }]} q 11 = δ(q 8, ) ) = {[F ( E ), {ɛ, +, }]} 136 / 338
84 The Canonical LR(1)-Automaton For example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 2 First 1 (S ) = First 1 (E) = First 1 (T) = First 1 (F) = name,, ( q 2 = δ(q 5, T) = {[E T, { ), +}], q 7 = δ(q 9, ) = {[T T F, { ), +, }], {[T T F, { ), +, }]} {[F ( E ), { ), +, }], {[F, { ), +, }]} q 3 = δ(q 5, F) = {[F F, { ), +, }]} q 8 = δ(q 5, E) = {[F ( E ), { ), +, }]} q 4 = δ(q 5, ) = {[F, { ), +, }]} {[E E + T, { ), +}]} q 6 = δ(q 8, +) = {[E E + T, { ), +}], q 9 = δ(q 6, T) = {[E E + T, { ), +}], {[T T F, { ), +, }], {[T T F, { ), +, }]} {[T F, { ), +, }], {[F ( E ), { ), +, }], q 10 = δ(q 7, F) = {[T T F, { ), +, }]} {[F, { ), +, }]} q 11 = δ(q 8, ) ) = {[F ( E ), { ), +, }]} 137 / 338
85 The Canonical LR(1)-Automaton 0 T E F ( F 5 T 2 + F ( ( 6 E + 8 * ) T * F / 338
86 The Canonical LR(1)-Automaton ( F ) * * T ( F T E ( F ( T F F F ( ( * * ( ) + + F E T E T / 338
87 The Canonical LR(1)-Automaton Discussion: In the example, the number of states was almost doubled... and it can become even worse The conflicts in states q 1, q 2, q 9 are now resolved! e.g. we have for: with: q 9 = {[E E + T, {ɛ, +}], {[T T F, {ɛ, +, }]} {ɛ, +} (First 1 ( F) {ɛ, +, }) = {ɛ, +} { } = 139 / 338
88 The LR(1)-Parser: action Output goto The goto-table encodes the transitions: goto[q, X] = δ(q, X) Q The action-table describes for every state q and possible lookahead w the necessary action. 140 / 338
89 The LR(1)-Parser The construction of the LR(1)-parser: States: Q {f } (f fresh) Start state: q 0 Final state: f Transitions: Shift: (p, a, p q) if q = goto[q, a], s = action[p, w] Reduce: (p q 1... q β, ɛ, p q) if [A β ] q β, q = goto(p, A), [A β ] = action[q β, w] Finish: (q 0 p, ɛ, f ) if [S S ] p with LR(G, 1) = (Q, T, δ, q 0, F). 141 / 338
90 The LR(1)-Parser: Possible actions are: shift // Shift-operation reduce (A γ) // Reduction with callback/output error // Error... for example: E E + T 0 T 1 T T F 0 F 1 F ( E ) 0 1 action ɛ ( ) + q 1 S, 0 s q 2 E, 1 s q 2 E, 1 s q 3 T, 1 T, 1 T, 1 q 3 T, 1 T, 1 T, 1 q 4 F, 1 F, 1 F, 1 q 4 F, 1 F, 1 F, 1 q 9 E, 0 E, 0 s q 9 E, 0 E, 0 s q 10 T, 0 T, 0 T, 0 q 10 T, 0 T, 0 T, 0 q 11 F, 0 F, 0 F, 0 q 11 F, 0 F, 0 F, / 338
91 The Canonical LR(1)-Automaton In general: We identify two conflicts: Reduce-Reduce-Conflict: [A γ, x], [A γ, x] q with A A γ γ Shift-Reduce-Conflict: [A γ, x], [A α a β, y] q with a T und x {a}. for a state q Q. Such states are now called LR(1)-unsuited 143 / 338
92 The Canonical LR(1)-Automaton In general: We identify two conflicts: Reduce-Reduce-Conflict: [A γ, x], [A γ, x] q with A A γ γ Shift-Reduce-Conflict: [A γ, x], [A α a β, y] q with a T und x {a} k First k (β) k {y}. for a state q Q. Such states are now called LR(k)-unsuited 143 / 338
93 Special LR(k)-Subclasses Theorem: A reduced contextfree grammar G is called LR(k) iff the canonical LR(k)-automaton LR(G, k) has no LR(k)-unsuited states. 144 / 338
94 Special LR(k)-Subclasses Theorem: A reduced contextfree grammar G is called LR(k) iff the canonical LR(k)-automaton LR(G, k) has no LR(k)-unsuited states. Discussion: Our example apparently is LR(1) In general, the canonical LR(k)-automaton has much more states then LR(G) = LR(G, 0) Therefore in practice, subclasses of LR(k)-grammars are often considered, which only use LR(G) / 338
95 Special LR(k)-Subclasses Theorem: A reduced contextfree grammar G is called LR(k) iff the canonical LR(k)-automaton LR(G, k) has no LR(k)-unsuited states. Discussion: Our example apparently is LR(1) In general, the canonical LR(k)-automaton has much more states then LR(G) = LR(G, 0) Therefore in practice, subclasses of LR(k)-grammars are often considered, which only use LR(G)... For resolving conflicts, the items are assigned special lookahead-sets: 1 independently on the state itself == Simple LR(k) 2 dependent on the state itself == LALR(k) 144 / 338
96 Syntactic Analysis Chapter 5: Summary 145 / 338
97 Parsing Methods deterministic languages = LR(1) =... = LR(k) LALR(k) SLR(k) LR(0) regular languages LL(1) LL(k) 146 / 338
98 Parsing Methods deterministic languages = LR(1) =... = LR(k) LALR(k) SLR(k) LR(0) regular languages Discussion: LL(1) LL(k) All contextfree languages, that can be parsed with a deterministic pushdown automaton, can be characterized with an LR(1)-grammar. LR(0)-grammars describe all prefixfree deterministic contextfree languages The language-classes of LL(k)-grammars form a hierarchy within the deterministic contextfree languages. 147 / 338
99 Lexical and Syntactical Analysis: Concept of specification and implementation: 0 0 [1-9][0-9]* Generator [1 9] [0 9] E E{op}E Generator 148 / 338
100 Lexical and Syntactical Analysis: From Regular Expressions to Finite Automata t * f f f 3 4 f a f f f f f a b a b a b a 0 b a 1 a b a a 2 a b 3 4 From Finite Automata to Scanners a 0 2 b w r i t e l n ( " H a l l o " ) ; 1 a 1 4 A B / 338
101 Lexical and Syntactical Analysis: Computation of lookahead sets: F ɛ (S ) F ɛ (E) F ɛ (E) F ɛ (E) F ɛ (E) F ɛ (T) F ɛ (T) F ɛ (T) F ɛ (T) F ɛ (F) F ɛ (F) { (, name, } a a b c 3 S E T F (,, name From Item-Pushdown Automata to LL(1)-Parsers: S AB i 0 S i 1 A 1 β 0 A a B b i n A n β 1 a b i B β γ w First 1( ) δ M Output 150 / 338
102 Lexical and Syntactical Analysis: From characteristic to canonical Automata: S E E T T E E+ T T T F F E E T T F S E E T T F ( E ) F ( E ) F ( E ) F ( E ) F ( E ) F F + T E E + T E E+ T E E+ T F T T F T T F T T F 0 T E F ( ( F T + F ( ( 6 E + 8 * T 11 ) 9 7 * F 10 From Shift-Reduce-Parsers to LR(1)-Parsers: S i 0 + T E + T γ A 1 i F + F 4 γ 1 A m i m * ( F ( F 3 E ) 11 * 5 γ m B i ( 8 T ( F ( T E ) 5 8 F 2 ( *( 7 10 α β T F 2 x w * 7 10 action goto Output 151 / 338
Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.
Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A Aβ α P and A be reachable from S Assumption: G is LL(k) A n A S First k (αβ n γ) First
More informationParsing -3. A View During TD Parsing
Parsing -3 Deterministic table-driven parsing techniques Pictorial view of TD and BU parsing BU (shift-reduce) Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1) Problems with
More informationCS 406: Bottom-Up Parsing
CS 406: Bottom-Up Parsing Stefan D. Bruda Winter 2016 BOTTOM-UP PUSH-DOWN AUTOMATA A different way to construct a push-down automaton equivalent to a given grammar = shift-reduce parser: Given G = (N,
More informationLR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class
LR2: LR(0) Parsing LR Parsing CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Parsing - Roadmap Parser: decision procedure: builds a parse tree Top-down vs. bottom-up
More informationShift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.
Bottom-up Parsing Bottom-up Parsing Until now we started with the starting nonterminal S and tried to derive the input from it. In a way, this isn t the natural thing to do. It s much more logical to start
More informationCompiler Design 1. LR Parsing. Goutam Biswas. Lect 7
Compiler Design 1 LR Parsing Compiler Design 2 LR(0) Parsing An LR(0) parser can take shift-reduce decisions entirely on the basis of the states of LR(0) automaton a of the grammar. Consider the following
More informationCMSC 330: Organization of Programming Languages. Pushdown Automata Parsing
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing Chomsky Hierarchy Categorization of various languages and grammars Each is strictly more restrictive than the previous First described
More informationLecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1
Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS 164 -- Berkley University 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient
More informationTHEORY OF COMPILATION
Lecture 04 Syntax analysis: top-down and bottom-up parsing THEORY OF COMPILATION EranYahav 1 You are here Compiler txt Source Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR)
More informationBottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id
Bottom-Up Parsing Attempts to traverse a parse tree bottom up (post-order traversal) Reduces a sequence of tokens to the start symbol At each reduction step, the RHS of a production is replaced with LHS
More informationAdministrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example
Administrivia Test I during class on 10 March. Bottom-Up Parsing Lecture 11-12 From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS14 Lecture 11 1 2/20/08 Prof. Hilfinger CS14 Lecture 11 2 Bottom-Up
More informationCA Compiler Construction
CA4003 - Compiler Construction Bottom Up Parsing David Sinclair Bottom Up Parsing LL(1) parsers have enjoyed a bit of a revival thanks to JavaCC. LL(k) parsers must predict which production rule to use
More informationParsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario
Parsing Algorithms CS 4447/CS 9545 -- Stephen Watt University of Western Ontario The Big Picture Develop parsers based on grammars Figure out properties of the grammars Make tables that drive parsing engines
More informationSLR(1) and LALR(1) Parsing for Unrestricted Grammars. Lawrence A. Harris
SLR(1) and LALR(1) Parsing for Unrestricted Grammars Lawrence A. Harris Abstract Simple LR(1) and lookahead LR(1) phrase structure grammars are defined and corresponding deterministic two-pushdown automata
More informationCompiler Design Spring 2017
Compiler Design Spring 2017 3.4 Bottom-up parsing Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 1 Bottom up parsing Goal: Obtain rightmost derivation in reverse w S Reduce
More informationComputer Science 160 Translation of Programming Languages
Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Building a Handle Recognizing Machine: [now, with a look-ahead token, which is LR(1) ] LR(k) items An LR(k) item
More informationCS20a: summary (Oct 24, 2002)
CS20a: summary (Oct 24, 2002) Context-free languages Grammars G = (V, T, P, S) Pushdown automata N-PDA = CFG D-PDA < CFG Today What languages are context-free? Pumping lemma (similar to pumping lemma for
More informationCompiler Construction
Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: LR(0) Grammars LR(0) Grammars The case k = 0 is
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc13.html Textbook:Modern Compiler Design Chapter 2.2.5 (modified) 1 Pushdown automata Deterministic fficient Parsers Report
More informationCompiler Construction Lent Term 2015 Lectures (of 16)
Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.
More informationn Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)
Announcements n Quiz 1 n Hold on to paper, bring over at the end n HW1 due today n HW2 will be posted tonight n Due Tue, Sep 18 at 2pm in Submitty! n Team assignment. Form teams in Submitty! n Top-down
More informationCompiler Construction Lent Term 2015 Lectures (of 16)
Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.
More information1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,
1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u, v, x, y, z as per the pumping theorem. 3. Prove that
More informationCompiling Techniques
Lecture 5: Top-Down Parsing 6 October 2015 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream
More informationCS415 Compilers Syntax Analysis Bottom-up Parsing
CS415 Compilers Syntax Analysis Bottom-up Parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: LR(k) Shift-reduce Parsing Shift reduce
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 3 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-saarland.de and Mooly Sagiv Tel Aviv
More informationTHEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET
THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET Regular Languages and FA A language is a set of strings over a finite alphabet Σ. All languages are finite or countably infinite. The set of all languages
More informationEverything You Always Wanted to Know About Parsing
Everything You Always Wanted to Know About Parsing Part V : LR Parsing University of Padua, Italy ESSLLI, August 2013 Introduction Parsing strategies classified by the time the associated PDA commits to
More informationSyntax Analysis Part I
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer
More informationPushdown Automata: Introduction (2)
Pushdown Automata: Introduction Pushdown automaton (PDA) M = (K, Σ, Γ,, s, A) where K is a set of states Σ is an input alphabet Γ is a set of stack symbols s K is the start state A K is a set of accepting
More informationSyntax Analysis Part I
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013 2 Position of a Parser in the Compiler Model Source Program Lexical Analyzer
More informationCompiling Techniques
Lecture 5: Top-Down Parsing 26 September 2017 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream
More informationParsing VI LR(1) Parsers
Parsing VI LR(1) Parsers N.B.: This lecture uses a left-recursive version of the SheepNoise grammar. The book uses a rightrecursive version. The derivations (& the tables) are different. Copyright 2005,
More informationI 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }
Let s try building an SLR parsing table for another simple grammar: S XaY Y X by c Y X S XaY Y X by c Y X Canonical collection: I 0 : {S S, S XaY, S Y, X by, X c, Y X} I 1 : {S S } I 2 : {S X ay, Y X }
More informationSyntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van ngelen, Flora State University, 2007 Position of a Parser in the Compiler Model 2 Source Program Lexical Analyzer Lexical
More informationCompiler Construction Lectures 13 16
Compiler Construction Lectures 13 16 Lent Term, 2013 Lecturer: Timothy G. Griffin 1 Generating Lexical Analyzers Source Program Lexical Analyzer tokens Parser Lexical specification Scanner Generator LEX
More informationSyntax Analysis, VI Examples from LR Parsing. Comp 412
COMP 412 FALL 2017 Syntax Analysis, VI Examples from LR Parsing Comp 412 source code IR IR target code Front End Optimizer Back End Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved.
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-saarland.de and Mooly Sagiv Tel Aviv University
More informationCS Rewriting System - grammars, fa, and PDA
Restricted version of PDA If (p, γ) δ(q, a, X), a Σ {ε}, p. q Q, X Γ. restrict γ Γ in three ways: i) if γ = YX, (q, ay, Xβ) (p, y, YXβ) push Y Γ, ii) if γ = X, (q, ay, Xβ) (p, y, Xβ) no change on stack,
More information1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)
ECE 468 & 573 Problem Set 2: Contet-free Grammars, Parsers 1. For the following sub-problems, consider the following contet-free grammar: S AB$ (1) A A (2) A B (3) A λ (4) B B (5) B A (6) B λ (7) (a) What
More informationBottom-Up Syntax Analysis
Bottom-Up Syntax Analysis Wilhelm/Maurer: Compiler Design, Chapter 8 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-sb.de and Mooly Sagiv Tel Aviv University sagiv@math.tau.ac.il Subjects Functionality
More informationFoundations of Informatics: a Bridging Course
Foundations of Informatics: a Bridging Course Week 3: Formal Languages and Semantics Thomas Noll Lehrstuhl für Informatik 2 RWTH Aachen University noll@cs.rwth-aachen.de http://www.b-it-center.de/wob/en/view/class211_id948.html
More information8. LL(k) Parsing. Canonical LL(k) parser dual of the canonical LR(k) parser generalization of strong LL(k) parser
3/8/18 8. 8. LL(k) Parsing 1 1 8. LL(k) Parsing Canonical LL(k) parser The most general deterministic parsing method in which the input string is parsed (1) in a single Left-to-right scan, (2) producing
More informationEXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationType 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.
Course 7 1 Type 3 languages Regular grammars Finite automata Deterministic Nondeterministic Regular expressions a, a, ε, E 1.E 2, E 1 E 2, E 1*, (E 1 ) Type 2 languages Type 2 grammars 2 Brief history
More informationLR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
LR(1) Parsers Part III Last Parsing Lecture Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. LR(1) Parsers A table-driven LR(1) parser looks like source code Scanner Table-driven Parser
More informationCompiling Techniques
Lecture 6: 9 October 2015 Announcement New tutorial session: Friday 2pm check ct course webpage to find your allocated group Table of contents 1 2 Ambiguity s Bottom-Up Parser A bottom-up parser builds
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 7a Syntax Analysis Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Operator-precedence Parsing A class of shiq-reduce parsers that can be writen by hand
More informationSyntax Analysis (Part 2)
Syntax Analysis (Part 2) Martin Sulzmann Martin Sulzmann Syntax Analysis (Part 2) 1 / 42 Bottom-Up Parsing Idea Build right-most derivation. Scan input and seek for matching right hand sides. Terminology
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering La. The University of izu Japan Syntax nalysis (Parsing) 1. Uses Regular Expressions to define tokens 2. Uses Finite utomata to recognize
More informationOctober 6, Equivalence of Pushdown Automata with Context-Free Gramm
Equivalence of Pushdown Automata with Context-Free Grammar October 6, 2013 Motivation Motivation CFG and PDA are equivalent in power: a CFG generates a context-free language and a PDA recognizes a context-free
More informationEinführung in die Computerlinguistik
Einführung in die Computerlinguistik Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Summer 2016 1 / 22 CFG (1) Example: Grammar G telescope : Productions: S NP VP NP
More informationINF5110 Compiler Construction
INF5110 Compiler Construction Parsing Spring 2016 1 / 131 Outline 1. Parsing Bottom-up parsing Bibs 2 / 131 Outline 1. Parsing Bottom-up parsing Bibs 3 / 131 Bottom-up parsing: intro LR(0) SLR(1) LALR(1)
More informationWhy augment the grammar?
Why augment the grammar? Consider -> F + F FOLLOW F -> i ---------------- 0:->.F+ ->.F F->.i i F 2:F->i. Action Goto + i $ F ----------------------- 0 s2 1 1 s3? 2 r3 r3 3 s2 4 1 4? 1:->F.+ ->F. i 4:->F+.
More informationFinal exam study sheet for CS3719 Turing machines and decidability.
Final exam study sheet for CS3719 Turing machines and decidability. A Turing machine is a finite automaton with an infinite memory (tape). Formally, a Turing machine is a 6-tuple M = (Q, Σ, Γ, δ, q 0,
More informationFundamentele Informatica II
Fundamentele Informatica II Answer to selected exercises 5 John C Martin: Introduction to Languages and the Theory of Computation M.M. Bonsangue (and J. Kleijn) Fall 2011 5.1.a (q 0, ab, Z 0 ) (q 1, b,
More informationOn LR(k)-parsers of polynomial size
On LR(k)-parsers of polynomial size Norbert Blum October 15, 2013 Abstract Usually, a parser for an LR(k)-grammar G is a deterministic pushdown transducer which produces backwards the unique rightmost
More information1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)
ECE 468 & 573 Problem Set 2: Contet-free Grammars, Parsers 1. For the following sub-problems, consider the following contet-free grammar: S $ (1) (2) (3) (4) λ (5) (a) What are the terminals and non-terminals
More informationCS Pushdown Automata
Chap. 6 Pushdown Automata 6.1 Definition of Pushdown Automata Example 6.2 L ww R = {ww R w (0+1) * } Palindromes over {0, 1}. A cfg P 0 1 0P0 1P1. Consider a FA with a stack(= a Pushdown automaton; PDA).
More information60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor
60-354, Theory of Computation Fall 2013 Asish Mukhopadhyay School of Computer Science University of Windsor Pushdown Automata (PDA) PDA = ε-nfa + stack Acceptance ε-nfa enters a final state or Stack is
More informationPushdown Automata. Chapter 12
Pushdown Automata Chapter 12 Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely what it needs is a stack, which gives it an unlimited
More informationComputational Models - Lecture 4
Computational Models - Lecture 4 Regular languages: The Myhill-Nerode Theorem Context-free Grammars Chomsky Normal Form Pumping Lemma for context free languages Non context-free languages: Examples Push
More informationPARSING AND TRANSLATION November 2010 prof. Ing. Bořivoj Melichar, DrSc. doc. Ing. Jan Janoušek, Ph.D. Ing. Ladislav Vagner, Ph.D.
PARSING AND TRANSLATION November 2010 prof. Ing. Bořivoj Melichar, DrSc. doc. Ing. Jan Janoušek, Ph.D. Ing. Ladislav Vagner, Ph.D. Preface More than 40 years of previous development in the area of compiler
More informationAccept or reject. Stack
Pushdown Automata CS351 Just as a DFA was equivalent to a regular expression, we have a similar analogy for the context-free grammar. A pushdown automata (PDA) is equivalent in power to contextfree grammars.
More information3.13. PUSHDOWN AUTOMATA Pushdown Automata
3.13. PUSHDOWN AUTOMATA 317 3.13 Pushdown Automata We have seen that the regular languages are exactly the languages accepted by DFA s or NFA s. The context-free languages are exactly the languages accepted
More informationLL conflict resolution using the embedded left LR parser
DOI: 10.2298/CSIS111216023S LL conflict resolution using the embedded left LR parser Boštjan Slivnik University of Ljubljana Faculty of Computer and Information Science Tržaška cesta 25, 1000 Ljubljana,
More informationPushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen
Pushdown Automata Notes on Automata and Theory of Computation Chia-Ping Chen Department of Computer Science and Engineering National Sun Yat-Sen University Kaohsiung, Taiwan ROC Pushdown Automata p. 1
More informationSyntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412
COMP 412 FALL 2018 Syntax Analysis, VII The Canonical LR(1) Table Construction Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights
More informationBriefly on Bottom-up
Briefly on Bottom-up Paola Quaglia University of Trento November 11, 2017 Abstract These short notes provide descriptions and references relative to the construction of parsing tables for SLR(1), for LR(1),
More informationTranslator Design Lecture 16 Constructing SLR Parsing Tables
SLR Simple LR An LR(0) item (item for short) of a grammar G is a production of G with a dot at some position of the right se. Thus, production A XYZ yields the four items AA XXXXXX AA XX YYYY AA XXXX ZZ
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Outline Review LL parsing Shift-reduce parsing The LR parsing algorithm Constructing LR parsing tables 2 Top-Down Parsing: Review Top-down parsing expands a parse tree
More informationComputability and Complexity
Computability and Complexity Push-Down Automata CAS 705 Ryszard Janicki Department of Computing and Software McMaster University Hamilton, Ontario, Canada janicki@mcmaster.ca Ryszard Janicki Computability
More informationCurs 8. LR(k) parsing. S.Motogna - FL&CD
Curs 8 LR(k) parsing Terms Reminder: rhp = right handside of production lhp = left handside of production Prediction see LL(1) Handle = symbols from the head of the working stack that form (in order) a
More informationContext free languages
Context free languages Syntatic parsers and parse trees E! E! *! E! (! E! )! E + E! id! id! id! 2 Context Free Grammars The CF grammar production rules have the following structure X α being X N and α
More informationIntroduction to Bottom-Up Parsing
Introduction to Bottom-Up Parsing Outline Review LL parsing Shift-reduce parsing The LR parsing algorithm Constructing LR parsing tables Compiler Design 1 (2011) 2 Top-Down Parsing: Review Top-down parsing
More informationHarvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata
Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata Salil Vadhan October 4, 2012 Reading: Sipser, 2.2. Another example of a CFG (with proof) L = {x {a, b} : x has the same # of a s and
More informationTop-Down Parsing and Intro to Bottom-Up Parsing
Predictive Parsers op-down Parsing and Intro to Bottom-Up Parsing Lecture 7 Like recursive-descent but parser can predict which production to use By looking at the next few tokens No backtracking Predictive
More informationIntroduction to Bottom-Up Parsing
Outline Introduction to Bottom-Up Parsing Review LL parsing Shift-reduce parsing he LR parsing algorithm Constructing LR parsing tables 2 op-down Parsing: Review op-down parsing expands a parse tree from
More informationIntroduction to Bottom-Up Parsing
Outline Introduction to Bottom-Up Parsing Review LL parsing Shift-reduce parsing he LR parsing algorithm Constructing LR parsing tables Compiler Design 1 (2011) 2 op-down Parsing: Review op-down parsing
More informationSyntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis
Context-free grammars Derivations Parse Trees Left-recursive grammars Top-down parsing non-recursive predictive parsers construction of parse tables Bottom-up parsing shift/reduce parsers LR parsers GLR
More informationAn Alternative Construction in Symbolic Reachability Analysis of Second Order Pushdown Systems
An Alternative Construction in Symbolic Reachability Analysis of Second Order Pushdown Systems Anil Seth CSE Department, I.I.T. Kanpur, Kanpur 208016, INDIA. seth@cse.iitk.ac.in Abstract. Recently, it
More informationCDM Parsing and Decidability
CDM Parsing and Decidability 1 Parsing Klaus Sutner Carnegie Mellon Universality 65-parsing 2017/12/15 23:17 CFGs and Decidability Pushdown Automata The Recognition Problem 3 What Could Go Wrong? 4 Problem:
More informationSYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES
Contents i SYLLABUS UNIT - I CHAPTER - 1 : AUT UTOMA OMATA Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 2 : FINITE AUT UTOMA OMATA An Informal Picture of Finite Automata,
More informationMA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation CFL Hierarchy CFL Decision Problems Your Questions? Previous class days' material Reading Assignments HW 12 or 13 problems Anything else I have included some slides online
More informationHarvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs
Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs Harry Lewis October 8, 2013 Reading: Sipser, pp. 119-128. Pushdown Automata (review) Pushdown Automata = Finite automaton
More informationCMPT-825 Natural Language Processing. Why are parsing algorithms important?
CMPT-825 Natural Language Processing Anoop Sarkar http://www.cs.sfu.ca/ anoop October 26, 2010 1/34 Why are parsing algorithms important? A linguistic theory is implemented in a formal system to generate
More informationWhat we have done so far
What we have done so far DFAs and regular languages NFAs and their equivalence to DFAs Regular expressions. Regular expressions capture exactly regular languages: Construct a NFA from a regular expression.
More informationThe Idea of a Pushdown Automaton
Pushdown Automata 5DV037 Fundamentals of Computer Science Umeå University Department of Computing Science Stephen J. Hegner hegner@cs.umu.se http://www.cs.umu.se/~hegner The Idea of a Pushdown Automaton
More informationSyntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. source code. IR IR target.
COMP 412 FALL 2017 Syntax Analysis, VII The Canonical LR(1) Table Construction Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights
More informationPushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.
Pushdown Automata We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata. Next we consider a more powerful computation model, called a
More informationCS153: Compilers Lecture 5: LL Parsing
CS153: Compilers Lecture 5: LL Parsing Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Proj 1 out Due Thursday Sept 20 (2 days away) Proj 2 out Due Thursday Oct 4 (16 days away)
More informationBlackhole Pushdown Automata
Fundamenta Informaticae XXI (2001) 1001 1020 1001 IOS Press Blackhole Pushdown Automata Erzsébet Csuhaj-Varjú Computer and Automation Research Institute, Hungarian Academy of Sciences Kende u. 13 17, 1111
More informationEXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully
EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write
More informationMA/CSSE 474 Theory of Computation
MA/CSSE 474 Theory of Computation Bottom-up parsing Pumping Theorem for CFLs Recap: Going One Way Lemma: Each context-free language is accepted by some PDA. Proof (by construction): The idea: Let the stack
More informationComputational Models: Class 5
Computational Models: Class 5 Benny Chor School of Computer Science Tel Aviv University March 27, 2019 Based on slides by Maurice Herlihy, Brown University, and modifications by Iftach Haitner and Yishay
More informationGeneralized Bottom Up Parsers With Reduced Stack Activity
The Author 2005. Published by Oxford University Press on behalf of The British Computer Society. All rights reserved. For Permissions, please email: journals.permissions@oupjournals.org Advance Access
More informationCOMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 9 : Myhill-Nerode Theorem and applications
COMP-33 Theory of Computation Fall 217 -- Prof. Claude Crépeau Lec. 9 : Myhill-Nerode Theorem and applications COMP 33 Fall 212: Lectures Schedule 1-2. Introduction 1.5. Some basic mathematics 2-3. Deterministic
More informationPush-down Automata = FA + Stack
Push-down Automata = FA + Stack PDA Definition A push-down automaton M is a tuple M = (Q,, Γ, δ, q0, F) where Q is a finite set of states is the input alphabet (of terminal symbols, terminals) Γ is the
More information5 Context-Free Languages
CA320: COMPUTABILITY AND COMPLEXITY 1 5 Context-Free Languages 5.1 Context-Free Grammars Context-Free Grammars Context-free languages are specified with a context-free grammar (CFG). Formally, a CFG G
More informationPushdown Automata (Pre Lecture)
Pushdown Automata (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 1 / 41 Outline Pushdown Automata Pushdown
More information