Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compilers ABHISHEK REDDY PAM (11CS30002) DATE : 07/10/2013.

Similar presentations


Presentation on theme: "Compilers ABHISHEK REDDY PAM (11CS30002) DATE : 07/10/2013."— Presentation transcript:

1 Compilers ABHISHEK REDDY PAM (11CS30002) DATE : 07/10/2013

2 Viable Prefix  The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes.  Right-Sentential Form : A sentential form that can be derived by a rightmost derivation  The prefix of the sentential form may not be the content of the stack.

3 Example Consider: S  aABe A  Abc | b B  d Rightmost Derivation of the string abbcde: S  aABe  aAde  aAbcde  abbcde The (unique) handle is underlined for each step.

4 Continuation… A viable prefix is : (1) a string that equals a prefix of a right- sentential form up to (and including) its unique handle. (2) any prefix of a string that satisfies (1) For given example : Viable prefixes: a, aA, aAd, aAbc, ab, aAb,… Not viable prefixes: aAde, Abc, aAA,…

5 Shift/Reduce Parser StackInputRemark $abbcde$SHIFT $abbcde$SHIFT $abbcde$REDUCE $aAbcde$SHIFT $aAbcde$SHIFT $aAbcde$REDUCE $aAde$SHIFT $aAde$e$REDUCE $aABe$e$SHIFT $aABe$REDUCE $S$ACCEPT All strings in the stack are viable prefixes

6 When to shift/reduce ? Sometimes on top of the stack something appears to be a handle (i.e., matches the RHS of a production). But: maybe we have not shifted enough elements to identify the handle. Observe the correct sequence of Shift and Reduce steps preserves the property that the stack IS a viable prefix. Example : $aAb cde$Shift or Reduce? If we shift we obtain aAbc in the stack (aA is the valid item for the viable prefix aAbc). Recall that Abc is a handle. Instead if we reduce we obtain aAA in the stack. (this is NOT a viable prefix!!!)

7 Valid Items DEFINITION: A  α.β, {a} is a valid LR(1)-item for a viable prefix γα if there exists a right-most derivation. S  γAy  γαβy Such that i.a is the first symbol of y (or) ii.y = ε and a = $

8 THE END A DFA accepting viable prefixes. Its states are sets of LR(1)-items (that are valid for the corresponding viable prefixes) Canonical LR Parser(LR(1) Parser), States and encoding of valid items and viable prefixes are discussed in the next lecture.


Download ppt "Compilers ABHISHEK REDDY PAM (11CS30002) DATE : 07/10/2013."

Similar presentations


Ads by Google