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...

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

1 Dr. Alekha Kumar Mishra Bottom-Up Parsing
Image of page 1
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.
Image of page 2
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
Image of page 3
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
Image of page 4
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
Image of page 5
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
Image of page 6
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
Image of page 7
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
Image of page 8
9 Dr. Alekha Kumar Mishra Shift-Reduce Parsing(2) Initial state: Final state (Acceptance state)
Image of page 9
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.
Image of page 10
11 Dr. Alekha Kumar Mishra Example : id 1 * id 2
Image of page 11
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.
Image of page 12
13 Dr. Alekha Kumar Mishra
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 92 pages?

  • Winter '17
  • sathya raja shekaran

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors