Semantic analysis and code generation
مُقدَّم
مفردات
| English | المعنى |
|---|---|
| symbol table/ˈsɪmbl ˈteɪbl/ | A compiler data structure recording identifiers and information such as their types, scopes or locations. |
| intermediate representation | intermediate representation |
English
Decorate with meaning
- The symbol table maps identifiers to (type, scope); type rules walk the tree bottom-up: $\mathbf{id} + \mathbf{id}$ needs both sides numeric. The output is intermediate representation — three-address code, one operation per line.

{exercise:uni-cs1106-decorate}
تدريب
The symbol table binds —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Name resolution before type checking: scoping first.
English
The course program, exactly
count = a1 + b2 * 3witha1, b2: int: the table holds three int bindings; the walker checks $b_2 \times 3$ then $a_1 + (\ldots)$. IR:t1 = b2 * 3; t2 = a1 + t1; count = t2— three instructions, one temp, suite-generated.- Swap
b2tostr: the walker rejectsb2 * 3with the lexer's position — positions are the pipeline's error currency.
{exercise:uni-cs1106-program}
تدريب
count = a1 + b2 * 3 emits IR —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Three-address: one operation per line, one temp here.
English
Why an IR
- Direct translation multiplies: $m$ languages $\times$ $n$ targets = $mn$ compilers; an IR makes it $m + n$. Optimisations live on the IR — folding is literal-only ($2 \times 3 \to 6$; $b_2 \times 3$ stays), and each optimisation is a proof about the IR's semantics.
- Check names before types (scoping first), decorate fully before emitting, and never fold through non-literal operands.
{exercise:uni-cs1106-ir}
تدريب
Constant folding applies to —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
b2 * 3 stays: b2 is not a literal - folding is literal-only.