1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Compiler
• A program that translates source code
written in a high-level language (like C, Java) into machine
code (low-level language) that a computer can execute.
• It performs this translation before the program runs.
• Output is usually executable code or intermediate code.
Ensures code is correct and safe to run

Benefits of a compiler
• Performance: Optimized machine code runs faster than interpreted
code.
• Early Error Detection: Compile-time errors catch bugs before
execution.
• Security: Invalid or malicious patterns can be caught during compilation.
• Portability: Same source code can target different architectures by changing the compiler backend
Compiler vs Interpreter
• Compiler: Translates entire code once, produces executable, faster runtime.
• Interpreter: Executes code line by line, slower but more flexible.
• Examples:
• Compiler: C, C++ programs.
• Interpreter: Python, JavaScript engines.

Another diagram of a compiler

Lexical Analysis
1st phase of a compiler
It reads the raw source code character-by-character and groups those characters into meaningful units called lexemes, then converts those lexemes into tokens.
If the compiler were a pipeline, this is the part that turns the messy text you typed into clean, structured data that the rest of the compiler can understand.
Why is Lexical Analysis needed?
• The parser can’t work directly on raw characters.
• Code needs to be broken into logical “words” before grammar rules
can apply.
• Errors like illegal characters must be caught early.
• Spaces, comments, and formatting must be ignored.
• Lexical analysis extracts meaning out of a stream of characters.
Lexeme
• The actual sequence of characters pulled directly from the source code.
• Think of it as: The raw “word” the programmer typed, A concrete substring taken from the character stream
• Examples: x, 123, while, +, "Hello"
• These are literal pieces of text.
Token
• The category or classification that a lexeme belongs to.
• A label the compiler attaches to a lexeme.
• Examples:
ID - for variable names like x, count, sum.
NUMBER - for 123, 45.6
KEYWORD_WHILE or KEYWORD
OP_PLUS
STRING_LITERAL
• Is like saying: “This lexeme belongs to this grammatical class.”
Lexical Analyzer Tasks
• Reads the input as a continuous stream of characters.
Example: int sum = a + 10;
• Groups characters into lexemes.
Examples: int, sum , = , a , + , 10 , ;
• Converts each lexeme into a token.
Examples: KEYWORD_INT, ID , ASSIGN , ID, PLUS, NUMBER, SEMICOLON
• Output:
KEYWORD_INT, ID(sum), ASSIGN, ID(a), PLUS, NUMBER(10), SEMICOLON
What are the tokens and lexemes for x = y + 5?

What are the tokens and lexemes for int age = 21

What are the lexemes and tokens for result = (a+b)*3

What are the lexemes and tokens for while(count<10){ count=count+1; }?

What are the lexemes and tokens for total = price * quantity / 2.5;

Step 1 of a Compiler
Lexical Analysis
Step 2 of a Complier
Syntax Analysis/Parsing
Step 3 of a compiler
Semantic Analysis
Step 4 of a Compiler
Intermediate Code Generation
Step 5 of a Compiler
Code Optimization
Step 6 of a Compiler
Target Code Generation
ID
Token type for variable names
Ex:
x
count
sum
NUMBER
Token type for integers, floats
EX:
123
45.6
KEYWORD
Token type for reserved words
EX:
if
else
while
Another way to write the token type is KEYWORD_{insert keyword here}
OP
A token type representing operators
EX:
+
-
*
The tokens of these would be written as {}_PLUS, {}_MINUS, and {}_MUL
STRING_LITERAL
Token type for strings
EX:
“Hello World”
ASSIGN
Token type for =
SEMICOLON
Token type for a ;
L/RPAREN
Token type for (,)
L/RBRACE
Token type for {,}
FLOAT_LITERAL
Token type for decimals
EX:
2.5
Alternative token expression for operators
+: PLUS
-: MINUS
*:MULTIPLY