90 likes | 322 Views
Compilers. ABHISHEK REDDY PAM (11cs30002) date : 07/10/2013. 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.
E N D
Compilers ABHISHEK REDDY PAM (11cs30002) date : 07/10/2013
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.
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.
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,…
Shift/Reduce Parser All strings in the stack are viable prefixes
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 Reducesteps preserves the property that the stack IS a viable prefix. Example : $aAbcde$ Shift or Reduce? • If we shift we obtain aAbcin the stack (aA is the valid item for the viable prefix aAbc). Recall that Abcis a handle. • Instead if we reduce we obtain aAAin the stack. (this is NOT a viable prefix!!!)
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 • a is the first symbol of y (or) • y = ε and a = $
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.