LightMySky
به انگلیسی

Semantic Analysis and the Intermediate Representation

Syntactically valid code can fail a language's name, type or argument-count rules. Semantic analysis uses declaration and scope information to check those rules. A shared intermediate representation can connect language front ends to multiple target back ends.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Name three syntactically valid constructs that fail the lesson's static semantic checks
  • Explain what a symbol table holds and when it is consulted
  • Say why lowering to an intermediate form is worth the extra pass

1 · Read

The parser you studied checks grammatical structure. Here names must resolve to declarations in scope, + requires two numbers without converting strings, and functions have fixed parameter counts. An undeclared name, incompatible operands or an incorrect argument count can have valid syntax but fail semantic analysis. Some compilers combine parsing with semantic checks; we separate their jobs for study.

Try it together

Suppose paint is declared to take three numbers. paint(2, 4) has valid call syntax but only two arguments. A semantic check compares that count with paint's declaration and reports the mismatch. Type checking is separate: even a three-argument call must supply the required types.

A symbol table records declarations and facts such as kind, type, scope and function parameters. Scope is the region where a declaration is visible. This model adds entries at declarations and consults them at name uses, following scope rules. A declaration elsewhere may not be visible here. The checking code applies the language rules using those entries. Actual table layouts and lookup timing vary between compilers.

declare paint: 3 numbersrecord declarationcall paint(2, 4)lookup paint in scopecompare 2 arguments to 3
Compare the call's supplied arguments with the declaration's expected parameters.

An intermediate representation (IR) records program operations in a form shared by compiler stages. Lowering translates a checked tree into IR while preserving meaning. Different language front ends can produce the same IR, and compatible target back ends can consume it. When suitable back ends already exist, a new language can reuse them. This does not guarantee smaller translators or support for every target.

language front endshared IRselected target back endtarget instructions

Parsing checks structure. Semantic analysis applies name, type and call rules using declaration information. A shared IR can connect several front ends and back ends.

Lesson, questions and flashcards on semantic analysis and intermediate representations written for this dataset.

2 · Watch

به انگلیسی

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Semantic Analysis and the Intermediate Representation · Computing, ages 21 to 22 · LightMySky