Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.

Similar presentations


Presentation on theme: "CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error."— Presentation transcript:

1 CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.

2 Parsing Top-down Parsing 1. Root node  leaves 2. Abstract  concrete 3. Uses grammar left  right 4. Works by "guessing" Parsing -- Syntax/Semantic Analysis Bottom-up Parsing 1. Leaves  root node 2. Concrete  abstract 3. Uses grammar right  left 4. Works by "pattern matching"

3 Introduction Top down parsing –Scan across the string to be parsed –Attempt to find patterns that match the right hand side of a rule –Reduce them to the left hand side of the rule –If the eventual result is reduction to the start symbol then parse is successful

4 Imagine... We are parsing 1 + 2 * 3 or num + num * num We need some way to make sure that we don't turn the num + num into + and reduce it to Can num + num be reduced to ? Why is it a problem?

5 Problem... We cannot reduce * num What we need is a way of recognizing that we must reduce first num * num num + num * num

6 Recall our expression grammar ::= + | ::= + | ::= * | ::= * | ::= '(' ')' | num | id ::= '(' ')' | num | id +It would suggest that what follows a + must be a term. *It would also suggest that if a num is followed by a * then we will somehow need to find a factor to perform ::= * ::= *

7 Bottom Up Parsing Bottom up parsing tries to group tokens into things it can reduce (based on a rule in the grammar) in the correct sequence This group of symbols is known as a handle. Wirth-WeberHandles are indicated using special symbols known as Wirth-Weber operators likeThese symbols function like parentheses which can be used to indicate precedence 1 + (2 * 3) We will determine where to put these symbols by examining the grammar and developing additional information to assist us

8 Wirth-Weber Operators x < yy has higher precedence than x (We expect y will be involved in a reduction before x) x = yx and y have equal precedence (We expect x and y will be involved in a reduction together) x > yx has higher precedence than y (We expect y will be involved in a reduction before x)

9 Bottom Up Parsing Two Things must be understood: –Given the ability to determine precedence between symbols how can we use this to parse a string? –How do we determine this precedence between symbols/tokens? We deliberately choose to explain in this order and we'll use a very simple grammar to explain

10 Recall Well Formed Formulae ::= p | q | r | s ::= N ::= ( C | A | K | E ) Suppose we wish to parse CANpqp

11 Bottom Up Parsing C A N p q p

12 Bottom Up Parsing < C A N p q p We can assume that the string has a leading less than precedence operator

13 Bottom Up Parsing < C < A N p q p We move from left to right (and in fact in reality we would normally proceed by asking a lexical scanner for the next token As we get to each token or symbol we get its precedence from a precedence table that we'll present later

14 Bottom Up Parsing < C < A < N p q p We continue in this fashion as long as we place the < and = operators

15 Bottom Up Parsing < C < A < N < p q p We continue in this fashion as long as we place the < and = operators

16 Bottom Up Parsing < C < A < N < p q p We continue in this fashion as long as we place < and = operators We are postponing the discussion on the precedence table because this part of the algorithm must be clear to be able to understand where the precedence table comes from!

17 Bottom Up Parsing q p When we place a > operator we have found a handle or something that we should be able to reduce We examine the rules of the grammer to see if there is a rule to match this handle

18 Bottom Up Parsing q p We find ::= p If no rule is found we have a parse errorNote: If no rule is found we have a parse error

19 Bottom Up Parsing q p Note that we have removed the entire handle and replaced it with the appropriate symbol from the grammar. We "backup" to examine the relationship between N and

20 Bottom Up Parsing q p We continue

21 Bottom Up Parsing > q p We continue, again, until we find a handle

22 Bottom Up Parsing q p We can reduce this using the rule ::= N

23 Bottom Up Parsing q p We continue

24 Bottom Up Parsing < q p We continue

25 Bottom Up Parsing p We can reduce this one also

26 Bottom Up Parsing p Once again backtracking

27 Bottom Up Parsing = p Once again backtracking

28 Bottom Up Parsing = > p Continuing

29 Bottom Up Parsing p Continuing

30 Bottom Up Parsing p Continuing

31 Bottom Up Parsing < p Continuing

32 Bottom Up Parsing A greater than precedence symbol is assumed after the last symbol in the input.

33 Bottom Up Parsing Continuing

34 Bottom Up Parsing = Continuing

35 Bottom Up Parsing = > Again a trailing greater than can be added

36 Bottom Up Parsing Since is our start symbol (and we have nothing left over) Successful Parse!

37 Bottom Up Parsing What kind of algorithm? –Stack based –Known as semantic stack or shift/reduce algorithm We won't code this algorithm but understanding this parsing technique will make some concepts found in yacc clearer

38 Example Our stream of tokens C A N p q p

39 Example Our stream of tokens C A N p q p Stack

40 Example Our stream of tokens C A N p q p Stack Color Commentary Welcome to Monday Night Parsing

41 Example Our stream of tokens A N p q p < C Stack We will place the Wirth-Weber operator and following token on the stack. Encountering the end of a handle > will initiate additional processing

42 Example Our stream of tokens N p q p < A < C Stack Working

43 Example Our stream of tokens p q p < N < A < C Stack Working

44 Example Our stream of tokens q p < p < N < A < C Stack Working

45 Example Our stream of tokens q p < p < N < A < C Stack Now, between the next token in the stream (q) and the symbol on top of the stack, we find a greater than precedence > indicating we have the end of a handle. We must now go down the stack and search for the beginning

46 Example Our stream of tokens q p < N < A < C Stack We can remove the p and looking at the grammar determine it can be reduced to be a. We then examine the in relation to the top of the stack

47 Example Our stream of tokens q p = < N < A < C Stack We can remove the p and looking at the grammar determine it can be reduced to be a. We then examine the in relation to the top of the stack

48 Example Our stream of tokens q p = < N < A < C Stack Looking at the followed bt the q we again find a greater than precedence relationship. We find that we can reduce the N to a.

49 Example Our stream of tokens q p < A < C Stack Now have from previous reduction. Compare it with A

50 Example Our stream of tokens q p = < A < C Stack Now have from previous reduction. Compare it with A

51 Example Our stream of tokens p < q = < A < C Stack Working

52 Example Our stream of tokens p = < A < C Stack q followed by p yields greater than allowing us to reduce the q to a

53 Example Our stream of tokens p = < A < C Stack followed by p yields greater than > so we reduce the A to a

54 Example Our stream of tokens p = < C Stack C followed by a yields equal precedence

55 Example Our stream of tokens < p = < C Stack Working

56 Example Our stream of tokens EOS < p = < C Stack End of input stream (EOS) allows us to place greater than precedence operator

57 Example Our stream of tokens EOS = < C Stack End of input stream allows us to reduce C

58 Example Our stream of tokens Stack End of input stream allows us to place greater than precedence operator allowing reduction to final Since is our start symbol: Successful Parse

59 Questions?

60 Constructing the Precedence Table Being a table which when given two successive symbols will return to us the correct interstitial Wirth-Weber Operator

61 Precedence Table CAKE † pqrs † N CAKE † pqrs † N Left Hand Symbol Right Hand Symbol

62 The Grammar ::= p | q | r | s ::= N ::= C ::= A ::= K ::= E Consider the previous slides As we move through a string we want to capture as a handle an occurrences of the rules above

63 The Grammar ::= p | q | r | s ::= N = ::= C = = ::= A = = ::= K = = ::= E = = Does this seem logical???

64 Precedence Table CAKE † pqrs † N CAKE † pqrs † N = = = Left Hand Symbol Right Hand Symbol

65 Now consider Whenever we come across a p, q, r or s We will want to follow this sequence A p A < p A So we might reason that any of the terminals C, A, K, E or N followed by a p, q, r,s will be < And a p, q, r or s will always be followed by a >

66 Precedence Table CAKE † pqrs † N CAKE † pqrs † N = = > = > < < > < > Left Hand Symbol Right Hand Symbol

67 We continue to use this reasoning Note that anything followed by a C, A, K, E or N should have < precedence to allow a proper WFF to be formed first i.e. C ??? Note also that the exception which we have already taken care of is p, q, r or s followed by C, A, K, E or N

68 Precedence Table CAKE † pqrs † N CAKE † pqrs † N = = > = < < > < < < > < < < > < Left Hand Symbol Right Hand Symbol Note: The exception which we have already taken care of is p, q, r or s followed by C, A, K, E or N

69 So It appears that this technique is quite simple We –Construct a grammar –Examine it to produce a precedence table –Write a program to execute our stack based algorithm Not so fast! There are two issues to deal with –Simple precedence –Size

70 Simple Precedence The technique we have been using is known as Bottom-Up Parsing or Shift-Reduce Parsing The action we take during operation is based on the precedence relationship found x < y x = y x > y What happens if there is no relationship in the table? What happens if there is more than one relationship in the table??? Shift Reduce

71 More than one relationship! Gadzooks! Actually we could deal with < = using lookahead (we'll see that in a moment) However rules that allowed >= or >< would be known as a shift reduce error Speaking of errors finding two rules that match is known as a reduce-reduce error Not finding a rule that matches is a syntax error

72 But how can we have multiple precedence relationships?

73 Recall our expression grammar ::= + | ::= * | ::= '(' ')' | num | id

74 Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id)

75 Some things are impossible + () )(

76 Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id)

77 We know From our WFF example we note that certain items must be reduced immediately (e.g. p, q, r and s) In a similar fashion we have ::= num | id So, anything followed by a num or an id will have

78 << << << >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id)

79 Precedence Between symbols there must be ? ::= + | ::= * | ::= '(' ')' | num | id

80 Precedence Between symbols there must be = ::= = + = | ::= = * = | ::= '(' = = ')' | num | id

81 == = = << = << << >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) =

82 Precedence To determine "end points" we must look at multiple rules to see how they interact... <wff> N<wff>= Do not be alarmed We are returning to the wff example just for a moment

83 Precedence To determine "end points" we must look at multiple rules to see how they interact... <wff> N<wff>= To determine what goes here... Do not be alarmed We are returning to the wff example just for a moment

84 Precedence To determine "end points" we must look at multiple rules to see how they interact... <wff> N<wff>= We look here. Do not be alarmed We are returning to the wff example just for a moment

85 Precedence ::= + | ::= * | ::= '(' ')' | num | id * ? ( What is the relationship between * and ( ?

86 Precedence ::= + | ::= * | ::= '(' ')' | num | id * ? ( = = ) What is the relationship between * and ( If we have parentheses it must be this form

87 Precedence ::= + | ::= * | ::= '(' ')' | num | id * = * = * < ( = = ) We go up the parse tree. Since ( ) will be a factor and a factor will need to be reduced as part of * we conclude that we will need to reduce the ( ) first

88 Precedence ::= + | ::= * | ::= '(' ')' | num | id + ? ( What is the relationship between + and (

89 Precedence ::= + | ::= * | ::= '(' ')' | num | id + ? ( = = ) Again the grammar reveals that ( must come from ( )

90 Precedence ::= + | ::= * | ::= '(' ')' | num | id + + + < ( = = ) = = ) + = + = We examine the parse tree noting that a + can only be followed by a followed by a

91 == = =<< =<< << >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) = << <

92 Continuing to analyze in this way...

93 < == >=> >>> <<< =<<< <<< >>> >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) = =

94 < == >=> >>> <<< =<<< <<< >>> >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) '('...

95 Now for the complex part Consider ( followed by ( Is it ( ) = or ( + < ::= + | ::= * | ::= '(' ')' | num | id

96 Or Consider + followed by + Is it + + = + ) = + * < ::= + | ::= * | ::= '(' ')' | num | id

97 < == >=> >>> = <<<< =<<< <<< >>> >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) = <

98 < == >=> >>> <<< =<<< <<< >>> >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) = <

99 < == >=> >>> <<< =<<< <<< >>> >>> >>> Precedence Table ::= + | ::= * | ::= '(' ')' | num | id L R + * ( ) num id +(num*id) = < '(' = ')' '(' +

100 Resolving Ambiguity + = + * Solve by lookahead: + = + or ) + * Ambiguity can be resolved by increasing k in LR(k) but that's not the only way: We could rewrite grammar

101 Original Grammar ::= + | ::= * | ::= '(' ')' | num | id

102 Sources of Ambiguity ::= + | ::= * | ::= '(' ')' | num | id

103 Add 2 New Rules ::= + | ::= * | ::= '(' ')' | num | id ::=

104 Modify ::= + | ::= * | ::= '(' ')' | num | id ::=

105 Original Grammar ::= + | ::= * | ::= '(' ')' | num | id ::= Rewritten Grammar ::= + | ::= * | ::= '(' ')' | num | id

106 Bottom-Up Parsing No issues regarding left-recursive versus right- recursive such as those found with Top-down parsing Note: There are grammars that will break a bottom- up parser.

107 So It appears that this technique is quite simple We –Construct a grammar –Examine it to produce a precedence table –Write a program to execute our stack based algorithm Not so fast! There are two issues to deal with –Simple precedence –Size

108 Performance Size of table is O(n 2 ) For a "real" language this can be a problem One possibility: Use operator precedence –Only uses terminals –Thus the table size is not affected by adding non- terminals We will not go into details of Operator Precedence Tables You should be aware that they exist

109 Question Where do precedence relationships come from? –Make a table by hand –Write a program to make table How such a program works or how to write it are topics beyond the scope of this course.

110 Example

111 + 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

112 + > + 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

113 + > + 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

114 + > + = + 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

115 + > + = + = + < num 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

116 + > + = + = + < num = + * 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

117 + > + = + = + < num = + * = + > * 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

118 + > + = + = + < num = + * = + > * = + = * 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

119 + > + = + = + < num = + * = + > * = + = * = + = * < num 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

120 + > + = + = + < num = + * = + > * = + = * = + = * < num = + = * 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

121 + > + = + = + < num = + * = + > * = + = * = + = * < num = + = * = + = * = > 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

122 + > + = + = + < num = + * = + > * = + = * = + = * < num = + = * = + = * = > = + = > 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

123 + > + = + = + < num = + * = + > * = + = * = + = * < num = + = * = + = * = > = + = > > 1 + 2 * 3 Tokenized: num + num * num 1 + 2 * 3 Tokenized: num + num * num

124 Questions?

125


Download ppt "CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error."

Similar presentations


Ads by Google