Key to Homework #8 1. For each of the following context-free grammars (a) and (b) below, construct an LL(k) parser with minimum k according to the following guide lines (i) and (ii). (a) a b bbb (b) B aaab ab aaaaaaab B bba bbbb (i) Choose a typical string w from the language of the grammar, and write the parsing profile (i.e., sequence of configurations, see the lecture note for an example) that your parser will take to parse w. long this sequence of configurations, whenever your parser needs to look ahead, write a clear remark disclosing the minimum required look ahead length (i.e., the number of cells) and the reason why it needs to look ahead. (ii) Construct the parse table of your parser. nswer: (a) a b bbb (i) We choose w = ababbb, for which the parsing profile is We need 2 look ahead to choose one of the two rules for. ince there are ba, the parser applies b (q 0, ababbb, Z 0 ) (q 1, ababbb, Z 0 ) (q 1, ababbb, az 0 ) (q 1, babbb, Z 0 ) (q 1, babbb, bz 0 ) (q 1, abbb, Z 0 ) (q 1, abbb, az 0 ) (q 1, bbb, Z 0 ) (q 1, bbb, bbbz 0 )... (q 1, ε, Z 0 ) // The LL(2) parsing is successful. Here again we need 2 look ahead, and since it is bb, the parser applies rule bbb (ii) Parse table: 2 look ahead ba bb XX tack top b bbb a 1
(1) (2) (3) (4) (5) (6) nswer: (b) B aaab ab aaaaaaab B bba bbbb (i) We choose w = aaaaaaaaabbbbbbbbbaa, for which the parsing profile is The parser needs 4 look ahead to make sure that next a is generated by rule (1) or (2). ince there is no b at the end, the parser applies rule (1). 1 (q 0, aaaaaaaaabbbbbbbbbaa, Z 0 ) (q 1, aaaaaaaaabbbbbbbbbaa, Z 0 ) (q 1, aaaaaaaaabbbbbbbbbaa, BZ 0 ) The parser needs 8 look ahead to choose proper rule between rules (3) and (4). ince there is no b at the end, the parser applies rule (3). (q 1, aaaaaaaaabbbbbbbbbaa, abbz 0 ) (q 1, aaaaaaaaabbbbbbbbbaa, bbz 0 ) (q 1, aaaaaaaabbbbbbbbbaa, abbbz 0 ) The parser needs 8 look ahead to choose proper rule between rules (3) and (4). ince there is b at the end, the parser applies rule (4). (q 1, aaaaaaabbbbbbbbbaa, bbbz 0 ) (q 1, aaaaaaabbbbbbbbbaa, aaaaaaabbbbz 0 )..... (q 1, bbbbbbaa, BZ 0 ) It needs 5 look ahead to choose proper rule between rules (5) and (6). If 5 look ahead is bbbbb, apply rule (5), if it is bbbba, apply rule (6). (q 1, bbbbbbaa, bbaz 0 ) (q 1, bbbbbaa, BaZ 0 ) (q 1, bbbbbaa, bbaaz 0 ) (q 1, bbbbaa, BaaZ 0 ) (q 1, bbbbaa, bbbbaaz 0 ).... (q 1, ε, Z 0 ) 2
nswer (b)-(ii): Parse table for grammar B aaab ab aaaaaaab B bba bbbb tack top 8 look ahead aaabxxxx aaaaxxxx aaaaaaaa aaaaaaab bbbbbxxx bbbbaxxx aaab B ab aaaaaaab B bba bbbb 2. (a) Why the grammar G below is not LL(k) grammar (i.e., it is impossible to construct an LL(k) parser, for any constant k)? (b) Construct an LL(k) grammar G that generates the same language as the grammar G. (c) how the parse table of an LL(k) parser with minimum k for grammar G. G: ab abb ab c B abb d nswer: (a) The language of grammar G is {a i xb i x {c, d} }. Consider an LL(k) parser in the starting configuration with an input (q 1, aa.....acbb....b, Z 0 ). The parser should know that ab is the next rule applied for. However, the c at the center of the input which is the only information available for the decision can be located arbitrarily far away from the first a. It is impossible for the parser to see the c (or d) by looking some constant k cells ahead. Hence, G is not LL(k) grammar for any constant k. 3
(b) Construct an LL(k) grammar G that generates the same language as the grammar G. nswer: G : ab ab c d (c) how the parse table of an LL(k) parser with minimum k for grammar G. nswer: We examine how string aaacbbb can be parsed by an LL(k) parser. Clearly, the substring acb at the center of this string is generated by : ab followed by c. ll the other part of the string is generated by ab. The parser will use this property of the language. (q 0, aaacbbb, Z 0 ) (q 1, aaacbbb, Z 0 ) (q 1, aaacbbb, abz 0 ) (q 1, aacbbb, bz 0 ) (q 1, aacbbb, abbz 0 ) ince 2 look ahead is aa, apply ab. 2 look ahead is ac. pply ab 1 look ahead is c. pply rule c (q 1, acbbb, bbz 0 ) (q 1, acbbb, abbbz 0 ) (q 1, cbbb, bbbz 0 ) (q 1, cbbb, cbbbz 0 ).... (q 1, e, Z 0 ) Based on the above analysis, we can construct the following parse table for an LL(2) parser. 2 look ahead aa ac or ad cx dx tack top ab ab c d x: don t care 4
3. For each of the following context-free grammars (a) and (b) below, construct an LR(k) parser with minimum k according to the guide lines (i) and (ii) above in problem 1. (a) BC BC C aaa B aa C a (b) a a aaaaab (1) (2) (3) (4) (5) (6) nswer: (a) BC BC C aaa B aa C a We choose w = aaaaaa, which involves the most production rules, and examine how an LR(k) parser can parse this string. tring w is derived according to the rightmost derivation as follows; BC Ba aaa aaaaaa. The parser will apply rules (4)(5)(6)(1) in this order which is the reverse order of the rightmost derivation. Here it needs 1 look ahead to see if there is no more a s such that rule (6) can be applicable. If 1 look ahead were blank, the parser should have applied rule (6). Here it needs 2 look ahead. ince it sees aa, the parser shifts the next a in onto the stack instead of applying rule (5). If there were only one a, rule (5) should ve been applied. (q 0, aaaaaa, Z 0 ) (q 1, aaaaaa, Z 0 ) (q 1, aaaaa, az 0 ) (q 1, aaaa, aaz 0 ) (q 1, aaa, aaaz 0 ) (q 1, aaa, Z 0 ) (q 1, aa, az 0 ) (q 1, a, aaz 0 ) (q 1, a, BZ 0 ) (q 1, ε, abz 0 ) (q 1, ε, CBZ 0 ) (q 1, ε, Z 0 ) 5
(ii) Parse table. 2 look ahead aa ab BB xx *tack top portion aaa aa a hift-in hift-in B C x : don t care B : blank BC BC C * tack top portion is depth 3. The bottom of stack symbol Z 0 is not include. 6
(1) (2) (3) nswer: (b) a a aaaaab (i): We choose w = aaaaaaaabaaaaab, which is derived as follows by the rightmost derivation; (1) (3) (1) (3) (2) a aaaaaab aaaaaaab aaaaaaabaaaaab aaaaaaaabaaaaab Now we examine how this string can be parsed according to the LR(k) strategy applying the sequence of rules in the reverse order of the rightmost derivation. The first target to be brought up to the stack top and reduced is the a generated by rule (2). This a is next to the left of the first aaaaab appearing in the input. o, the parser shift in the input until it sees aaaaab. When the first a of the input is shifted in, if there remains no symbols in the input, rule (2) should be applied. The parser looks aaaaab ahead, which implies that the a at the stack top should be reduced by applying rule (2). (q 0, aaaaaaaabaaaaab, Z 0 ).... (q 1, aaaaabaaaaab, aaaz 0 ) (q 1, aaaaabaaaaab, aaz 0 ).... (q 1, aaaaab, baaaaaaaz 0 ) (q 1, aaaaab, aaz 0 ) (q 1, aaaaab, az 0 )... (q 1, ε, baaaaaaz 0 ) (q 1, ε, az 0 ) (q 1, ε, Z 0 ) (ii) The reduction table: tack top portion a aaaaab 6 look ahead aaaaaa aaaaab hift-in xxxxxx BBBBBB a 7
4. (a) Why the grammar G below is not LR(k) grammar (i.e., it is impossible to construct an LR(k) parser, for any constant k)? (b) Construct an LR(k) grammar G such that it generates the same language as the grammar G. (c) how the parse table of an LR(k) parser with minimum k for grammar G. * Notice that in the grammar capital letters (,, B) are nonterminals and the lower case letters (t, h, i, s, a, m, e) are terminals. G: this Bthat same same B Bsame same nswer: (a) For the convenience of argument lets transform the grammar as shown below with the following substitution. this = b that = c same = a b Bc a a B Ba a This grammar is the same grammar shown in slide number 172 of the lecture note. Refer to this slide for the argument that it is impossible to construct an LR(k) parser. (b) G : same same this that (c) We can easily construct an LR(0) parser, which reads the input until this or that appear on the stack top portion, and begins reductions as the following example shows. Notice that the parser needs no look ahead information. (q 0, samesamethis, Z 0 ).... (q 1, ε, sihtemasemasz 0 ) (q 1, ε, emasemasz 0 ) (q 1, ε, emasz 0 ) (q 1, ε, emasz 0 ) (q 1, ε, Z 0 ) Parse table: tack top portion this that same same 0 look ahead x x: don t care 8