Q.How does the computer understand a program written in high level language?
CBSENCERTSubjective· 3mImportance★★★★★est
31% · 10/32 Questions
✓ Free question
Concept understanding — Compiler Translation Process
The Compiler Translation Process
Imagine you are giving instructions to a friend who only speaks French, but you only speak English. You cannot just shout English at them and expect them to build the bookshelf you described. You need a translator — someone who understands both languages, reads your English instructions, and writes out the equivalent instructions in French.
A computer faces exactly this problem. You write your program in a high-level language (like C, C++, or Java) — a language designed for human readability, with words like if, while, and function. The computer's processor, however, understands only machine language — raw binary (0s and 1s) or, at best, a very simple symbolic form called assembly language. The processor has no idea what if (x > 5) means.
The compiler is that translator. It takes your entire high-level program as input and produces an equivalent machine-language program as output. The key word is entire: a compiler translates the whole program at once, before you run it. This is different from an interpreter, which translates and runs your code line by line.
The Intuition: A Multi-Stage Factory
Think of a compiler as a factory assembly line. Your source code enters at one end, and a finished executable program exits at the other. The factory has several stations, each performing a specific transformation:
Lexical Analysis (Scanning): The factory reads the raw text and breaks it into meaningful tokens — like sorting Lego bricks by colour and shape. int x = 5; becomes the tokens: int, x, =, 5, ;.
Syntax Analysis (Parsing): The factory checks whether the tokens form a valid sentence in the language's grammar. It builds a parse tree — a hierarchical structure showing how tokens relate. If you wrote int x = ;, the parser would reject it as grammatically invalid.
Semantic Analysis: The factory checks for meaning. Is x declared before use? Are you adding a string to an integer? This is where type-checking happens. The parse tree gets annotated with type information.
Intermediate Code Generation: The factory produces a low-level, machine-independent representation of your program — often called three-address code or IR (Intermediate Representation). This is like translating English into a simplified, universal "bridge language" that is neither English nor French.
Optimization: The factory improves the intermediate code without changing its meaning. It removes dead code, folds constant expressions, and rearranges instructions to run faster. This is the "make it efficient" station.
Code Generation: The factory translates the optimized intermediate code into the target machine's assembly language (or directly into machine code). This is the final translation from the bridge language into French.
Code Optimization (Target-Specific): A final pass optimizes the generated machine code for the specific processor — choosing the fastest instructions, managing registers efficiently.
Note
Not all compilers have exactly these seven phases, and some combine adjacent phases. But the logical flow — from text to tokens to tree to intermediate code to machine code — is universal.
The Precise Statement
A compiler is a program that reads a program written in a source language (high-level) and translates it into an equivalent program in a target language (typically machine code or assembly). The translation preserves the meaning (semantics) of the source program. The compiler reports any errors it detects in the source program — lexical, syntactic, or semantic — and produces no target code if errors are found.
The entire process is called compilation, and the output is an executable file that can be run directly on the hardware, without the compiler being present.
Source ProgramCompilerTarget Program (Executable)
Why This Matters for Exams
You will often be asked to list and explain the phases of a compiler, or to distinguish between a compiler and an interpreter. The key points to remember:
A compiler translates all source code at once → produces a standalone executable.
An interpreter translates and executes one statement at a time → no standalone executable.
Compilation is a multi-phase process: analysis (front end) + synthesis (back end).
The front end (lexical, syntax, semantic analysis, intermediate code generation) is language-dependent but machine-independent.
The back end (code generation, optimization) is machine-dependent but language-independent.
Tip
To remember the phases in order, use the mnemonic: Lexical → Syntax → Semantic → Intermediate → Optimization → Code → Optimization. Or simply: Let Some Smart Indian Officer Catch Offenders.
A Concrete Example
Consider this tiny C program:
int main() {
int a = 5;
int b = a + 3;
return b;
}
The compiler's phases would handle it like this:
Phase
What Happens
Lexical
Tokens: int, main, (, ), {, int, a, =, 5, ;, ...
Syntax
Builds a parse tree showing main is a function, a and b are declarations, etc.
Semantic
Checks that a is declared before use, that a + 3 is valid (both integers), that return b matches the return type int.
Intermediate
Generates three-address code: t1 = 5, a = t1, t2 = a + 3, b = t2, return b
Optimization
Might notice a is never used after b is computed, so could eliminate a entirely: t1 = 5, t2 = t1 + 3, b = t2, return b
Code Gen
Translates to assembly: MOV R1, #5, ADD R2, R1, #3, MOV R0, R2, BX LR
Target Opt
Might replace MOV R1, #5 with a more efficient instruction depending on the CPU.
The final output is a binary executable that the processor can run directly.
Important
The compiler never runs your program. It only translates it. Running the program is a separate step, done by the operating system loading the executable into memory.
A computer's CPU understands only machine language — patterns of 0s and 1s. A program written in a high-level language (Python, C++, Java) is called source code, and the CPU cannot execute it directly.
A language translator converts the source code into machine code:
A compiler translates the entire program in one go into an executable machine-code file (e.g. C, C++). The translated file can then run again and again without re-translation.
An interpreter translates and executes the program one line at a time, every time it is run (e.g. Python). It stops at the first line containing an error.
The computer understands a high-level-language program only after a translator — a compiler (whole program at once) or an interpreter (line by line) — converts the source code into binary machine code, which is the only language the CPU can execute.
The CPU executes only binary machine code, so a high-level-language program must first be converted by a language translator — a compiler (all at once) or an interpreter (line by line) — before the computer can "understand" it.
The idea. Hardware is built from digital circuits that recognise exactly two states — 0 and 1. The only language a CPU natively executes is therefore machine language: binary-coded instructions specific to that processor. Humans, on the other hand, find it far easier to write in a high-level language (HLL) such as Python, C++ or Java, whose statements resemble English and mathematics. The program we write in an HLL is called the source code, and there is a gap between the two: the CPU simply cannot run source code as-is.
The bridge: language translators. System software called a translator converts source code into machine code. There are two main kinds:
Translator
How it works
Examples
Compiler
Reads the entire source program, checks it, and translates it in one go into a machine-code (object/executable) file. Translation happens once; afterwards the executable runs directly, without the compiler. All syntax errors are reported together after scanning the whole program.
C, C++ compilers
Interpreter
Translates and immediately executes the source code one line at a time, every time the program runs. It halts at the first erroneous line, so errors surface one at a time. No separate executable file is produced.
Python, JavaScript
The overall flow:
High-level source code
|
v
Compiler / Interpreter (translation)
|
v
Machine code (0s and 1s)
|
v
CPU fetches, decodes and executes
(For completeness: a program written in assembly language is converted to machine code by an assembler, a third kind of translator.)
Watch out
Don't say the computer "learns" the high-level language — it never does. The translation step always happens; with an interpreter it just happens invisibly on every run.
✓Final answer
A computer understands a high-level-language program only through a language translator: a compiler converts the whole source code into machine code at once, while an interpreter converts and executes it line by line — in both cases producing the binary machine instructions that the CPU can actually execute.