编译原理 | 第三章课后习题答案
·
本文为编译原理(第三版)【清华大学出版社】部分课后答案
本答案均截取自网络,如有错误,望指正
📌第1题
【题目】
【答案】
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error \n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符T:T→ST’
14.void ParseT()
15.{
16. if (lookahead == 'a' || lookahead == '⋀' || lookahead == '(')
17. ParseS();
18. ParseT′();
19. else {
20. printf("syntax error \n");
21. exit(0);
22. }
23.}
24.// 解析非终结符T’:T’→,ST’|
25.void ParseT′()
26.{
27. if (lookahead == ',')
28. {
29. MatchToken(',');
30. ParseS();
31. ParseT′();
32. }
33. else if (lookahead == ')')
34. {
35. }
36. else {
37. printf("syntax error \n");
38. exit(0);
39. }
40.}
41.// 解析非终结符S→a||(T)
42.void ParseS()
43.{
44. switch (lookahead)
45. {
46. case 'a':
47. MatchToken('a');;
48. break;
49. case '⋀':
50. MatchToken('⋀');;
51. break;
52. case '(':
53. MatchToken('(');
54. ParseT();
55. MatchToken(')');
56. break;
57.default:
58. printf("syntax error \n");
59. exit(0);
}
📌第2题
【题目】
【答案】
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error \n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符E
14.void ParseE()
15.{
16. if (lookahead == '(' || lookahead == 'a' || lookahead == 'b' || lookahead == '⋀')
17. {
18. ParseT();
19. ParseE′();
20. }
21. else
22. {
23. printf("syntax error \n");
24. exit(0);
25. }
26.}
27.// 解析非终结符E′
28.void ParseE′()
29.{
30. if (lookahead == '+')
31. {
32. MatchToken(')');
33. ParseE();
34. }
35. else if (lookahead == ')' || lookahead == '#')
36. {
37. }
38. else
39. {
40. printf("syntax error \n");
41. exit(0);
42. }
43.}
44.// 解析非终结符T
45.void ParseT()
46.{
47. switch (lookahead)
48. {
49. case '(', 'a', 'b', ' ⋀':
50. ParseF();
51. ParseT′();
52. break;
53. default:
54. printf("syntax error \n");
55. exit(0);
56. }
57.}
58.// 解析非终结符T′
59.void ParseT′()
60.{
61. switch (lookahead)
62. {
63. case '(', 'a', 'b', ' ⋀':
64. ParseT();
65. break;
66. case '+', ' )', '#':
67. break;
68. default:
69. printf("syntax error \n");
70. exit(0);
71. }
72.}
73.// 解析非终结符F
74.void ParseF()
75.{
76. switch (lookahead)
77. {
78. case '(', 'a', 'b', ' ⋀':
79. ParseP();
80. break;
81. default:
82. printf("syntax error \n");
83. exit(0);
84. }
85.}
86.// 解析非终结符F′
87.void ParseF′()
88.{
89. switch (lookahead)
90. {
91. case '*':
92. MatchToken('*');
93. ParseF’();
94. break;
95. case '+', '(', ')', 'a', 'b', ' ⋀', '#':
96. break;
97. default:
98. printf("syntax error \n");
99. exit(0);
100. }
101.}
102.// 解析非终结符P
103.void ParseP()
104.{
105. switch (lookahead)
106. {
107. case '(':
108. MatchToken('(');
109. ParseE();
110. MatchToken(')');
111. break;
112. case 'a':
113. MatchToken('a');
114. break;
115. case 'b':
116. MatchToken('b');
117. break;
118. case '⋀':
119. MatchToken('⋀');
120. break;
121. default:
122. printf("syntax error \n");
123. exit(0);
124. }
}
📌第3题
【题目】
【答案】
📌第4题
【题目】
【答案】
📌第5题
【题目】
【答案】
📌第6题
【题目】
【部分答案】
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error\n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符S
14.void ParseS()
15.{
16. switch (lookahead)
17. {
18. case 'a': ParseA(); break;
19. case 'b': ParseB(); break;
20. default: printf("syntax error\n"); exit(0);
21. }
22.}
23.// 解析非终结符A
24.void ParseA()
25.{
26. if (lookahead == 'a')
27. { MatchToken('a');
28. ParseA’();
29. }
30. else
31. { printf("syntax error\n");
32. exit(0);
33. }
34.}
35.// 解析非终结符B
36.void ParseB()
37.{
38. if (lookahead == 'b')
39. { MatchToken('b');
40. ParseB();
41. }
42. else
43. { printf("syntax error\n");
44. exit(0);
45. }
46.}
47.void ParseA’() // 解析非终结符A’
48.{
49. switch (lookahead)
50. {
51. case 'a': ParseA(); break;
52. case '#': break;
53. default:
54. printf("syntax error\n");
55. exit(0);
56. }
57.}
58.// 解析非终结符B′
59.void ParseB′()
60.{
61. switch (lookahead)
62. {
63. case 'b': ParseB(); break;
64. case '#': break;
65. default:
66. printf("syntax error\n");
67. exit(0);
68. }
}
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error\n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符S
14.void ParseS()
15.{
16. if (lookahead == 'd' || lookahead == 'b' || lookahead == 'a' || lookahead == '#')
17. {
18. ParseB();
19. ParseS′();
20. }
21. else
22. {
23. printf("syntax error\n");
24. exit(0);
25. }
26.}
27.
28.// 解析非终结符S′
29.void ParseS′()
30.{
31. switch (lookahead)
32. {
33. case 'a':
34. MatchToken('a');
35. break;
36. case '#':
37. break;
38. default:
39. printf("syntax error\n");
40. exit(0);
41. }
42.}
43.// 解析非终结符B
44.void ParseB()
45.{
46. switch (lookahead)
47. {
48. case 'a', 'd', 'b', '#':
49. ParseD();
50. ParseB′();
51. break;
52. default:
53. printf("syntax error\n");
54. exit(0);
55. }
56.}
57.// 解析非终结符B′
58.void ParseB′()
59.{
60. switch (lookahead)
61. {
62. case 'b':
63. MatchToken('b');
64. break;
65. case '#', 'a':
66. break;
67. default:
68. printf("syntax error\n");
69. exit(0);
70. }
71.}
72.// 解析非终结符D
73.void ParseD()
74.{
75. switch (lookahead)
76. {
77. case 'd':
78. MatchToken('d');
79. break;
80. case 'b', 'a', '#':
81. break;
82. default:
83. printf("syntax error\n");
84. exit(0);
85. }
}
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error\n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符S
14.void ParseS()
15.{
16. switch (lookahead)
17. {
18. case 'a':
19. //
20. MatchToken('a');
21. ParseA();
22. MatchToken('a');
23. ParseB();
24. break;
25. Case 'b': //
26. MatchToken('b');
27. ParseA();
28. MatchToken('b');
29. ParseB();
30. break;
31. default:
32. printf("syntax error\n");
33. exit(0);
34. }
35. }
36.// 解析非终结符A
37.
38.void ParseA()
39.{
40. switch (lookahead )
41. case 'a': ParseS();break;
42. case 'b': ParseS(); break;
43. case 'd': MatchToken('d'); MatchToken('b'); break;
44. default:
45. { printf("syntax error\n");
46. exit(0);
47. }
48.}
49.// 解析非终结符B
50.void ParseB()
51.{
52. switch (lookahead )
53. {
54. case 'a': MatchToken('a');break;
55. case 'b': MatchToken('b'); ParseB(); break;
56. default:
57. { printf("syntax error\n");
58. exit(0);
59. }
}
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error\n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符S
14.void ParseS()
15.{
16. switch (lookahead)
17. {
18. case 'i':
19. MatchToken('i');
20. break;
21. case '(':
22. MatchToken('(');
23. ParseE();
24. MatchToken(')');
25. break;
26. default:
27. printf("syntax error\n");
28. exit(0);
29. }
30.}
31.// 解析非终结符E
32.void ParseE()
33.{
34. switch (lookahead)
35. {
36. case 'i', '(':
37. ParseS();
38. ParseE’();
39. break;
40. default:
41. printf("syntax error\n");
42. exit(0);
43. }
44.}
45.// 解析非终结符E′
46.void ParseE′()
47.{
48. switch (lookahead)
49. {
50. case '+':
51. MatchToken('+');
52. ParseS();
53. ParseE′();
54. break;
55. case '-':
56. MatchToken('-');
57. ParseS();
58. ParseE′();
59. break;
60. case ')':
61. break;
62. default:
63. printf("syntax error\n");
64. exit(0);
65. }
}
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error\n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符S
14.void ParseS()
15.{
16. if (lookahead == 'b')
17. { MatchToken('b');
18. ParseB();
19. ParseS’(); }
20. else
21. { printf("syntax error\n");
22. exit(0);
23. }
24.}
25.// 解析非终结符S’
26.void ParseS’()
27.{
28. switch (lookahead)
29. {
30. case 'a': MatchToken('a'); ParseA();ParseS’();break;
31. case '#': break;
32. default:
33. printf("syntax error\n");
34. exit(0);
35. }
36.}
37. // 解析非终结符A
38.void ParseA()
39.{
40. switch (lookahead)
41. {
42. case 'a': MatchToken('a'); ParseB();break;
43. case 'c': MatchToken('c'); break;
44. default:
45. printf("syntax error\n");
46. exit(0);
47. }
48.}
49.void ParseB() // 解析非终结符B
50.{
51. if (lookahead=='d')
52. {
53. MatchToken('d');
54. ParseB();
55. }
56. else
57. { printf("syntax error\n");
58. exit(0);
59. }
60.}
61.// 解析非终结符B′
62.void ParseB′()
63.{
64. switch (lookahead)
65. {
66. case 'a', '#': break;
67. case 'b': MatchToken('b'); ParseB’(); break;
68. default:
69. printf("syntax error\n");
70. exit(0);
71. }
}
1.void MatchToken(char expected)
2.{
3. if (lookahead != expected)
4. {
5. printf("syntax error\n");
6. exit(0);
7. }
8. else
9. {
10. lookahead = getToken();
11. }
12.}
13.// 解析非终结符M
14.void ParseM()
15.{
16. switch (lookahead)
17. {
18. case 'b', '(':
19. ParseH();
20. ParseM′();
21. break;
22. default:
23. printf("syntax error\n");
24. exit(0);
25. }
26.}
27.// 解析非终结符M′
28.void ParseM′()
29.{
30. switch (lookahead)
31. {
32. case 'a':
33. MatchToken('a');
34. ParseH();
35. ParseM′();
36. break;
37. case ')','#':
38. break;
39. default:
40. printf("syntax error\n");
41. exit(0);
42. }
43.}
44.// 解析非终结符H
45.void ParseH()
46.{
47. switch (lookahead)
48. {
49. case 'b':
50. MatchToken('b');
51. ParseH’();
52. break;
53. case '(':
54. MatchToken('(');
55. ParseM();
56. MatchToken(')');
57. break;
58. default:
59. printf("syntax error\n");
60. exit(0);
61. }
62.}
63.// 解析非终结符H′
64.void ParseH′()
65.{
66. switch (lookahead)
67. {
68. case '(':
69. MatchToken('(');
70. ParseM();
71. MatchToken(')');
72. break;
73. case ')', 'a', '#':
74. break;
75. default:
76. printf("syntax error\n");
77. exit(0);
78. }
}
📌第7题
【题目】
【答案】
更多推荐
已为社区贡献6条内容
所有评论(0)