Compiler Design - Lecture 1
Introduction to Compiler
Stage 3 - Computer Science
Department
What is a Compiler?
• • 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.
Why Do We Need 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.
Phases of a Compiler
• 1. Lexical Analysis
• 2. Syntax Analysis
• 3. Semantic Analysis
• 4. Intermediate Code Generation
• 5. Code Optimization
• 6. Code Generation
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
Syntax vs Logical Errors
• • 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;)
Thinking Question
• Imagine if 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.

Compiler Design Theory, Tools, and Examples

  • 1.
    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.
  • 4.
    Phases of aCompiler • 1. Lexical Analysis • 2. Syntax Analysis • 3. Semantic Analysis • 4. Intermediate Code Generation • 5. Code Optimization • 6. Code Generation
  • 5.
    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.