090_Top_Down_Parsing

090_Top_Down_Parsing - CS143 Handout 09 Summer 2011 June 27th 2011 Top-Down Parsing Handout written by Maggie Johnson and revised by Julie Zelenski

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS143 Handout 09 Summer 2011 June 27 th , 2011 Top-Down Parsing Handout written by Maggie Johnson and revised by Julie Zelenski. Possible Approaches The syntax analysis phase of a compiler verifies that the sequence of tokens extracted by the scanner represents a valid sentence in the grammar of the programming language. There are two major parsing approaches: top-down and bottom-up . In top-down parsing, you start with the start symbol and apply the productions until you arrive at the desired string. In bottom-up parsing, you start with the string and reduce it to the start symbol by applying the productions backwards. As an example, let’s trace through the two approaches on this simple grammar that recognizes strings consisting of any number of a’s followed by at least one (and possibly more) b’s: S –> AB A –> aA | ε B –> b | bB Here is a top-down parse of aaab. We begin with the start symbol and at each step, expand one of the remaining nonterminals by replacing it with the right side of one of its productions. We repeat until only terminals remain. The top-down parse produces a leftmost derivation of the sentence. S AB S –> AB aAB A –> aA aaAB A –> aA aaaAB A –> aA aaa ε B A –> ε aaab B –> b A bottom-up parse works in reverse. We begin with the sentence of terminals and each step applies a production in reverse, replacing a substring that matches the right side with the nonterminal on the left. We continue until we have substituted our way back to the start symbol. If you read from the bottom to top, the bottom-up parse prints out a rightmost derivation of the sentence. aaab aaa ε b (insert ε ) aaaAb A –> ε aaAb A –> aA aAb A –> aA Ab A –> aA AB B –> b S S –> AB
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 In creating a parser for a compiler, we normally have to place some restrictions on how we process the input. In the above example, it was easy for us to see which productions were appropriate because we could see the entire string aaab . In a compiler’s parser, however, we don’t have long-distance vision. We are usually limited to just one-symbol of lookahead . The lookahead symbol is the next symbol coming up in the input. This restriction certainly makes the parsing more challenging. Using the same grammar from above, if the parser sees only a single b in the input and it cannot lookahead any further than the symbol we are on, it can’t know whether to use the production B –> b or B –> bB . Backtracking One solution to parsing would be to implement backtracking . Based on the information the parser currently has about the input, a decision is made to go with one particular production. If this choice leads to a dead end, the parser would have to backtrack to that decision point, moving backwards through the input, and start again making a different choice and so on until it either found the production that was the appropriate one or ran out of choices. For example, consider this simple grammar: S –> bab | bA A –> d | cA Let’s follow parsing the input
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 07/18/2011.

Page1 / 16

090_Top_Down_Parsing - CS143 Handout 09 Summer 2011 June 27th 2011 Top-Down Parsing Handout written by Maggie Johnson and revised by Julie Zelenski

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online