LR Parsing Techniques

Size: px
Start display at page:

Download "LR Parsing Techniques"

Transcription

1 LR Parsing Techniques Bottom-Up Parsing - LR: a special form of BU Parser LR Parsing as Handle Pruning Shift-Reduce Parser (LR Implementation) LR(k) Parsing Model - k lookaheads to determine next action Parsing Table Construction: SLR, LR, LLR 1

2 Bottom-Up Parsing bottom-up parser attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working up towards the root (the top). 2

3 Bottom-Up Parsing: Ex1 BU Parsing: Construct a parse tree from the leaves to the root: left-to-right reduction G: S a B e input: abbcde b c b B d S B B a b b c d e a b b c d e a b b c d e a b b c d e a b b c d e 3

4 Bottom-Up Parsing: Ex2 BU Parsing: Construct a parse tree from the leaves to the root: random reduction G: S a B e input: abbcde b c b B d S B B B a b b c d e a b b c d e a b b c d e a b b c d e a b b c d e 4

5 LR Parsing: BU + Left-to-Right Many ways to construct a parse tree bottom-up Ex1 & Ex2 Prefer a simpler form of parser Left-to-right scanning If scanning strictly Left-to-right Rightmost derivation in reverse (thus the name LR Parser). Why rm.? ( Ex1) Never consider right terminals while reducing left ( N)* Reduce left ( N)* (terminals or non-terminals) as much as possible until no further reduce Shift when no further reduce Reversing the sequence of reduction corresponds to a rightmost derivation LR Parser special form of BU Parser parser with simpler form: left-to-right scan 5

6 LR Parsing: BU + Left-to-Right LR Parsing: Construct a parse tree from the leaves to the root, scanning left-to-right (resulting in rightmost derivation in reverse) a b b c S a B e b c b B d d e a a b b c b b c d e d e a b b c d e input: abbcde a b b c B d e abbcde rm abcde rm ade rm abe rm S a b b S c B d e 6

7 Rightmost Derivation in Reverse E 1 E 5 E 3 E 2 E 3 E 4 E 5 E 4 E 1 E 2 id1 + id2 * id3 id1 + id2 * id3 8

8 LR Parsing The L stands for scanning the input from left to right The R stands for constructing a rightmost derivation in reverse 9

9 LR Parsing LR Parsing =/= Leftmost Reduction The 1 st reducible substring does not always result in successful parse Handle(s): those successfully lead to S Top-Down: Expansion Matching Bottom-Up: Shift/Reduce Locating next handle to reduce [How To??] Handle pruning: hide details below reduced ( N)* a b b c d e a b b c d e 10

10 Handles NOT all (leftmost) reduction ( ) leads to the start symbol S: rm rm (n) rm S Only some handles do handle of a right-sentential form consists of a production a position of where can be replaced by to produce the previous right-sentential form in a rightmost derivation of Right-sent. forms: abbcde rm abcde rm ade rm abe rm S Handles: b b c B d S a B e 11

11 If S * rm rm Handles, then in the position following is a handle of. (The string contains only terminal symbols.) We say a handle rather than the handle since the grammar may be ambiguous. But if the grammar is unambiguous, then every right sentential form has exactly one handle. 12

12 LR Parsing as Handle Pruning rm rm S S Pruning: Find a string that is reducible to S and hide its details by reduction and proceed with the new sentential form. Never consider right terminals while reducing left grammar symbols The string to the right of the handle contains only terminals ( is the rightmost non-terminal) is the leftmost complete interior node with all its children in the tree 15

13 n Example S S S B B B a b b c d e a b c d e a d e S B S a e 16

14 LR Parsing as Handle Pruning (1st reduction sequence) rightmost derivation in reverse can be obtained by handle pruning. Let G = E E+E E*E (E) id (ambiguous!) rm Right-sentential Handle Reducing form production id 1 +id 2 *id 3 id 1 E id E+id 2 *id 3 id 2 E id E+E*id 3 id 3 E id E+E*E E*E E E*E E+E E+E E E+E E 17

15 LR Parsing as Handle Pruning (2nd reduction sequence) rightmost derivation in reverse can be obtained by handle pruning. rm Let G = E E+E E*E (E) id (ambiguous!) Right-sentential Handle Reducing form production id 1 +id 2 *id 3 id 1 E id E+id 2 *id 3 id 2 E id E+E*id 3 E+E E E+E E*id 3 id 3 E id E*E E*E E E*E E 18

16 Shift-Reduce Parsing rm rm S shift Input Handle reduce) Parsing program Output Stack Parsing table 20

17 Stack Implementation of Shift-Reduce Parsers convenient way to implement a shift-reduce parse is to use a stack to hold grammar symbols and an input buffer to hold the string to be parsed. a push-down machine with a tape The parser operates by shifting zero or more symbols onto the stack until a handle is on top of the stack. The parser then replaces/reduces with/to the left side of the appropriate production. This procedure repeats until the stack contains the start symbol and the input is empty. 21

18 Stack Operations Shift: shift the next input symbol onto the top of the stack Reduce: replace the handle at the top of the stack with the corresponding nonterminal ccept: announce successful completion of the parsing Error: call an error recovery routine 22

19 n Example ction Stack Input S $ a b b c d e $ S $ a b b c d e $ R $ a b b c d e $ S $ a b c d e $ S $ a b c d e $ R $ a b c d e $ S $ a d e $ R $ a d e $ S $ a B e $ R $ a B e $ $ S $ 24

20 Configurations of shift-reduce parser on inputid 1 +id 2 *id 3 Step Stack Input ction 1 $ id 1 +id 2 *id 3 $ shift 2 $id 1 +id 2 *id 3 $ reduce by E id 3 $E +id 2 *id 3 $ shift 4 $E+ id 2 *id 3 $ shift 5 $E+id 2 *id 3 $ reduce by E id 6 $E+E *id 3 $ shift 7 $E+E* id 3 $ shift 8 $E+E*id 3 $ reduce by E id 9 $E+E*E $ reduce by E E*E 10 $E+E $ reduced by E E+E 11 $E $ accept *Note: The grammar is ambiguous. Therefore, there is another possible reduction sequence. 25

21 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 How to represent parsing states so we can tell the right parsing actions to take? B a b b c d e $

22 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 How to represent parsing states so we can tell the right parsing actions to take? B a b b c d e $

23 G: S a B e b c b B d Input: abbcde LR Parsing States S0:. S. a B e S 10 B a b b c d e $

24 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 S1: S a. B e (shift a).bc.b B a b b c d e $

25 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 S2: b. (shift b, to reduce b) B a b b c d e $

26 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 S3: S a. B e B. d.bc B a b b c d e $

27 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 S4: b.c (shift b) B a b b c d e $

28 G: S a B e b c b B d Input: abbcde LR Parsing States S5: bc. (shift c, reduce bc) S 10 B a b b c d e $

29 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 S6: S a. B e B. d B a b b c d e $

30 G: S a B e b c b B d Input: abbcde LR Parsing States S 10 S7: B d. (shift d, reduce B d) B a b b c d e $

31 G: S a B e b c b B d Input: abbcde LR Parsing States S8: S a B. e S 10 B a b b c d e $

32 G: S a B e b c b B d Input: abbcde LR Parsing States S9: S a B e. (shift e, reduce S a B e ) S 10 B a b b c d e $

33 G: S a B e b c b B d Input: abbcde LR Parsing States S10: S S. (S reduced) S 10 B a b b c d e $

34 G: S a B e b c b B d Input: abbcde LR Parsing States S0:. S. a B e S1: S a. B e (shift a) a.bc.b.bc.b (closed, no further expansion) S2: b. (shift b, reduce b) a S3: S a. B e B. d.bc S4: b.c (shift b) a b S5: bc. (shift c, reduce bc) S6: S a. B e B. d S7: B d. (shift d, reduce B d) a b S8: S a B. e S9: S a B e. (shift e, reduce S a B e ) S10: S S. (S reduced) b b b b c b c d e B b c c d e d e d e a S B b b c d e 39

35 mbiguity: Sources of Conflicts When trying to reduce a sub-string of the current sentential form: Not all reducible substrings are handles mbiguous: More than one substring as a handle Sources of Conflicts non-lr Grammar Shift-reduce conflicts Reduce-reduce conflicts 40

36 Shift/Reduce Conflict stmt if expr then stmt if expr then stmt else stmt other Stack Input $ if expr then stmt * else stmt $ Shift if expr then stmt else stmt Reduce if expr then stmt 41

37 Reduce/Reduce Conflict (1) stmt id ( para_list ) // func(a,b) (2) stmt expr := expr (3) para_list para_list, para (4) para_list para (5) para id (6) expr id ( expr_list ) // array(a,b) (7) expr id (8) expr_list expr_list, expr (9) expr_list expr -Need a complex lexical analyzer to identify id vs. procid - Reduction depends on stack[sp-2] Stack Input (a) $ id ( id, id ) $ [Q: r5? r7?] [Sol: use stmt procid ( para_list ) => (a) r7 (b) r5] (b) $- - - procid ( id, id ) $ [r5] 42

38 LR(k) Grammars Only some classes of grammars, known as the LR(k) Grammars, can be parsed deterministically by a shift-reduce parser CFG s that are non-lr may need some adaptation to make them deterministically parsed with a shift-reduce parser Parsing Table Construction Predict handles at each positions (after shifts) 43

39 LR(k) Parsing The L stands for scanning the input from left to right The R stands for constructing a rightmost derivation in reverse The k stands for the number of lookahead input symbols used to make parsing decisions 44

40 LR Parsing The LR parsing algorithm Constructing SLR(1) parsing tables Constructing LR(1) parsing tables Constructing LLR(1) parsing tables 45

41 Model of an LR Parser State after action handle Stack S m X m S m-1 Shift/Reduce LR Parsing Program Input Output State after Reduction State before action Initial State X m-1 S 0 ction Goto Parsing table 46

42 Parsing Table for Expression Grammar (0) E E (1) E E + T (2) E T (3) T T * F (4) T F (5) F ( E ) (6) F id Follow(E)={+,),$} Follow(T)={+,),$,*} Follow(F)={+,),$,*} State ction Goto id + * ( ) $ E T F 0 s5 s s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s r6 r6 r6 r6 6 s5 s s5 s s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 47

43 GOTO ctions I0: E. E E.E+T E.T T.T*F T.F F.(E) F.id E T F I1: E E. E E.+T I2: E T. T T.*F I3: T F. 0 E 1 0 T 2 0 F 3 0 id 5 ( id fter reduction Before reduction I4: F (.E) E.E+T E.T T.T*F T.F F.(E) F.id I5: F id. 48

44 LR Parsing lgorithm Input: n input string and an LR parsing table with functions action and goto for a grammar G. Output: If is in L(G), a bottom-up parse for ; otherwise, an error indication. Method: Initially, the parser has s 0 on its stack, where s 0 is the initial state, and $ in the input buffer. Shift/reduce according to the parsing table (See next Page) 49

45 LR Parsing Program while (1) do { s := the state of top of the stack; a := get input token; if (action[s,a] == shift s ) { push a then s on top of the stack; a = get input token; } else if (action[s,a] == reduce -> ) { pop 2* symbols off the stack; s = the state now on top of the stack; push then goto[s,] on top of the stack; output the production -> ; } else if (action[s,a] == accept) return; else error(); } 50

46 Stack Input LR Parsing onid 1 *id 2 +id 3 shift/reduce+goto ction (1) 0 id * id + id $ (0,id):s5 Shift (2) 0 id 5 * id + id $ (5,*):r6; (0,F):3 Reduce by F id (3) 0 F 3 * id + id $ (3,*):r4; (0,T):2 Reduce by T F (4) 0 T 2 * id + id $ (2,*):s7 Shift (5) 0 T 2 * 7 id + id $ (7,id):s5 Shift (6) 0 T 2 * 7 id 5 + id $ (5,+):r6; (7,F):10 Reduce by F id (7) 0 T 2 * 7 F 10 + id $ (10,+):r3; (0,T):2 Reduce by T T*F (8) 0 T 2 + id $ (2,+):r2; (0,E):1 Reduce by E T (9) 0 E 1 + id $ (1,+):s6 Shift (10) 0 E id $ (6,id):s5 Shift (11) 0 E id 5 $ (5,$):r6; (6,F):3 Reduce by F id (12) 0 E F 3 $ (3,$):r4; (6,T):9 Reduce by T F (13) (14) 0 E T 9 0 E 1 $ $ (9,$):r1; (0,E):1 (1,$):acc Reduce by E E+T ccept 51

47 LR Parsing dvantages Efficient: non-backtracking Efficient Parsing Efficient Error detection (& correction) Coverage: Detect syntax error as soon as one appear during L-o-R scan virtually all programming languages G(LR) > G(TD predictive parsing) Disadvantages: Too much work to construct by hands ( YCC) 52

48 How To: LR Parsing (repeated) LR Parsing =/= Leftmost Reduction The 1 st reducible substring does not always result in successful parse Handle(s): those successfully lead to S Top-Down: Expansion Matching Bottom-Up: Shift/Reduce Locating next handle to reduce [How To??] Handle pruning: hide details below reduced ( N)* 53

49 LR Parsing Table Construction Techniques Parsing Table Construction: SLR(1) Parser - LR(0) Items & States LR(1) Parser - shift/reduce conflict resolution - LR(1) Items & States LLR(1) Parser - LR(1) state merge - reduce-reduce conflict 54

50 SLR Parser Coverage: weakest in terms of #grammars it succeeds Easiest to construct Parser: a DF for recognizing viable prefixes States: Sets of LR(0) Items The items in a set can be viewed as the states of an NF recognizing viable prefixes Grouping items into sets is equivalent to subset construction 56

51 G: S a B e b c b B d Input: abbcde LR Parsing States S0:. S. a B e S1: S a. B e (shift a) a.bc.b.bc.b (closed, no further expansion) S2: b. (shift b, reduce b) a S3: S a. B e B. d.bc S4: b.c (shift b) a b S5: bc. (shift c, reduce bc) S6: S a. B e B. d S7: B d. (shift d, reduce B d) a b S8: S a B. e S9: S a B e. (shift e, reduce S a B e ) S10: S S. (S reduced) b b b b c b c d e B b c c d e d e d e a S B b b c d e 57

52 Viable Prefix The set of prefixes of c.s.f. s (canonical/right sentential forms) that can appear on the stack of a shift-reduce parser are called viable prefixes. Equivalently, it is a prefix of a right-sentential form that does not continue past the right end of the rightmost handle of that sentential form If is a viable prefix, then w * w is a c.s.f. 59

53 Item and Valid Item n LR(0) item (item for short) is a marked production [ 1 2 ] (dotted rule: production with a dot at RHS) n item [ 1 2 ] is said to be valid for some viable prefix 1 iff w * S * w 1 2 w The represents where we are now during parsing Left of dot: those scanned Right of dot: those to be visited later S w

54 Example of Valid Item Consider the grammar: S 1C D C 3 4 D 1B B 2 S S or S D 1 C Valid items for the viable prefix : [S 1C], [S D], and [D 1B] 1 B 61

55 Example of Valid Item (cont.) ssume 1, i.e., S ' could be S 1 C or S 1 C D S 1 B Valid items for the viable prefix 1 : [S 1 C], [C 3], [C 4], [D 1 B], and [B 2] 62

56 Example of Valid Item (cont.) ssume S 1 C 3 Valid item for viable prefix 13 : [C 3 ] Valid item for viable prefix 1C : [S 1C ] 63

57 Closure: ll Valid Items Enumerable from G Given a grammar E E E E+T T T T*F F F (E) id What are valid items for the viable prefix E+? [E E+ T], but also [T... F] since 1 2 E * E+T T F E+ F 1 2 Likewise, [T T*F], [T F], [F (E)], [F id] called Closure of [E E+ T] (inclusive)

58 Computation of Closure Given a set, I, of items Initially Closure(I) = I Loop: for all items [ B ] If [ B ] is in Closure(I) and B is in P, then include [B ] into Closure(I). Repeat the Loop until no new dotted rules can be added Initial set of items for a grammar: I 0 = Closure({[S S] }) (S: start symbol, S : augmented start symbol) 65

59 GOTO Computation Let I be a set of items which are valid for some viable prefix. Then goto(i,x), where X (N or Σ), is the set of items which are valid for the viable prefix X. So [ X ] in I implies Closure({[ X ]}) in goto(i,x) S * ]w X w X w ([]: set of items I, including [ X ] others) = 66

60 Sets of LR(0) Items Construction ugment the grammar with: S S Let I 0 = Closure({[S S] }), C = {I 0 } while (not all elements of C are marked) { } -select an unmarked item set of C (say I ) and mark it; - X (V or Σ), if goto(i,x) is not already in C, then add goto(i,x) to C (unmarked); also called Characteristic Finite State Machine (CFSM) Construction lgorithm. 67

61 SLR(1) Parsing ctions Compute the CFSM states C={I 0,I 1,,I n }. 1. If [ a ] I i and goto(i i,a) = I j then set action(i i,a) = shift,i j (where a is a terminal) 2. If [ ] I i then set action(i i,a) = reduce for all a in Follow() 1. terminal a in Follow() does not guarantee that will result in a successful parse. (not necessarily a handle ) 2. But, a terminal NOT in Follow() will definitely indicate an impossible parse. 3. So reduction on symbols in Follow() is only a loose criterion for possible success parse. 3. If [S S ] I i then set action(i i,$) = accept 4. Other action(*,*) = error 68

62 Conflicts Shift-reduce conflicts: both a shift action and a reduce action are possible in the same Closure. E.g., state 2 in Figure 4.37 (p.229) [ho 86] Reduce-reduce conflicts: two or more distinct reduce actions are possible in the same Closure. 69

63 Example: Grammar G for Math Expressions (0) E E (1) E E+T (2) E T (3) T T*F (4) T F (5) F (E) (6) F id Follow(E)={+,),$}, Follow(T)={+,),$,*}, Follow(F)={+,),$,*} 70

64 Computing SLR(1) States for G an SLR(1) State = a set of LR(0) items (See the next slide, Fig. 4.35, page 225, [ho 86]) 71

65 Canonical LR(0) Collection for G I0: E. E E.E+T E.T T.T*F T.F F.(E) F.id ( ( E T F T I1: E E. E E.+T I2: E T. T T.*F I3: T F. I4: F (.E) E.E+T E.T T.T*F T.F F.(E) F.id F + * E I6: E E+.T T.T*F T.F F.(E) F.id F I7: T T*.F F.(E) F.id + I8: F (E.) E E.+T T ( id ( id I9: E E+T. T T.*F * I10: T T*F. F I11: F (E). ) id id I5: F id. 72

66 GOTO ctions I0: E. E E.E+T E.T T.T*F T.F F.(E) F.id E T F I1: E E. E E.+T I2: E T. T T.*F I3: T F. 0 E 1 0 T 2 0 F 3 0 id 5 ( id fter reduction Before reduction I4: F (.E) E.E+T E.T T.T*F T.F F.(E) F.id I5: F id. 74

67 Parsing Table for Expression Grammar (0) E E (1) E E + T (2) E T (3) T T * F (4) T F (5) F ( E ) (6) F id Follow(E)={+,),$} Follow(T)={+,),$,*} Follow(F)={+,),$,*} State ction Goto id + * ( ) $ E T F 0 s5 s s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s r6 r6 r6 r6 6 s5 s s5 s s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 76

68 Transition Diagram of DF D for Viable Prefixes State transition in terms of sets of LR(0) items (Fig. 4.36) SLR(1) Parsing Table: (Fig. 4.31) I i = a => I j : action(i,a) = shift-j I i = => I j : goto(i,) = j I i : [. ] action(i,follow()) = reduce [ If = S (augmented start symbol ) action(i,$)=accept 77

69 Visualizing Transitions in the Transition Diagram Shift: moving forward one step along arc Equivalent to pushing input symbols Reduce LHS RHS : moving backward to a previous state s along arcs labeled with the RHS symbols Then GOTO(s, LHS) equivalent to popping RHS symbols from stack then pushing LHS, then redefining current state 78

70 Parsing Table for Expression Grammar State action goto id + * ( ) $ E T F 0 s5 s s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s r6 r6 r6 r6 6 s5 s s5 s s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 79

71 LR Parsing Table Construction Techniques Canonical LR Parsing Table LLR Parsing Table (See Textbook ) 80

72 Canonical LR Parser SLR(1) parser does NOT always work SLR(1) Grammar => Unambiguous Unambiguous CFG =/=> SLR(1) Grammar E.g., Shift-reduce conflicts in the SLR(1) parsing table may NOT be a real shift-reduce conflict (e.g., impossible reduce ) Need more specific & additional information to define states [to avoid false reductions] use LR(1) items, instead of LR(0) items Much more states than SLR(1) Need (canonical) LR(1) or LLR(1) Parsers (Parsing Table construction methods) 81

73 (0) S S (1) S L = R (2) S R Example: non-slr(1) Grammar for ssignment (3) L * R (content of R) (4) L id (5) R L L I2: Follow(R) = { =, } (1) S L. = R (5) R L. R ction(2, = ) = reduce 5 I3: ction(2, = ) =shift 6 (2) S R. = Follow(S) S => L = R => *R = R IF: Reduce on = Goto I3 Error ( Follow(S)) 82 NOT Really Reducible

74 Example: non-slr(1) Grammar for ssignment Problem: G is unambiguous SLR Shift/Reduce conflict is false, but SLR parsing table is unable to remember enough left context to decide proper action on = when seeing a string reducible to L 83

75 Why Unambiguous Yet Non-SLR(1) Some reduce actions are not really reducible by checking input against Follow(LHS) Not all symbols in FOLLOW(LHS) result in successful reduction to S. May fail after a few steps of reductions. SLR(1) states does not resolve such conflicts by using LR(0)-item defined states Need more specific constraints to rule out a subset of Follow(LHS) from indicating a reduction action 84

76 LR(1) Parsing Table Construction SLR: reduce on input a if Ii contains [.] & a FOLLOW() Not really reducible for all a FOLLOW() Only a subset (maybe proper subset) But on some cases: S a =/=> a Reduce does not produce a right sentential form E.g., S L = R =/=> S R = R although S *R = R = in follow(r) 85

77 LR(1) Parsing Table Construction Solution: Define each state by including more specific information to rule out invalid reductions Sometimes results in splitting states of the same core LR(0) items: [. ] Only dotted production (the core ) LR(1) items: [., L s] Dotted production(the core ), plus lookaheads that allow reduction upon [ ] 1 : length of L symbols 86

78 LR(1) Parsing Table Construction [., a] (& ) : L ( a ) has no effect on items of this form [., a] (i.e., = ): L has effect on items of this form Reduction is called for only when next input is a (not all terminal symbols in Follow()) Only a subset in Follow() will be the right L s Initially, only one restriction is known: [S. S, $] Infer other restrictions by closure computation 87

79 LR(1) Item and Valid Item n LR(1) item is a dotted production plus lookahead symbols: [,, a] n LR(1) item [,, a] is said to be valid for a viable prefix if r.m. derivation S * w w, where 2. a First(w) (or w= && a = $ ) The represents where we are now during parsing Left of dot: those scanned Right of dot: those to be visited later 88

80 LR(1) Parsing Table Construction Change the closure() and goto() functions of SLR parsing table construction, with initial collection: C = {closure({s. S, $})} [ B a] valid implies [B, b] valid if b is in FIRST( a) Construction method for set of LR(1) items See next few pages 89

81 LR(1): Closure(I) Given a set, I, of items Initially Closure(I) = I Repeat: for each items [ B a] in I, each production B is in G, and each terminal b in FIRST( a), include [B, b] to Closure(I). Until no more items can be added to I 90

82 LR(1): GOTO(I,X) Let J = {[ X, a] such that [ X a] is in I}. goto(i,x) = closure(j) That is: J = {} For all [ X a] in I, J += {[ X, a]} Return(closure(J)) I: [ X, a] [ X, a ] X J: [ X, a] [ X, a ] Goto(I,X) = Closure ({[ X, a], [ X, a ]}) 91

83 Sets of LR(1) Items Construction ugment the grammar with: S S, call it G Let I 0 = Closure({[S S, $] }), C = {I 0 } Repeat { } - I C, - X (N or Σ), if goto(i,x) is not already in C, then add goto(i,x) to C Until no more sets of items can be added to C 92

84 Example: resolving shift/reduce conflicts with LR(1) items G : {S S, S CC, C cc d} L(G)={ c m d c n d } => I0 ~ I9 (Fig. 4.39, p. 235 [ho 86]) I3 vs. I6: same set of LR(0) items with different lookaheads Conditions for reduction are different I3: reduce on c/d (when constructing 1 st C ) I6: reduce on $ (when constructing 2 nd C ) 93

85 SLR(1) Goto Graph S I1: S S. [$] I0: S. S S.CC C.cC C.d C I2: S C.C C.cC C.d C I5: S CC.[$] c d Follow Sets: S: {$} C: {c,d,$} c c I3: C c.c C.cC C.d C I8: C cc.,c/d/$ G: S S S CC C cc C d d d I4: C d.,c/d/$ 94

86 LR(1) Goto Graph S I1: S S., $ I0: S. S, $ S.CC, $ C.cC, c/d C.d,c/d C I2: S C.C, $ C.cC, $ C.d,$ C c I5: S CC., $ c I6: C c.c, $ C.cC, $ C.d,$ C I9: C cc.,$ c c I3: C c.c, c/d C.cC, c/d C.d,c/d C d d I7: C d.,$ I8: C cc.,c/d G: S S S CC C cc C d d d I4: C d.,c/d 95

87 Construction of Canonical LR(1) Parsing Table lgorithm 4.10 Shift: (same as SLR, ignoring L in item) Reduce on a : [,, a] ccept on $ : [S S,, $] Goto: (same as SLR) LR(1) Grammar: a grammar without conflicts (multiply defined actions) in LR(1) Parsing Table 96

88 SLR(1) vs. LR(1) LR(1): more specific states May split into states with the same core but with different lookaheads SLR(1) Grammar LR(1) Grammar Number of states LR(1) >> SLR(1) 97

89 LLR(1) Merge LR(1) states with the same core, while retaining lookahead symbols Considerably smaller than canonical LR tables Most programming language constructs can be expressed by an LLR grammar SLR and LLR have the same number of states Without/with lookahead symbols [full/subset of FOLLOW] Several hundred states for PSCL Several thousands, if using LR(1) G is an LLR(1) Grammar: if no conflicts after state merge 98

90 LLR(1) vs. LR(1) Effect of LR(1) state merge: The merging of states with common cores can never produce a shift-reduce conflict that was not present in one of the original states Because shift actions depend only on the core, not the lookahead However, a merge may produce a reducereduce conflict. Because union of lookaheads may introduce unnecessary reductions 99

91 LLR(1) vs. LR(1) Example: merging that produces reduce-reduce conflicts. LR(1) Grammar: S S S ad bbd abe be c B c Sets of LR(1) items: {[ c.,d], [B c.,e]} (valid for viable prefix ac) {[ c.,e], [B c.,d]} (valid for viable prefix bc) Merging states with common cores {[ c.,d/e], [B c.,d/e]} merging also merges loohaheads Reduce-reduce conflicts: cand B c,on inputs d and e 100

92 LLR(1) vs. LR(1) Effect of LR(1) state merge: Behave like the original, or Declare error later, but before shifting next input symbol For correct input: LR and LLR have the same sequence of shift/reduce For erroneous input: LLR requires extra reduces after LR has detected an error (but before shifting next) 101

93 Example: Merge States with Same Core Fig. 4.39: I4 vs. I7 same reduction with different lookaheads State merge: dotted rules remain, L s merged Examples: I3 + I6 => I36 I4 + I7 => I47 I8 + I9 => I89 Same as SLR(1) table (Fig. 4.41, p239, [ho 86]) 102

94 LLR(1) Parsing Table Construction (I) Method 1: (Naïve Method) [1] Construct LR(1) parsing table Very costly [#states is normally very large] [2] Merge states with the same core 103

95 LLR(1) Parsing Table Construction (II) Method 2: (Efficient Construction Method) [1] Construct kernels set of LR(0) items, from [S S] It is Possible to Compute shift/reduce/goto actions directly from kernel items kernel items: items whose dot is not at the beginning, except [S. S, $]: those not derived from closure() Can represent a set of items [2] ppend lookaheads Compute initial spontaneous lookaheads, and those item pairs that pass Propagated lookaheads 104

96 LLR(1) Parsing Table Construction (II.1) Compute shift/reduce/goto actions directly from kernel items: (pps ) Reduce: Shift: Goto: Need to pre-compute First (C) = { r.m. C * } for all pairs of nonterminals (C, ) and 105

97 LLR(1) Parsing Table Construction (II.2) Determine spontaneous and propagated lookaheads (Fig. 4.43) Compute closure({core,#}) by assuming a dummy lookahead # 106

98 LLR(1) Parsing Table Construction: Example Example: 4.46/Fig [p. 241, ho 86] Kernels of sets of LR(0) items Fig [with non-kernel items] Example: 4.47 Get Spontaneous & Propagated lookaheads Fig. 4.44: item pairs that propagate lookaheads Fig. 4.45: initial spontaneous lookahead, and multiple passes of lookahead propagation LLR(1) parsing table: Todo by yourself 107

99 LLR(1) Parsing Table Construction LLR(/LR) (Fig 4.45) SLR (Fig. 4.37) SLR: I2: shift/reduce conflict on = I2: (1) S L. = R (5) R L. LLR(/LR): I2: shift on =, reduce on $, NO conflict I2: (1) S L. = R, $ (5) R L., $ 108

100 Using mbiguous Grammar (see Handouts) 109

101 Parser Generators YCC (Slide Part II) 110

LR Parsing Techniques

LR Parsing Techniques LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser

More information

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form Bottom-up parsing Bottom-up parsing Recall Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form If α V t,thenα is called a sentence in L(G) Otherwise it is just

More information

LALR Parsing. What Yacc and most compilers employ.

LALR Parsing. What Yacc and most compilers employ. LALR Parsing Canonical sets of LR(1) items Number of states much larger than in the SLR construction LR(1) = Order of thousands for a standard prog. Lang. SLR(1) = order of hundreds for a standard prog.

More information

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S. Bottom up parsing Construct a parse tree for an input string beginning at leaves and going towards root OR Reduce a string w of input to start symbol of grammar Consider a grammar S aabe A Abc b B d And

More information

UNIT-III BOTTOM-UP PARSING

UNIT-III BOTTOM-UP PARSING UNIT-III BOTTOM-UP PARSING Constructing a parse tree for an input string beginning at the leaves and going towards the root is called bottom-up parsing. A general type of bottom-up parser is a shift-reduce

More information

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino 3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the

More information

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence. Bottom-up parsing Recall For a grammar G, with start symbol S, any string α such that S α is a sentential form If α V t, then α is a sentence in L(G) A left-sentential form is a sentential form that occurs

More information

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----

More information

MODULE 14 SLR PARSER LR(0) ITEMS

MODULE 14 SLR PARSER LR(0) ITEMS MODULE 14 SLR PARSER LR(0) ITEMS In this module we shall discuss one of the LR type parser namely SLR parser. The various steps involved in the SLR parser will be discussed with a focus on the construction

More information

S Y N T A X A N A L Y S I S LR

S Y N T A X A N A L Y S I S LR LR parsing There are three commonly used algorithms to build tables for an LR parser: 1. SLR(1) = LR(0) plus use of FOLLOW set to select between actions smallest class of grammars smallest tables (number

More information

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 8! Bo;om- Up Parsing Shi?- Reduce LR(0) automata and

More information

Context-free grammars

Context-free grammars Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,

More information

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (1) Principle of Compilers Lecture IV Part 4: Syntactic Analysis Alessandro Artale Faculty of Computer Science Free

More information

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Formal Languages and Compilers Lecture VII Part 4: Syntactic A Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Compiler Construction: Parsing

Compiler Construction: Parsing Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax

More information

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table. MODULE 18 LALR parsing After understanding the most powerful CALR parser, in this module we will learn to construct the LALR parser. The CALR parser has a large set of items and hence the LALR parser is

More information

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309 PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

More information

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Formal Languages and Compilers Lecture VII Part 3: Syntactic A Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) TD parsing - LL(1) Parsing First and Follow sets Parse table construction BU Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1) Problems with SLR Aho, Sethi, Ullman, Compilers

More information

UNIT III & IV. Bottom up parsing

UNIT III & IV. Bottom up parsing UNIT III & IV Bottom up parsing 5.0 Introduction Given a grammar and a sentence belonging to that grammar, if we have to show that the given sentence belongs to the given grammar, there are two methods.

More information

Lexical and Syntax Analysis. Bottom-Up Parsing

Lexical and Syntax Analysis. Bottom-Up Parsing Lexical and Syntax Analysis Bottom-Up Parsing Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with

More information

CS308 Compiler Principles Syntax Analyzer Li Jiang

CS308 Compiler Principles Syntax Analyzer Li Jiang CS308 Syntax Analyzer Li Jiang Department of Computer Science and Engineering Shanghai Jiao Tong University Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program.

More information

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Parse Table Construction Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Parse Tables (Review) ACTION Goto State ( ) $ X s0 shift to s2 error error goto s1

More information

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing Parsing Wrapup Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION

More information

Syn S t yn a t x a Ana x lysi y s si 1

Syn S t yn a t x a Ana x lysi y s si 1 Syntax Analysis 1 Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token, tokenval Get next token Parser and rest of front-end Intermediate representation Lexical error Syntax

More information

Downloaded from Page 1. LR Parsing

Downloaded from  Page 1. LR Parsing Downloaded from http://himadri.cmsdu.org Page 1 LR Parsing We first understand Context Free Grammars. Consider the input string: x+2*y When scanned by a scanner, it produces the following stream of tokens:

More information

Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax

More information

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1 CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts UW CSE P 501 Spring 2018

More information

LR Parsers. Aditi Raste, CCOEW

LR Parsers. Aditi Raste, CCOEW LR Parsers Aditi Raste, CCOEW 1 LR Parsers Most powerful shift-reduce parsers and yet efficient. LR(k) parsing L : left to right scanning of input R : constructing rightmost derivation in reverse k : number

More information

Compiler Construction 2016/2017 Syntax Analysis

Compiler Construction 2016/2017 Syntax Analysis Compiler Construction 2016/2017 Syntax Analysis Peter Thiemann November 2, 2016 Outline 1 Syntax Analysis Recursive top-down parsing Nonrecursive top-down parsing Bottom-up parsing Syntax Analysis tokens

More information

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous. Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or

More information

WWW.STUDENTSFOCUS.COM UNIT -3 SYNTAX ANALYSIS 3.1 ROLE OF THE PARSER Parser obtains a string of tokens from the lexical analyzer and verifies that it can be generated by the language for the source program.

More information

Compilers. Bottom-up Parsing. (original slides by Sam

Compilers. Bottom-up Parsing. (original slides by Sam Compilers Bottom-up Parsing Yannis Smaragdakis U Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Bottom-Up Parsing More general than top-down parsing And just as efficient Builds

More information

Monday, September 13, Parsers

Monday, September 13, Parsers Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions

More information

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised: EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)

More information

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States TDDD16 Compilers and Interpreters TDDB44 Compiler Construction LR Parsing, Part 2 Constructing Parse Tables Parse table construction Grammar conflict handling Categories of LR Grammars and Parsers An NFA

More information

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1 CSE 401 Compilers LR Parsing Hal Perkins Autumn 2011 10/10/2011 2002-11 Hal Perkins & UW CSE D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts 10/10/2011

More information

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant Syntax Analysis: Context-free Grammars, Pushdown Automata and Part - 4 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler

More information

Wednesday, September 9, 15. Parsers

Wednesday, September 9, 15. Parsers Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468 Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

More information

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs: What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

More information

Wednesday, August 31, Parsers

Wednesday, August 31, Parsers Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically

More information

SYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic

More information

Syntax Analysis Part I

Syntax Analysis Part I Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,

More information

Bottom-Up Parsing II. Lecture 8

Bottom-Up Parsing II. Lecture 8 Bottom-Up Parsing II Lecture 8 1 Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk 2 Recall: he Stack Left string can be implemented by a stack

More information

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6 Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient

More information

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh. Bottom-Up Parsing II Different types of Shift-Reduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Doesn

More information

Acknowledgements. The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal

Acknowledgements. The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal Acknowledgements The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error

More information

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing. LR Parsing Compiler Design CSE 504 1 Shift-Reduce Parsing 2 LR Parsers 3 SLR and LR(1) Parsers Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26

More information

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis. Topics Chapter 4 Lexical and Syntax Analysis Introduction Lexical Analysis Syntax Analysis Recursive -Descent Parsing Bottom-Up parsing 2 Language Implementation Compilation There are three possible approaches

More information

Concepts Introduced in Chapter 4

Concepts Introduced in Chapter 4 Concepts Introduced in Chapter 4 Grammars Context-Free Grammars Derivations and Parse Trees Ambiguity, Precedence, and Associativity Top Down Parsing Recursive Descent, LL Bottom Up Parsing SLR, LR, LALR

More information

Chapter 4: LR Parsing

Chapter 4: LR Parsing Chapter 4: LR Parsing 110 Some definitions Recall For a grammar G, with start symbol S, any string α such that S called a sentential form α is If α Vt, then α is called a sentence in L G Otherwise it is

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

Lecture Bottom-Up Parsing

Lecture Bottom-Up Parsing Lecture 14+15 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Example CFG 1. S S 2. S AyB 3. A ab 4. A cd 5. B z 6. B wz 2 Stacks in

More information

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Bottom-Up Parsing II Lecture 8 Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk Prof. Aiken CS 13 Lecture 8 1 Prof. Aiken CS 13 Lecture 8 2

More information

CS 4120 Introduction to Compilers

CS 4120 Introduction to Compilers CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: Bottom-Up Parsing 9/9/09 Bottom-up parsing A more powerful parsing technology LR grammars -- more expressive than LL can handle

More information

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing Lecture 7: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general

More information

shift-reduce parsing

shift-reduce parsing Parsing #2 Bottom-up Parsing Rightmost derivations; use of rules from right to left Uses a stack to push symbols the concatenation of the stack symbols with the rest of the input forms a valid bottom-up

More information

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing Lecture 8: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general

More information

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012 LR(0) Drawbacks Consider the unambiguous augmented grammar: 0.) S E $ 1.) E T + E 2.) E T 3.) T x If we build the LR(0) DFA table, we find that there is a shift-reduce conflict. This arises because the

More information

In One Slide. Outline. LR Parsing. Table Construction

In One Slide. Outline. LR Parsing. Table Construction LR Parsing Table Construction #1 In One Slide An LR(1) parsing table can be constructed automatically from a CFG. An LR(1) item is a pair made up of a production and a lookahead token; it represents a

More information

4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

More information

Chapter 4. Lexical and Syntax Analysis

Chapter 4. Lexical and Syntax Analysis Chapter 4 Lexical and Syntax Analysis Chapter 4 Topics Introduction Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing Copyright 2012 Addison-Wesley. All rights reserved.

More information

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412 COMP 412 FALL 2018 source code IR Front End Optimizer Back End IR target

More information

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ  S. 2. S  AyB. 3. A  ab. 4. Example CFG Lectures 16 & 17 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Fall 2016 1. Sʹ " S 2. S " AyB 3. A " ab 4. A " cd Matt Crane University of Waterloo 5. B " z 6. B " wz 2 LL(1)

More information

4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11

More information

SLR parsers. LR(0) items

SLR parsers. LR(0) items SLR parsers LR(0) items As we have seen, in order to make shift-reduce parsing practical, we need a reasonable way to identify viable prefixes (and so, possible handles). Up to now, it has not been clear

More information

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution Exam Facts Format Wednesday, July 20 th from 11:00 a.m. 1:00 p.m. in Gates B01 The exam is designed to take roughly 90

More information

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

More information

Bottom-up Parser. Jungsik Choi

Bottom-up Parser. Jungsik Choi Formal Languages and Compiler (CSE322) Bottom-up Parser Jungsik Choi chjs@khu.ac.kr * Some slides taken from SKKU SWE3010 (Prof. Hwansoo Han) and TAMU CSCE434-500 (Prof. Lawrence Rauchwerger) Bottom-up

More information

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1 CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008 1/15/2008 2002-08 Hal Perkins & UW CSE C-1 Agenda for Today Parsing overview Context free grammars Ambiguous grammars Reading:

More information

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University Compilation 2012 Parsers and Scanners Jan Midtgaard Michael I. Schwartzbach Aarhus University Context-Free Grammars Example: sentence subject verb object subject person person John Joe Zacharias verb asked

More information

Parsing. Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018

Parsing. Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Parsing Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax tree Semantic

More information

How do LL(1) Parsers Build Syntax Trees?

How do LL(1) Parsers Build Syntax Trees? How do LL(1) Parsers Build Syntax Trees? So far our LL(1) parser has acted like a recognizer. It verifies that input token are syntactically correct, but it produces no output. Building complete (concrete)

More information

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides

More information

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis Chapter 4 Lexical and Syntax Analysis Introduction - Language implementation systems must analyze source code, regardless of the specific implementation approach - Nearly all syntax analysis is based on

More information

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite LALR r Driver Action Table for CSX-Lite Given the GoTo and parser action tables, a Shift/Reduce (LALR) parser is fairly simple: { S 5 9 5 9 void LALRDriver(){ Push(S ); } R S R R R R5 if S S R S R5 while(true){

More information

VIVA QUESTIONS WITH ANSWERS

VIVA QUESTIONS WITH ANSWERS VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the

More information

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp. 11-711 Algorithms for NL L arsing eading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section 10.6-10.7, pp. 248 256 Shift-educe arsing A class of parsers with the following principles:

More information

Context-Free Grammars and Parsers. Peter S. Housel January 2001

Context-Free Grammars and Parsers. Peter S. Housel January 2001 Context-Free Grammars and Parsers Peter S. Housel January 2001 Copyright This is the Monday grammar library, a set of facilities for representing context-free grammars and dynamically creating parser automata

More information

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

More information

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing ICOM 4036 Programming Languages Lexical and Syntax Analysis Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing This lecture covers review questions 14-27 This lecture covers

More information

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1 CS453 : JavaCUP and error recovery CS453 Shift-reduce Parsing 1 Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm using

More information

Conflicts in LR Parsing and More LR Parsing Types

Conflicts in LR Parsing and More LR Parsing Types Conflicts in LR Parsing and More LR Parsing Types Lecture 10 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 2 Due Friday, Apr. 24, 11:55pm The usual lecture time is being replaced by a discussion

More information

Syntax Analyzer --- Parser

Syntax Analyzer --- Parser Syntax Analyzer --- Parser ASU Textbook Chapter 4.2--4.9 (w/o error handling) Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 A program represented by a sequence of tokens

More information

CS415 Compilers. LR Parsing & Error Recovery

CS415 Compilers. LR Parsing & Error Recovery CS415 Compilers LR Parsing & Error Recovery These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: LR(k) items The LR(1) table construction

More information

Bottom-Up Parsing LR Parsing

Bottom-Up Parsing LR Parsing Bottom-Up Parsing LR Parsing Maryam Siahbani 2/19/2016 1 What we need for LR parsing LR0) states: Describe all possible states in which parser can be Parsing table ransition between LR0) states Actions

More information

CS 406/534 Compiler Construction Parsing Part I

CS 406/534 Compiler Construction Parsing Part I CS 406/534 Compiler Construction Parsing Part I Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and Dr.

More information

Introduction to Parsing. Comp 412

Introduction to Parsing. Comp 412 COMP 412 FALL 2010 Introduction to Parsing Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

More information

Syntactic Analysis. Top-Down Parsing

Syntactic Analysis. Top-Down Parsing Syntactic Analysis Top-Down Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make

More information

Parsing II Top-down parsing. Comp 412

Parsing II Top-down parsing. Comp 412 COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1 CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 C-1 Administrivia Project partner signup: please find a partner and fill out the signup form by noon

More information

ECE 573 Midterm 1 September 29, 2009

ECE 573 Midterm 1 September 29, 2009 ECE 573 Midterm 1 September 29, 2009 Name: Purdue email: Please sign the following: I affirm that the answers given on this test are mine and mine alone. I did not receive help from an person or material

More information