cs160-lec8

cs160-lec8 - CMPSC 160 Translation of Programming Languages...

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

View Full Document Right Arrow Icon
1 CMPSC 160 Translation of Programming Languages Lecture 8: Introduction to Bottom-up Parsing and Shift-Reduce Parsing Where are we in the process? compiler scan parse top down parsing with backtracking predictive parsing top down bottom up
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 Parsing Techniques Top-down parsers (LL(1), recursive descent parsers) Start at the root of the parse tree from the start symbol and grow toward leaves (similar to a derivation) Pick a production and try to match the input Bad “pick” may need to backtrack Some grammars are backtrack-free (predictive parsing) Bottom-up parsers (LR(1), shift-reduce parsers) Start at the leaves and grow toward root We can think of the process as reducing the input string to the start symbol At each reduction step, a particular substring matching the right-side of a production is replaced by the symbol on the left-side of the production Bottom-up parsers handle a large class of grammars Bottom-Up Parsers Bottom up parsers handle a larger class of useful grammars Why is that? With both techniques, we are trying to build a tree that connects the input string to our start symbol (S) called a parse tree Let’s look at what what each technique looks like halfway through parsing an input string
Background image of page 2
3 S look-ahead input string ? start symbol left-to-right scan S A B ? look-ahead fringe of the parse tree left-most derivation Top-down Parsing D C When we start with a predictive top down parser, the look-ahead symbol we read from our input string MUST fully specify the parse tree from S to the input symbol. In the example, we have to know that S AB before we even see any of B Top-Down Parsing C A ? look-ahead upper fringe of the parse tree Bottom-up Parsing D S right-most derivation in reverse In a bottom up parser, we can delay this decision because we only need to build the tree up above the part of the input string we have examined so far. In the graphical example on the left, you can see that even though we are at the same point in the input string, the production S AB has not been specified yet. This delayed decision allows us to parse more grammars than predictive top-down parsing (LL). As a nice side effect, bottom-up parsing allows us to handle left-recursive grammars without modification Bottom-Up Parsing
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 LR(k) versus LL(k) A k-look-ahead top-down predictive parser: LL( k ) Parser must select the reduction (production) based on 1 The complete left context 2 The next k terminals A k-look-ahead bottom-up parser: LR( k ) Each reduction (production) in the parse is detectable with 1 the complete left context, 2 the reducible phrase, itself, and 3 the k terminal symbols to its right Thus, LR( k ) examines more context From the Bottom Up How does a bottom-up parser work – The main idea of bottom-up parsing is to find the rightmost-
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

cs160-lec8 - CMPSC 160 Translation of Programming Languages...

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

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