CS331 SOLUTION Problem # Points 1 10 2 15 3 25 4 20 5 15 6 15 Total 100 1. ssume you are dealing with a ompiler for a Java-like language. For eah of the following errors, irle whih phase would normally issue the message: (L) lexial analysis, (P) parsing, (S) semanti analysis, (R) runtime, or (N) none. Misspelled built-in proedure name (e.g. Wirte for Write ) rray index out of bounds One-dimensional array aessed with two subsripts Identifier name that begins with an undersore Wrong number of arguments in funtion all S R S L S 2. For eah grammar below, answer yes (Y) or no (N) to eah of the three questions in the appropriate spae. You don t need to go through the entire parser onstrution to answer these. Is this grammar: LL(1)? LR(1)? Unambiguous? S asa asb N Y Y S Sa bs N N N S asa bs Y N Y S asa ɛ N N Y S Sa b N Y Y
3. Let G be the following CFG grammar: 1. S b 2. bd 3. d 4. ɛ a. Fill in the missing states (with items) in the partial LR(1) DF given below. 12 : [ d, b] 13 : [ bd, b] d d 2 : [S S, $] 9 : [ d, b] 4 : [ b d, b] S 0 : [S S, $] [S b, $] [ bd, b] [ d, b] [, b] 5 : [ bd, b] [ d, b] [ bd, d] [ d, d] [, d] b 6 : [ b d, b] [ bd, d] [ d, d] [, d] 1 : [S b, $] 7 : [ bd, d] [ d, d] [ bd, d] [ d, d] [, d] 10 : [ d, d] d b b 11 : [ d, d] 3 : [S b, $] 8 : [ b d, d] [ bd, d] [ d, d] [, d] 14 : [ b d, d] d 15 : [ bd, d] Page 2
b. Fill in the tion and Goto tables for state 0. (Note: eof is the end of input symbol.) CTION GOTO b d eof S 0 R 4 Shift 5 error error 2 1. How would an SLR(1) parser table differ from the table for this LR(1) parser? In general, an SLR parser would use follow sets when filling the ells of the tion table to determine the lookahead, rather than using the expliit lookahead attahed to the LR(1) items. Speifially, an SLR parser would use follow() determine the lookaheads for the produtions in state 5. Sine b is in follow() (f. prodution S b), the ation for State 5 on input b would inlude Shift b (due to the item [ bd, b]) but also Redue ɛ. Therefore, the SLR parser for this grammar would ontain a shift/redue onflit. The SLR parser would also have fewer states, sine without the differenes due to lookahead symbols, states 4 and 14, 5 and 7, 13 and 15, 9 and 10, 11 and 12, and 6 and 8 would be the same. d. How would an LLR(1) parser table differ from the table for this LR(1) parser? n LLR parser would ombine states 4 and 14, 5 and 7, 13 and 15, 9 and 10, 11 and 12, and 6 and 8 beause they have the same ores i.e., they differ in lookahead only. e. Give one prodution whih, if added to the grammar above, would introdue a shift/redue onflit. Explain your answer by indiating whih state would hange and how the onflit would be realized in the DF. There are many produtions that, if added to the grammar, would produe a shift/redue onflit. One is b, whih would add [ b, b] to the set of items for state 0, and a new state for GOTO(S 0, b) would be introdued. Thus, in state 0 there would be the option to shift b and go to the newly introdued state, or redue by ɛ on b. I.e., the entry in the parse table for state 0, input b would ontain both Shift and Redue by ɛ, reating the shift/redue onflit. Page 3
4. Consider the following grammar: E B &B ɛ B true B false where {E,, B} is the set of nonterminal symbols, E is the start symbol, {&, true, false} is the set of terminal symbols, and ɛ denotes the empty string. a. Compute the following sets: first (B) first () follow () follow (B) true, false &, ɛ eof &, eof b. Construt the LL(1) parse table for this grammar. & true false eof E E B E B &B ɛ B B true B false Page 4
. Trae the stak and input ontents for a parse of the string true & false & true (you may not need all the rows). Stak E eof B eof true eof eof &B eof B eof false eof eof &B eof B eof true eof eof eof Remaining input true & false & true eof true & false & true eof true & false & true eof & false & true eof & false & true eof false & true eof false & true eof & true eof & true eof true eof true eof eof eof aept Page 5
5. Using the grammar and parse table below, show the steps of an LR(1) parser on input string a b b by filling in the table on the next page (you may not need all the rows). eof is the end of input signal, and a indiates ept. Produtions in the table are referened by number. 0: S 1: a 2: 3: b CTION GOTO State a b eof S 1 s2 s3 5 4 2 s2 s3 6 3 r3 r3 r3 r3 4 s2 s3 r0 7 5 a 6 s3 s8 7 7 r2 r2 r2 r2 8 r1 r1 r1 r1 Stak Input tion [1 $] abb$ Shift 2 [1 $][2 a] bb$ Shift 3 [1 $][2 a][3 b] b$ Redue 3: b [1 $][2 a][6 ] $ Shift 3 [1 $][2 a][6 ][3 b] $ Redue 3: b [1 $][2 a][6 ][7 ] $ Redue 2: [1 $][2 a][6 ] $ Shift 8 [1 $][2 a][6 ][8 ] $ Redue 1: a [1 $][4 ] $ Redue 0: S [1 $][5 S] $ CCEPT Page 6
6. Given the grammar: S E E E + E (E) id num E = E where S and E are non-terminals and all the other grammar symbols are terminals. ssume that the above grammar represents expressions suh as a = (b = ) a = (b = + 1) a. Extend the above grammar with attributes suh that S has a boolean synthesized attribute whih is true iff the left hand side of any assignments derived from S are simple ids. So for all of the examples above, the attribute should be true, but for (a + b) = or (a = b) = the attribute should be false. Note that a statement like (a) = 1 should also yield true. The approah is straightforward. We add boolean synthesized attribute Simple to both S and E, whih keeps trak of whether or not the subtree below a given E node has a single id (or a parenthesized id) as a left hand side of an equal sign. We also add a boolean synthesized attribute V alid to E, whih indiates whether the subtree below a given E node an serve as a valid left-hand side. This results in the following grammar: Prodution S E E E + E E (E 1 ) E id E num E E 1 = E 2 Semanti Rule S.V alid = E.V alid; E.Simple = false; E.V alid = E 1.V alid && E 2.V alid; E.Simple = E 1.Simple; E.V alid = E 1.V alid; E.V alid = E.Simple = true; E.V alid = true; E.Simple = false; E.Simple = false; E.V alid = E 1.V alid && E 2.V alid && E 1.Simple; Page 7
b. List the attributes in your extended grammar and indiate whether eah is synthesized or inherited. Explain your answer. Simple is a synthesized attribute that is used to determine if the left-hand side value is a simple id. It is initialized at nodes of the tree labeled with E that resolve to id (given the value true) and num (given the value false). From there, values of the attribute are passed to parent nodes based on omputations involving the values of the attribute for the hildren. V alid is a synthesized attribute that keeps trak of whether or not the left-hand side of the urrent expression (derived from the E in the given prodution) is valid, i.e., is a simple id.. Give an attributed parse tree for the soure string a = (b = ) and deorate the tree with the appropriate attributes and values. S E Simple=false E Simple=true = E Simple=false id a ( E Simple=false ) E Simple=true = E Simple=true id id b Page 8