The following grammar is proposed to remove the “dangling-else ambiguity” discussed in Section…

The following grammar is proposed to remove the “dangling-else ambiguity” discussed in Section 4.3.2:

Show that this grammar is still ambiguous.

Section 4.3.2, this grammar is ambiguous because it does not resolve the dangling-else ambiguity. To simplify the discussion, let us consider an abstraction of this grammar, where i stands for if expr then, e stands for else, and a stands for “all other productions.” We can then write the grammar, with augmenting production S” → S,

 As The sets of LR(0) items for grammar (4.67) are shown in Fig. 4.50. The ambiguity in (4.67) gives rise to a shift/reduce conflict in la. There, S → iS.eS calls for a shift of e and, since FOLLOW(S)= {e, $}, item S → iS. calls for reduction by S → iS on input e.

Translating back to the if-then-else terminology, given

 Figure 4.50: LR(0) states for augmented grammar (4.67)

if expr then stmt

on the stack and else as the first input symbol, should we shift else onto the stack (i.e., shift e) or reduce if expr then stmt (i.e, reduce by S → is)? The answer is that we should shift else, because it is “associated” with the previous then. In the terminology of grammar (4.67), the e on the input, standing for else, can only form part of the body beginning with the iS now on the top of the stack. If what follows e on the input cannot be parsed as an S, completing body iSeS, then it can be shown that there is no other parse possible. We conclude that the  Shift/reduce conflict in I4 should be resolved in favor of shift on input e. The SLR parsing table constructed from the sets of items of Fig. 4.48, using this resolution of the parsing-action conflict in I4 on input e, is shown in Fig. 4.51. Productions 1 through 3 are S → iSeS, S → is, and S→a, respectively.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer