Chapter_5 - 1 Chapter 5: Bottom-Up Parsing (Shift-Reduce)...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 5: Bottom-Up Parsing (Shift-Reduce) 2- attempts to construct a parse tree for an input string beginning at the leaves (the bottom ) and working towards the root (the top ). i.e., reduce a string w to the start symbol of a grammar. At each reduction step a particular substring matching the right side of a production (grammar rule) is replaced by the left nonterminal symbol. A rightmost derivation is traced out in reverse . Objectives of Bottom-Up Parsing 3 An Example S -> aABe A -> Abc | b B -> d w = abbcde S => aABe => aAde => aAbcde => abbcde LR parsing: abbcde ==> aAbcde ==> aAde ==> aABe ==> S (traced out in reverse) rm rm rm rm S a A B e d 1 2 A b c 3 b 4 LR parsing: a b bcde ==> a A bc de ==> a A de ==> aAB e ==> S 5 Stack Implementation of Bottom-Up Parsing There are four actions a parser can make: (1) shift (2) reduce (3) accept (4) error. There is an important fact that justifies the use of a stack in shift-reduce parsing: the handle will always eventually appear on top of the stack, never inside. Initially, (stack) $ w$ (input buffer) Finally, (stack)$S $ (input buffer) // S is a start symbol of grammar G 6 7 8 $ . . . 9 10 $ $ $ $ $ $ $ $ $ $ $ $ $ $ 11 Handles A substring that matches the right side of a production, and whose reduction to the nonterminal on the left side of the production represents one step along the reverse of a rightmost derivation. However, in many cases the leftmost substring ' β ' that matches the right side of some production A -> β is not a handle, because a reduction by the production yields a string that cannot be reduced to the start symbol. 12 Handles (Continued) A handle of a right sentential form γ is a production A -> β and a position of γ where the string β may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of γ . i.e., S => α A w => α β w, then A -> β in the position following α is a handle of αβ w. The string w to the right of the handle contains only terminal symbols . Handle = leftmost complete subtree. * 13 Handle Pruning A rightmost derivation in reverse can be obtained by "handle pruning". Two Problems: 1. To locate the substring to be reduced in right-sentential form. 2. To determine the production with the same substring on the right-hand side to be chosen. 14 Write a LL parser in ? and a LR parser in Yacc separately for the TINY language defined in Fig. 3.6. The parsers will parse any input legal TINY program and generate a parse tree for it. Use the program in Fig. 3.8 to test your parsers and turn in the tested results with your parser codes. Assignment #4 15 Viable Prefixes The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes....
View Full Document

This note was uploaded on 06/28/2011 for the course ENGINEERIN 100 taught by Professor Yangwei during the Spring '10 term at National Cheng Kung University.

Page1 / 57

Chapter_5 - 1 Chapter 5: Bottom-Up Parsing (Shift-Reduce)...

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

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