Interpreter Execution Cycle: Parse and Execute
You'll learn how interpreters process source code statement-by-statement in a continuous read-parse-execute loop.
The Core Idea
Unlike compilers that translate an entire program into machine code before running it (the Ahead-of-Time model you've seen), interpreters process source code directly. They follow a simple cycle: read a statement, parse it into an AST, then immediately execute it, repeating until the program ends.
Think of it like following a recipe while cooking: you read one instruction, understand what it means, do it, then move to the next instruction. You never write down translated "cooking machine code" first—you just act on each step as you encounter it.
The Three-Phase Cycle
- Read: Fetch the next line or statement from the source code
- Parse: Use lexical analysis (tokens) and parsing (building an AST fragment) to understand the statement's structure
- Execute: Traverse the AST and perform the operation immediately, updating program state (variables, memory) in real-time
This cycle repeats for each statement. If you assign x = 5, the interpreter parses that assignment, updates x in memory right now, then moves to the next line. No separate compilation phase exists.
Contrast with Compilation
Remember the translation pipeline you studied? Compilers go: source → tokens → AST → intermediate code → machine code → then execute. Interpreters compress this: source → tokens → AST → execute immediately, skipping code generation entirely.
Key Takeaway: Interpreters execute programs by repeatedly reading, parsing, and immediately executing each source statement in a continuous cycle, maintaining program state throughout rather than producing standalone compiled output.