Master the fundamental concepts of semantic analysis through this focused micro-challenge.
TypeScript reports used before its declaration; C warns on implicit declarations. OpenSSL once shipped bugs from calling functions that were never declared. The fix is a two-pass or ordered walk: process declarations before uses in each scope, or reject forward references where the language forbids them.
Maintain a set of declared names per scope. On declaration, insert with line number. On use, verify the name exists and that the declaration line precedes the use line. Distinguish undefined (never declared) from use-before-decl (declared later).
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will detect variables used before declaration and references to undefined names. This exercise asks you to walk simple statements, maintain a declaration table, and emit precise errors with line numbers.
Implement detection of variables used before declaration.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github