80 likes | 147 Views
We need 2 look ahead to choose one of the two rules for A. Since there are ba , the parser applies A bS. (q 0 , ababbb, Z 0 ) (q 1 , ababbb, S Z 0 ) (q 1 , ababbb, aA Z 0 ) (q 1 , ba bbb, A Z 0 ) (q 1 , babbb, bS Z 0 ) (q 1 , abbb, S Z 0 )
E N D
We need 2 look ahead to choose one of the two rules for A. Since there are ba, the parser applies A bS (q0, ababbb, Z0) (q1, ababbb, SZ0) (q1, ababbb, aAZ0) (q1, babbb, AZ0) (q1, babbb, bSZ0) (q1, abbb, SZ0) (q1, abbb, aAZ0) (q1, bbb, AZ0) (q1, bbb, bbbZ0) . . . (q1, , Z0) // The LL(2) parsing is successful. 2 look ahead ba bb XX S aA Stack top Here again we need 2 look ahead, and since it is bb, the parser applies rule A bbb A bS bbb 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). • S aA A bS | bbb • S AB | aaab A aAb | 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. Along this sequence of configurations, whenever your parser needs to look ahead, write aclear 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. Answer: (a) S aA A bS | bbb (i) We choose w = ababbb, for which the parsing profile is (ii) Parse table:
(1) (2) (3) (4) (5) (6) Answer: (b) S AB | aaab A aAb | aaaaaaab B bBa | bbbb The parser needs 8 look ahead to choose proper rule between rules (3) and (4). Since there is b at the end, the parser applies rule (4). (q1,aaaaaaabbbbbbbbbaa, AbbBZ0) (q1, aaaaaaabbbbbbbbbaa, aaaaaaabbbBZ0) . . . . . (q1,bbbbbbaa, BZ0) 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). (q1, bbbbbbaa, bBaZ0) (q1,bbbbbaa, BaZ0) (q1, bbbbbaa, bBaaZ0) (q1,bbbbaa, BaaZ0) (q1, bbbbaa, bbbbaaZ0) . . . . (q1,, Z0) (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). Since there is no b at the end, the parser applies rule (1). 1 (q0, aaaaaaaaabbbbbbbbbaa, Z0) (q1,aaaaaaaaabbbbbbbbbaa, SZ0) (q1,aaaaaaaaabbbbbbbbbaa, ABZ0) The parser needs 8 look ahead to choose proper rule between rules (3) and (4). Since there is no b at the end, the parser applies rule (3). (q1, aaaaaaaaabbbbbbbbbaa, aAbBZ0) (q1,aaaaaaaaabbbbbbbbbaa, AbBZ0) (q1, aaaaaaaabbbbbbbbbaa, aAbbBZ0)
8 look ahead aaabxxxx aaaaxxxx aaaaaaaa aaaaaaab bbbbbxxx bbbbaxxx Stack top S aaab AB aAb aaaaaaab A B bBa bbbb Answer (b)-(ii): Parse table for grammar S AB | aaab A aAb | 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) Show theparse table of an LL(k) parser with minimum k for grammar G’. G: S aAb | aBb A aAb | c B aBb | d Answer: (a) The language of grammar G is {aixbi | x {c, d} }. Consider an LL(k) parser in the starting configuration with an input (q1, aa . . . . .acbb. . . .b, SZ0). The parser should know that S aAb is the next rule applied for S. 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.
(q0, aaacbbb, Z0) (q1, aaacbbb, SZ0) (q1, aaacbbb, aSbZ0) (q1, aacbbb, SbZ0) (q1, aacbbb, aSbbZ0) Since 2 look ahead is aa, apply S aSb. 1 look ahead is c. Apply rule A c 2 look ahead is ac. Apply S aAb (q1, acbbb, SbbZ0) (q1, acbbb, aAbbbZ0) (q1, cbbb, AbbbZ0) (q1, cbbb, cbbbZ0) . . . . (q1, , Z0) 2 look ahead aa ac or ad cx dx Stack top S aSb aAb x: don’t care c d A (b) Construct an LL(k) grammar G’ that generates the same language as the grammar G. Answer:G’: S aSb | aAb A c | d (c) Show theparse table of an LL(k) parser with minimum k for grammar G’. Answer: 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 : S aAb followed by A c. All the other part of the string is generated by S aSb. The parser will use this property of the language. Based on the above analysis, we can construct the following parse table for an LL(2) parser.
(1) (2) (3) (4) (5) (6) Answer: (a) S ABC | BC | C A aaa B aa C a Here it needs 2 look ahead. Since 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. Here it needs 1 look ahead to see if there is no more a’s such thatrule (6) can be applicable. If 1 look ahead were blank, the parser should have applied rule (6). (q0, aaaaaa, Z0) (q1, aaaaaa, Z0) (q1, aaaaa, aZ0) (q1, aaaa, aaZ0) (q1, aaa, aaaZ0) (q1, aaa, AZ0) (q1, aa, aAZ0) (q1, a, aaAZ0) (q1, a, BAZ0) (q1, , aBAZ0) (q1, , CBAZ0) (q1, , SZ0) 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) S ABC | BC | C A aaa B aa C a (b) S aSA | a A aaaaab We choose w = aaaaaa, which involves the most production rules, and examine how an LR(k) parser can parse this string. String w is derived according to the rightmost derivation as follows; S ABC ABa Aaaa aaaaaa. The parser will apply rules (4)(5)(6)(1) in this order which is the reverse order of the rightmost derivation.
(ii) Parse table. 2 look ahead aa aB BB xx aaa A x : don’t care B : blank *Stack top portion B aa Shift-in a Shift-in C S ABC S BC C S * Stack top portion is depth 3. The bottom of stack symbol Z0 is not include.
(1) (2) (3) Answer:(b) S aSA | a A aaaaab (i): We choose w = aaaaaaaabaaaaab, which is derived as follows by the rightmost derivation; (1) (3) (1) (3) (2) S aSA aSaaaaab aaSAaaaaab aaSaaaaabaaaaab aaaaaaaabaaaaab The parser looks aaaaab ahead, which implies that the a at the stack top should be reduced by applying rule (2). (q0, aaaaaaaabaaaaab, Z0) . . . . (q1, aaaaabaaaaab, aaaZ0) (q1, aaaaabaaaaab, SaaZ0) . . . . (q1, aaaaab, baaaaaSaaZ0) (q1, aaaaab, ASaaZ0) (q1, aaaaab, SaZ0) . . . (q1, , baaaaaSaZ0) (q1, , ASaZ0) (q1, , SZ0) 6 look ahead aaaaaa aaaaab xxxxxx BBBBBB Stack top portion a Shift-in S S aaaaab A aSA S 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. So, 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. (ii) The reduction table:
0 look ahead Stack top portion x Parse table: this A that A x: don’t care sameS S sameA S 4. (a) Why the grammar G below is notLR(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) Show theparse table of an LR(k) parser with minimum k for grammar G’. * Notice that in the grammar capital letters (S, A, B) are nonterminals and the lower case letters (t, h, i, s, a, m , e) are terminals. G: S Athis | Bthat A Asame | same B Bsame |same Answer: (a) For the convenience of argument lets transform the grammar as shown below with the following substitution. this = b that = c same = a S Ab | Bc A Aa | 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’: S sameS | sameA A 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. (q0, samesamethis, Z0) . . . . (q1, , sihtemasemasZ0) (q1, , AemasemasZ0) (q1, , SemasZ0) (q1, , SemasZ0) (q1, , SZ0)