Compiler Design -Lecture 1
Introduction to Compiler
Stage 3 - Computer Science
Department
2.
What is aCompiler?
• • A compiler is a program that translates code
written in a high-level programming language
into machine code.
• • It allows programmers to write in human-
readable languages while enabling the
computer to execute the instructions.
• • Example: Translating C++ code into Assembly
or Machine Code.
3.
Why Do WeNeed a Compiler?
• • Computers only understand binary (0s and
1s).
• • Writing in machine code is complex and
error-prone.
• • Compilers make programming easier,
efficient, and portable.
• • Provide error detection (syntax/semantic
errors) and optimizations.
Example: int a= b + 5;
• • Lexical Analysis → Tokens: [int], [a], [=], [b],
[+], [5], [;]
• • Syntax Analysis → Valid assignment
expression
• • Semantic Analysis → Checks types of
variables
• • Intermediate Code → t1 = b + 5; a = t1
• • Optimization → a = b + 5
• • Code Generation → Assembly instructions
6.
Syntax vs LogicalErrors
• • Syntax Error: Missing semicolon in C code.
• Example: int x = 5 // Error
• • Logical Error: Program compiles but gives
wrong results.
• Example: Division by zero (int z = x / 0;)
7.
Thinking Question
• Imagineif compilers did not exist:
• • How would programmers write code?
• • Would programming be practical using only
0s and 1s?
• • Discuss the importance of compilers in
software development.