A compiler is a software that translates code written in a high-level programming language into a machine language that a computer's processor can execute. The process of compilation involves several phases, each transforming the source code in different ways. Here are the phases of a compiler and their corresponding outputs for the expression a := b + c * 50
:
Lexical Analysis:
Task: The lexical analyzer (or lexer) scans the source code and converts it into a sequence of tokens.
Output: Tokens representing the smallest units of meaning.
Example Output:
IDENTIFIER(a), ASSIGN_OP(:=), IDENTIFIER(b), PLUS_OP(+), IDENTIFIER(c), MULT_OP(*), NUMBER(50)
we
Syntax Analysis:
Task: The syntax analyzer (or parser) takes the tokens and arranges them into a syntax tree (parse tree) that represents the grammatical structure of the code according to the language's syntax rules.
Output: A parse tree.
Example Output:
Assignment
/ \\\\
a Expression
/ \\\\
b Term
/ \\\\
c Factor
|
50