1.27k likes | 1.46k Vues
Revised Version. CS 2130. Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing. Warning: The precedence table given for the Wff grammar is in error. Top-down Parsing 1. Root node leaves 2. Abstract concrete 3. Uses grammar left right 4. Works by "guessing". Bottom-up Parsing
E N D
Revised Version CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Top-down Parsing 1. Root node leaves 2. Abstract concrete 3. Uses grammar left right 4. Works by "guessing" Bottom-up Parsing 1. Leaves root node 2. Concrete abstract 3. Uses grammar right left 4. Works by "pattern matching" Parsing • Parsing -- Syntax/Semantic Analysis
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
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<expr> + <term> and reduce it to <expr> • Can num + num be reduced to <expr> ? • Why is it a problem?
Problem... • We cannot reduce <expr> * num • What we need is a way of recognizing that we must reduce first num + num * num
Recall our expression grammar <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | 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 <term> ::= <term> * <factor>
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. • Handles are indicated using special symbols known as Wirth-Weber operators • These 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
Wirth-Weber Operators x <• y y has higher precedence than x (We expect y will be involved in a reduction before x) x = y x and y have equal precedence (We expect x and y will be involved in a reduction together) x •> y x has higher precedence than y (We expect y will be involved in a reduction before x)
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
Recall Well Formed Formulae <wff> ::= p | q | r | s<wff> ::= N <wff><wff> ::= ( C | A | K | E ) <wff> <wff> Suppose we wish to parse CANpqp
Bottom Up Parsing C A N p q p
Bottom Up Parsing <• C A N p q p • We can assume that the string has a leading less than precedence operator
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
Bottom Up Parsing <• C <• A <• N p q p • We continue in this fashion as long as we place the < and = operators
Bottom Up Parsing <• C <• A <• N <• p q p • We continue in this fashion as long as we place the < and = operators
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!
Bottom Up Parsing <• C <• A <• N <• p •> 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
Bottom Up Parsing <• C <• A <• N <• p •> q p • We find <wff> ::= p • Note: If no rule is found we have a parse error
Bottom Up Parsing <• C <• A <• N<wff> 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 <wff>
Bottom Up Parsing <• C <• A <• N = <wff> q p • We continue
Bottom Up Parsing <• C <• A <• N = <wff> •> q p • We continue, again, until we find a handle
Bottom Up Parsing <• C <• A <wff> q p • We can reduce this using the rule <wff> ::= N <wff>
Bottom Up Parsing <• C <• A = <wff> q p • We continue
Bottom Up Parsing <• C <• A = <wff> <• q p • We continue
Bottom Up Parsing <• C <• A = <wff> <• q •> p • We can reduce this one also
Bottom Up Parsing <• C <• A = <wff> <wff> p • Once again backtracking
Bottom Up Parsing <• C <• A = <wff> = <wff> p • Once again backtracking
Bottom Up Parsing <• C <• A = <wff> = <wff> •> p • Continuing
Bottom Up Parsing <• C <wff> p • Continuing
Bottom Up Parsing <• C = <wff> p • Continuing
Bottom Up Parsing <• C = <wff> <• p • Continuing
Bottom Up Parsing <• C = <wff> <• p •> • A greater than precedence symbol is assumed after the last symbol in the input.
Bottom Up Parsing <• C = <wff> <wff> • Continuing
Bottom Up Parsing <• C = <wff>= <wff> • Continuing
Bottom Up Parsing <• C = <wff> = <wff> > • Again a trailing greater than can be added
Bottom Up Parsing <wff> • Since <wff> is our start symbol • (and we have nothing left over) • Successful Parse!
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
Example Our stream of tokens C A N p q p
Example Stack Our stream of tokens C A N p q p
Example Stack Our stream of tokens C A N p q p • Color Commentary • Welcome to Monday Night Parsing
Example Stack Our stream of tokens A N p q p <• C We will place the Wirth-Weber operator and following token on the stack. Encountering the end of a handle •> will initiate additional processing
Example Stack Our stream of tokens N p q p <• A <• C Working
Example Stack Our stream of tokens p q p <• N <• A <• C Working
Example Stack Our stream of tokens q p <• p <• N <• A <• C Working
Example Stack Our stream of tokens q p <• p <• N <• A <• C 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
Example Stack Our stream of tokens q p <• N <• A <• C We can remove the p and looking at the grammar determine it can be reduced to be a <wff>. We then examine the <wff> in relation to the top of the stack
Example Stack Our stream of tokens q p = <wff> <• N <• A <• C We can remove the p and looking at the grammar determine it can be reduced to be a <wff>. We then examine the <wff> in relation to the top of the stack
Example Stack Our stream of tokens q p = <wff> <• N <• A <• C Looking at the <wff> followed bt the q we again find a greater than precedence relationship. We find that we can reduce the N <wff> to a <wff>.
Example Stack Our stream of tokens q p <• A <• C Now have <wff> from previous reduction. Compare it with A
Example Stack Our stream of tokens q p = <wff> <• A <• C Now have <wff> from previous reduction. Compare it with A