Bottom Up Parsing

A bottom-up parse corresponds to the construction of a parse tree for an input string beginning at the leaves (the bottom) and working up towards the root (the top).

Reductions

We can think of bottom-up parsing as the process of “reducing” a string w to the start symbol of the grammar. At each reduction step, a specific substring matching the body of a production is replaced by the nonterminal at the head of that production.

The key decisions during bottom-up parsing are about when to reduce and about what production to apply, as the parse proceeds.

Right Most Derivation উল্টা করলে bottom up parsing tree পাওয়া যাই।

Handle Pruning

a “handle” is a substring that matches the body of a production, and whose reduction represents one step along the reverse of a rightmost derivation. যে substring গ্র্যামারের সাথে ম্যাচ খাই তাকে handle বলে। আর handle গুলোকে প্রোডাকশন হেডে কনভার্ট করে আস্তে আস্তে start symbol এ নিয়ে যেতে হবে।

Screenshot_20241110-194735~2.png

Shift-Reduce Parsing ( SR 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.

We use $ to mark the bottom of the stack and also the right end of the input.

Initially the stack is empty and after doing everything the input will be empty and stack will have the starting symbol

এই ছবিটা লেকচার নোট থেকে কর। বেটার হবে।

Screenshot_20241110-195645~2.png

Actions of Shift-Reduce Parsing

there are actually four possible actions a shift-reduce parser can make: (1) shift, (2) reduce, (3) accept, and (4) error.

  1. Shift. Shift the next input symbol onto the top of the stack. ইনপুটের সবচেয়ে লেফট সাইডের symbol কে ভাঙা।
  2. 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. Stack এর সবচেয়ে right সাইডের symbol কে ভাঙা।
  3. Accept. Announce successful completion of parsing.
  4. Error. Discover a syntax error and call an error recovery routine.