7.3. Bottom-Up Parsing

A bottom-up parser builds the parse tree from the bottom to the top. Bottom-up parsers make much less extravagant predictions and can handle grammars that top-down parsers cannot.

Let's do a bottom-up parse of n + n * n using the following grammar, from a previous page

E T
E E + T
T F
T T * F
F n
F ( E )

To start off, the first token n, is added.

   n

There is only one production Fn that can generate n.

   F
   |
   n

Only two productions, TF and TT + F, have F on the right-hand side. But only one of those starts with F, so we need to use that one.

   T
   |
   F
   |
   n

The same reasoning says to choose production ET next.

   E
   |
   T
   |
   F
   |
   n

Now the lookahead is +, and the only production that makes sense to build above the E is EE + T.

But a bottom-up parser builds the parse tree strictly from bottom to top. Instead of building above E, it builds a leaf holding token +.

   E
   |
   T
   |
   F
   |
   n   +

The next token is n. The parser creates a new leaf holding n and reasons as above, up to a point.

   E
   |
   T      T
   |      |
   F      F
   |      |
   n   +  n

The next two tokens are * and n. Keeping with the bottom-up philosophy, the parser builds a leaf holding *, and after that a leaf holding n. Then it builds what it must above n.

   E
   |
   T      T
   |      |
   F      F   F
   |      |   |
   n   +  n * n

But, out of the blue, we see T * F at the right end of the 5 small trees, which is precisely the right-hand side of production TT * F.

The parser does not need to predict that something of the form T * F is coming. It finds that empirically, after the right-hand side has already been built.

   E       T
   |      /|\
   T     T * F
   |     |   |
   F     F   n
   |     |    
   n  +  n    

Once again, the right-hand side of a production pops up. This time, the production is EE + T.

       E
      /|\
     / + \
    /     \
   E       T
   |      /|\
   T     T * F
   |     |   |
   F     F   n
   |     |   
   n     n   

Bottom-up parsers build the parse tree from right to left

You can see from the example that, although a bottom-up parser reads the sequence of tokens from left to right, it builds the parse tree from right to left.

A bottom-up parser can be thought of as creating a rightmost derivation.