Skip to content

Commit da5d500

Browse files
authored
Merge pull request #4 from ShiSeAB/add-exercise-solution
2 parents 167ed92 + 6d89de2 commit da5d500

9 files changed

Lines changed: 1077 additions & 468 deletions

File tree

exercises/ar.html

Lines changed: 569 additions & 210 deletions
Large diffs are not rendered by default.

exercises/cano.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Compiler Design – Self-Check Quiz</h1></header>
58+
<header><h1>Canonicalization – Self-Check Quiz</h1></header>
5959
<main>
6060
<section id="tf"><h2>True / False</h2></section>
6161
<section id="mcq"><h2>Multiple Choice</h2></section>
6262
<div id="result"></div>
6363
</main>
6464
<footer>
65-
<p>Based on Compiler Design chapters. © 2026</p>
65+
<p>Based on "Canonicalization" chapters. © 2026</p>
6666
<p style="margin-top: 1rem; font-size: 0.85rem;">
6767
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6868
</p>

exercises/gc.html

Lines changed: 450 additions & 211 deletions
Large diffs are not rendered by default.

exercises/inst.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Compiler Design – Self-Check Quiz</h1></header>
58+
<header><h1>Instruction Selection – Self-Check Quiz</h1></header>
5959
<main>
6060
<section id="tf"><h2>True / False</h2></section>
6161
<section id="mcq"><h2>Multiple Choice</h2></section>
6262
<div id="result"></div>
6363
</main>
6464
<footer>
65-
<p>Based on Compiler Design chapters. © 2026</p>
65+
<p>Based on "Instruction Selection" chapters. © 2026</p>
6666
<p style="margin-top: 1rem; font-size: 0.85rem;">
6767
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6868
</p>

exercises/ir.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Compiler Design – Self-Check Quiz</h1></header>
58+
<header><h1>IR – Self-Check Quiz</h1></header>
5959
<main>
6060
<section id="tf"><h2>True / False</h2></section>
6161
<section id="mcq"><h2>Multiple Choice</h2></section>
6262
<div id="result"></div>
6363
</main>
6464
<footer>
65-
<p>Based on Compiler Design chapters. © 2026</p>
65+
<p>Based on "IR" chapters. © 2026</p>
6666
<p style="margin-top: 1rem; font-size: 0.85rem;">
6767
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6868
</p>

exercises/lexing.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Compiler Design – Self-Check Quiz</h1></header>
58+
<header><h1>Lexical Analysis – Self-Check Quiz</h1></header>
5959
<main>
6060
<section id="tf"><h2>True / False</h2></section>
6161
<section id="mcq"><h2>Multiple Choice</h2></section>
6262
<div id="result"></div>
6363
</main>
6464
<footer>
65-
<p>Based on Compiler Design chapters. © 2026</p>
65+
<p>Based on "Lexical Analysis" chapters. © 2026</p>
6666
<p style="margin-top: 1rem; font-size: 0.85rem;">
6767
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6868
</p>

exercises/liveness.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Compiler Design – Self-Check Quiz</h1></header>
58+
<header><h1>Liveness Analysis – Self-Check Quiz</h1></header>
5959
<main>
6060
<section id="tf"><h2>True / False</h2></section>
6161
<section id="mcq"><h2>Multiple Choice</h2></section>
6262
<div id="result"></div>
6363
</main>
6464
<footer>
65-
<p>Based on Compiler Design chapters. © 2026</p>
65+
<p>Based on "Liveness Analysis" chapters. © 2026</p>
6666
<p style="margin-top: 1rem; font-size: 0.85rem;">
6767
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6868
</p>

exercises/parsing.html

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Compiler Design – Self-Check Quiz</h1></header>
58+
<header><h1>Parsing – Self-Check Quiz</h1></header>
5959
<main>
6060
<section id="tf"><h2>True / False</h2></section>
6161
<section id="mcq"><h2>Multiple Choice</h2></section>
6262
<div id="result"></div>
6363
</main>
6464
<footer>
65-
<p>Based on Compiler Design chapters. © 2026</p>
65+
<p>Based on "Parsing" chapters. © 2026</p>
6666
<p style="margin-top: 1rem; font-size: 0.85rem;">
6767
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6868
</p>
@@ -606,27 +606,25 @@
606606
{
607607
question: "Which of the following is true regarding 'Nullable' symbols in FIRST and FOLLOW set algorithms?",
608608
options: [
609-
{
610-
text: "A. A nonterminal X is nullable if there is a production X→Y1Y2…Ykwhere all Yi are nullable.",
611-
explanation: "正确答案\nIf every symbol on the right-hand side can eventually derive an empty string, then the parent nonterminal can too."
612-
}
609+
{
610+
text: "A. A nonterminal X is nullable if there is a production X→Y1Y2…Ykwhere all Yi are nullable.",
611+
explanation: "正确答案\nIf every symbol on the right-hand side can eventually derive an empty string, then the parent nonterminal can too."
612+
},
613+
{
614+
text: "B.A terminal symbol can be nullable if it represents an empty string literal.",
615+
explanation: "Terminals are fixed tokens and cannot derive the empty string; only nonterminals can be nullable."
616+
},
617+
{
618+
text: "C. Nullable symbols must be removed to perform LR parsing.",
619+
explanation: "LR parsing can handle nullable symbols without issue."
620+
},
621+
{
622+
text: "D. If X is nullable, then FIRST(X) must contain all terminal symbols in the grammar.",
623+
explanation: "Nullable only means the empty string is a possibility; it does not dictate the presence of all terminals."
624+
}
613625
],
614626
answer: 0
615627
},
616-
{
617-
question: "B.A terminal symbol can be nullable if it represents an empty string literal. Terminals represent fixed tokens from the alphabet and are never nullable.",
618-
options: [
619-
{
620-
text: "C. Nullable symbols must be removed to perform LR parsing.",
621-
explanation: "LR parsing can handle nullable symbols without issue."
622-
},
623-
{
624-
text: "D. If X is nullable, then FIRST(X) must contain all terminal symbols in the grammar.",
625-
explanation: "Nullable only means the empty string is a possibility; it does not dictate the presence of all terminals."
626-
}
627-
],
628-
answer: -1
629-
},
630628
{
631629
question: "Which set is defined as the terminals that can immediately follow a nonterminal X in any derivation?",
632630
options: [

exercises/reg.html

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5-
<title>Semantic Analysis Quiz</title>
5+
<title>Compiler Design Quiz</title>
66
<meta name="viewport" content="width=device-width,initial-scale=1">
77
<style>
88
:root{
@@ -12,7 +12,7 @@
1212
body{font-family:system-ui,Segoe UI,Roboto,sans-serif;margin:0;background:var(--bg);color:var(--fg);}
1313
header{padding:1rem;background:var(--accent);color:#fff;text-align:center;}
1414
main{padding:1rem;max-width:820px;margin:auto;}
15-
h2{margin-top:2rem;color:var(--accent);}
15+
h2{margin-top:2rem;color:var(--accent); border-bottom: 2px solid #eee; padding-bottom: 0.5rem;}
1616
.q{padding:1rem;border:1px solid var(--border);border-radius:6px;margin-bottom:.8rem;background:#fff; transition: background 0.3s;}
1717
.q.correct{border-color:var(--ok);background:#f1f8f4;}
1818
.q.wrong {border-color:var(--bad);background:#fef5f5;}
@@ -25,7 +25,7 @@
2525
#result.show{display:block; background:#e3f2fd;border:1px solid var(--accent);}
2626
footer{padding:1rem;text-align:center;font-size:.85rem;color:#555;}
2727

28-
/* 新增:代码块与解析的样式 */
28+
/* 代码块与解析的样式 */
2929
code { background: #eee; padding: 2px 5px; border-radius: 4px; color: #d63384; font-family: monospace; }
3030
.explanation {
3131
display: none;
@@ -55,19 +55,21 @@
5555
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
5656
</head>
5757
<body>
58-
<header><h1>Semantic Analysis – Self-Check Quiz</h1></header>
58+
<header><h1>Register Allocation – Self-Check Quiz</h1></header>
5959
<main>
60+
<section id="tf"><h2>True / False</h2></section>
6061
<section id="mcq"><h2>Multiple Choice</h2></section>
6162
<div id="result"></div>
6263
</main>
6364
<footer>
64-
<p>Based on "Semantic Analysis" chapter. © 2026</p>
65+
<p>Based on "Register Allocation" chapters. © 2026</p>
6566
<p style="margin-top: 1rem; font-size: 0.85rem;">
6667
📖 源码与贡献题目请访问:<a href="https://github.com/rainoftime/tiger/tree/main/exercises" target="_blank" style="color: var(--accent); text-decoration: none;">GitHub Exercises</a>
6768
</p>
6869
</footer>
6970

7071
<script>
72+
const tfWrap = document.getElementById('tf');
7173
const mcqWrap = document.getElementById('mcq');
7274
const resultBox = document.getElementById('result');
7375

@@ -77,27 +79,27 @@
7779
return text.replace(/`([^`]+)`/g, '<code>$1</code>');
7880
}
7981

80-
// build multiple choice element
81-
function buildMCQ(q, i){
82-
const div=document.createElement('div');
83-
div.className='q';
84-
div.dataset.correct=q.answer; // store index
85-
let optionsHTML='';
82+
// build multiple choice / TF element
83+
function buildMCQ(q, globalIdx, labelPrefix = "Q"){
84+
const div = document.createElement('div');
85+
div.className = 'q';
86+
div.dataset.correct = q.answer; // store index
87+
let optionsHTML = '';
8688

8789
q.options.forEach((opt, idx) => {
8890
// 只有当有解析时才渲染解析 div
89-
const expHTML = opt.explanation ? `<div class="explanation">${formatText(opt.explanation)}</div>` : '';
91+
const expHTML = opt.explanation ? `<div class=explanation>${formatText(opt.explanation)}</div>` : '';
9092
optionsHTML += `
91-
<label class="option-label" id="lbl-${i}-${idx}">
92-
<input type="radio" name="mc${i}" value="${idx}">
93+
<label class="option-label" id="lbl-${globalIdx}-${idx}">
94+
<input type="radio" name="q${globalIdx}" value="${idx}">
9395
<span class="opt-text">${formatText(opt.text)}</span>
9496
</label>
9597
${expHTML}
9698
`;
9799
});
98100

99-
div.innerHTML=`<strong>Q${i+1}.</strong> ${formatText(q.question)}<br><div style="margin-top:0.8rem;">${optionsHTML}</div>
100-
<button class="check-btn" id="btn-${i}">Check Answer / 查看解析</button>`;
101+
div.innerHTML=`<strong>${labelPrefix}.</strong> ${formatText(q.question)}<br><div style="margin-top:0.8rem;">${optionsHTML}</div>
102+
<button class="check-btn" id="btn-${globalIdx}">Check Answer / 查看解析</button>`;
101103
return div;
102104
}
103105

@@ -106,7 +108,7 @@
106108
const correctIdx = parseInt(div.dataset.correct);
107109
const sel = div.querySelector('input[type=radio]:checked');
108110

109-
// 如果没有选择,设定 selectedIdx 为 -1,不再拦截弹窗
111+
// 如果没有选择,设定 selectedIdx 为 -1
110112
const selectedIdx = sel ? parseInt(sel.value) : -1;
111113

112114
// 判断对错(未作答视为错误)
@@ -147,6 +149,17 @@
147149
checkAllFinished();
148150
}
149151

152+
// 检查是否所有题目均已作答,完成后显示总分
153+
function checkAllFinished() {
154+
const total = quizData.trueFalse.length + quizData.multipleChoice.length;
155+
const answered = document.querySelectorAll('.q.correct, .q.wrong').length;
156+
if (answered === total) {
157+
const correct = document.querySelectorAll('.q.correct').length;
158+
resultBox.innerHTML = `All Answered! Final Score (您的总成绩): ${correct} / ${total} (${Math.round(correct/total*100)}%)`;
159+
resultBox.classList.add('show');
160+
}
161+
}
162+
150163
// 检查是否所有题目均已作答,完成后显示总分
151164
function checkAllFinished() {
152165
const total = quizData.multipleChoice.length;

0 commit comments

Comments
 (0)