Download presentation
Presentation is loading. Please wait.
Published bySucianty Atmadja Modified over 5 years ago
1
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 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. (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. We need 2 look ahead to choose one of the two rules for A. Since there are ba, the parser applies A bS Here again we need 2 look ahead, and since it is bb, the parser applies rule A bbb Answer: (a) S aA A bS | bbb (i) We choose w = ababbb, for which the parsing profile is (ii) Parse table: S A Stack top 2 look ahead ba bb XX aA bS bbb
2
Answer: (b) S AB | aaab A aAb | aaaaaaab B bBa | bbbb
(1) (2) (3) (4) (5) (6) (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) (q1, aaaaaaabbbbbbbbbaa, AbbBZ0) (q1, aaaaaaabbbbbbbbbaa, aaaaaaabbbBZ0) (q1, bbbbbbaa, BZ0) 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). 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)
3
Answer (b)-(ii): Parse table for grammar S AB | aaab A aAb | aaaaaaab B bBa | bbbb
Stack top 8 look ahead aaabxxxx aaaaxxxx aaaaaaaa aaaaaaab bbbbbxxx bbbbaxxx aaab AB aAb aaaaaaab 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 the parse 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.
4
(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 the parse 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. (q0, aaacbbb, Z0) (q1, aaacbbb, SZ0) (q1, aaacbbb, aSbZ0) (q1, aacbbb, SbZ0) (q1, aacbbb, aSbbZ0) (q1, acbbb, SbbZ0) (q1, acbbb, aAbbbZ0) (q1, cbbb, AbbbZ0) (q1, cbbb, cbbbZ0) (q1, , Z0) Since 2 look ahead is aa, apply S aSb. 2 look ahead is ac. Apply S aAb 1 look ahead is c. Apply rule A c Based on the above analysis, we can construct the following parse table for an LL(2) parser. S A aa ac or ad cx dx aSb aAb c d Stack top 2 look ahead x: don’t care
5
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 Answer: (a) S ABC | BC | C A aaa B aa C a (1) (2) (3) (4) (5) (6) 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. (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) 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. 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.
6
(ii) Parse table. aa a ABC *Stack top portion 2 look ahead x : don’t care B : blank aaa aa aB Shift-in BB xx C A B S BC * Stack top portion is depth 3. The bottom of stack symbol Z0 is not include.
7
Answer: (b) S aSA | a A aaaaab
(i): We choose w = aaaaaaaabaaaaab, which is derived as follows by the rightmost derivation; (1) (2) (3) S aSA aSaaaaab aaSAaaaaab aaSaaaaabaaaaab aaaaaaaabaaaaab (1) (3) (1) (3) (2) 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. (q0, aaaaaaaabaaaaab, Z0) (q1, aaaaabaaaaab, aaaZ0) (q1, aaaaabaaaaab, SaaZ0) (q1, aaaaab, baaaaaSaaZ0) (q1, aaaaab, ASaaZ0) (q1, aaaaab, SaZ0) . . . (q1, , baaaaaSaZ0) (q1, , ASaZ0) (q1, , SZ0) The parser looks aaaaab ahead, which implies that the a at the stack top should be reduced by applying rule (2). (ii) The reduction table: a aaaaab aSA 6 look ahead Stack top portion aaaaaa Shift-in S xxxxxx A BBBBBB
8
4. (a) Why the grammar G below is not LR(k) grammar (i. e
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) Show the parse 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) this that sameS 0 look ahead A S Stack top portion x x: don’t care Parse table: sameA
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.