8_syntax_analysis_p2.pdf - Bottom-Up Parsing 1 Dr Alekha Kumar Mishra Bottom-Up Parsing Construction of a parse tree for an input string beginning at

# 8_syntax_analysis_p2.pdf - Bottom-Up Parsing 1 Dr Alekha...

• 92

This preview shows page 1 - 14 out of 92 pages.

1 Dr. Alekha Kumar Mishra Bottom-Up Parsing
2 Dr. Alekha Kumar Mishra Bottom-Up Parsing Construction of a parse tree for an input string beginning at the leaves (the bottom) and working up towards the root (the top). e.g. Cosider the grammar used in top-down parser for input string id * id.
3 Dr. Alekha Kumar Mishra Reduction Bottom-up parsing can be visualize as the process of " reducing " a string w to the start symbol of the grammar. In each step, a specific substring matching the body of a production is replaced by the nonterminal at the left side of that production. The key decisions are: When to reduce, and What production to apply
4 Dr. Alekha Kumar Mishra Example E E * T | T (1) T T * F | F (2) F (E) | id (3) id * id, F * id by Prod#3  T * id by Prod#2  T * id ????  T * F by Prod#3  T by Prod#2  E by Prod#1
5 Dr. Alekha Kumar Mishra Example E E * T | T (1) T T * F | F (2) F (E) | id (3) Look at the derivation in reverse order, What do you see?? id * id, F * id by Prod#3  T * id by Prod#2  T * id ????  T * F by Prod#3  T by Prod#2  E by Prod#1
6 Dr. Alekha Kumar Mishra Handle pruning While constructing a right-most derivation in reverse for an input, a substring that matches the body of a production and whose reduction represents a step along this derivation process is called a " handle ". A handle of a right-sentential form is a production A  and a position of where the string may be found, such that replacing at that position by A produces the previous right-sentential form in a rightmost derivation of . Thsi process is called handle pruning
7 Dr. Alekha Kumar Mishra Example: Handle pruning RIGHT SENTENTIAL FORM HANDLE REDUCING PRODUCTION id 1 * id 2 id 1 F id F * id 2 F T F T * id 2 id 2 F id T * F T * F T  T * F T T E
8 Dr. Alekha Kumar Mishra Shift-Reduce Parsing Shift-reduce parsing is a form of bottom-up parsing in which a stack holds grammar symbols and an input buffer holds the rest of the string to be parsed. The handle always appears at the top of the stack We use \$ to mark the bottom of the stack and also the right end of the input. Primary operations are shift and reduce
9 Dr. Alekha Kumar Mishra Shift-Reduce Parsing(2) Initial state: Final state (Acceptance state)
10 Dr. Alekha Kumar Mishra Actions of Shift-Reduce Parser Shift :- Shift the next input symbol onto the top of the stack. Reduce :- The right end of the string to be reduced must be at the top of the stack. Locate the left end of the string within the stack and decide with what nonterminal to replace the string. Accept :- Announce successful completion of parsing. Error :- Discover a syntax error and call an error recovery routine.
11 Dr. Alekha Kumar Mishra Example : id 1 * id 2
12 Dr. Alekha Kumar Mishra The use of a stack in shift-reduce parsing is justified by an important fact: the handle will always eventually appear on top of the stack, never inside.
13 Dr. Alekha Kumar Mishra

#### You've reached the end of your free preview.

Want to read all 92 pages?

• Winter '17
• sathya raja shekaran

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern