Unit content
Parsing, semantic analysis and intermediate representations
A compiler must determine what source text means before it can translate that meaning into machine code.
Lexing and parsing
The source is first organized into tokens and grammatical structure. A parser converts expressions, declarations and statements into a structured representation such as an abstract syntax tree (AST).
For example,
x + y * 2
is represented so that multiplication belongs inside the addition rather than being treated as a flat sequence of characters.
Semantic analysis
Grammar alone cannot determine whether a program is meaningful. The compiler resolves names, checks type rules, verifies declarations and attaches semantic information to the parsed structure.
Intermediate representations
Compilers often lower the program through one or more intermediate representations (IRs) between source syntax and final machine instructions.
An IR can make control flow, data flow or operations explicit in a form suited to analysis and optimization.
Separate meaning from syntax
Different source constructs can lower to the same IR operation, and one source language can target several machine architectures. Intermediate representations help separate language-specific syntax from target-specific code generation.