Quiz 2

Compiler Overview — Phases, Front-end vs Back-end, Tools

509 words
3 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Compiler Overview — Phases, Front-end vs Back-end, Tools ## 🎯 Learning Objectives - List and describe the phases of a compiler - Explain the front-end vs back-end distinction - Describe the role of Flex and Bison - Trace a program through all compilation phases * * * ## 1. Compiler Phases ### 1.1 Intuition A comp...

Compiler Overview — Phases, Front-end vs Back-end, Tools

🎯 Learning Objectives

  • List and describe the phases of a compiler
  • Explain the front-end vs back-end distinction
  • Describe the role of Flex and Bison
  • Trace a program through all compilation phases

1. Compiler Phases

1.1 Intuition

A compiler translates a high-level language (like C) into low-level machine code. This complex task is broken into phases — each phase transforms the program representation step by step.

1.2 Phase Overview

(Diagram)

1.3 Phase Detail

PhaseInputOutputKey Concepts
Lexical AnalysisSource charactersTokensRegex, NFA, DFA, Flex
Syntax AnalysisTokensParse Tree / ASTCFG, LL/LR parsing, Bison
Semantic AnalysisASTAnnotated ASTType checking, symbol table
Intermediate Code GenASTTAC / IR3-address code, temp variables
OptimizationIROptimized IRCSE, constant folding, LICM
Code GenerationIRAssembly/MachineRegister allocation, instruction selection

2. Front-end vs Back-end

(Diagram) Front-end: Depends on source language, independent of target machine Back-end: Depends on target machine, independent of source language Benefits: Reuse back-end for multiple languages (GCC), reuse front-end for multiple machines (LLVM).

3. Compiler Construction Tools

3.1 Flex (Lexical Analyzer Generator)

Takes regular expression patterns → generates C code for a DFA-based scanner

3.2 Bison (Parser Generator)

Takes a CFG grammar → generates C code for an LALR(1) parser (Diagram)

4. 📝 Practice Questions

Q1: List the six major phases of a compiler in order.
Answer: Lexical Analysis → Syntax Analysis → Semantic Analysis → Intermediate Code Generation → Optimization → Code Generation Q2: What is the advantage of using an intermediate representation (IR)?
Answer: IR decouples the front-end from the back-end. The same IR can be used for multiple source languages (C, C++, Fortran → same IR) and multiple target architectures (x86, ARM, RISC-V). This dramatically reduces the number of components needed (n languages × m architectures → n+m components instead of n×m). Q3: What does Flex generate? What does Bison generate?
Answer: Flex generates a C function called yylex() that performs lexical analysis (a DFA-based tokenizer). Bison generates a C function called yyparse() that performs syntax analysis (an LALR(1) parser). The two are linked: yylex() is called by yyparse() to get the next token. Q4: What is triple-address code (TAC)?
Answer: TAC is an IR where each instruction has at most three operands (two sources, one destination). Example: t1 = a + b; t2 = t1 * c;. It's called "triple" or "three-address" code because instructions reference at most three variables. TAC is easy to generate and simplifies optimization. Q5: Why are compilation phases separated rather than combined?
Answer: Separation provides: (1) modularity (each phase can be developed/tested independently), (2) reusability (front-end shared across targets), (3) manageability (each phase has a focused task), and (4) additional optimization (IR simplifications between phases).

5. 🔗 Cross-References

  • Week 2 - Lexical Analysis: First phase in detail
  • Week 3 - Syntax Analysis: Second phase
  • BSCS3021 (ToC): Regular languages, CFG theory Join Discord NextLexical Analysis
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.