LightMySky
En anglais

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.

Commence ce sujetPas besoin de compte. La progression est enregistrée dans ce navigateur.

Ce que l'enfant saura faire ensuite

  • 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 · Lis

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 · Regarde

En anglais

Sans écran

Imprimer une fiche d'exercicesFormat A4, avec une page de corrigés pour les adultes. Sans écran, sans internet.

Où se situe ce sujet

Puis s'entraîner

8 questions attendent derrière cette leçon, chacune avec sa réponse expliquée. Chaque réponse nourrit le ciel : les étoiles s'allument à mesure qu'elles sont apprises, et pâlissent quand vient le moment de revenir.

Commence ce sujet