Understanding interpretation


Where compiled languages translate source code into machine-executable form ahead of time, interpreted languages like Python and JavaScript execute instructions directly without producing a standalone machine code program. Instead, an interpreter reads the program step by step, analyses it, and executes it immediately. This makes interpretation highly flexible and dynamic, but often slower than compilation because translation happens during execution rather than beforehand.

During execution, the interpreter performs tasks such as lexical analysis, parsing, and evaluation, all while the program is running. It continuously processes code, manages variables, and executes instructions as needed.

Let's take a closer look at each of these steps using the code below to understand how source code becomes a running application.

  1. Typescript

  2. Javascript

  3. Python

Lexical analysis (Tokenisation)

Lexical analysis is the first stage of execution in an interpreted language, where the raw source code is read as plain text and broken down into meaningful units called tokens. Instead of executing characters directly, the interpreter scans the code from left to right, grouping sequences of characters into categories such as keywords (if, while), identifiers (variable and function names), operators (+, ==), and literals (numbers, strings). This step helps the interpreter make sense of the program structurally, turning a stream of text into an organised list of symbolic elements that the later stages can process more efficiently.

For example, in the statement x = x + 5, the lexer identifies the tokens as:

Tokenisation

Liking the course? Check our discounted plans to continue learning.