[go: up one dir, main page]

0% found this document useful (0 votes)
230 views63 pages

Compiler Structure and Phases Overview

The document outlines the construction and phases of compilers, including the roles of lexical analysis, syntax analysis, semantic analysis, intermediate code generation, optimization, and code generation. It discusses the evolution of programming languages from machine language to high-level languages and the impact of these developments on compiler design. Additionally, it emphasizes the scientific principles underlying compiler construction, including mathematical models and optimization techniques.

Uploaded by

mtmind4mo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views63 pages

Compiler Structure and Phases Overview

The document outlines the construction and phases of compilers, including the roles of lexical analysis, syntax analysis, semantic analysis, intermediate code generation, optimization, and code generation. It discusses the evolution of programming languages from machine language to high-level languages and the impact of these developments on compiler design. Additionally, it emphasizes the scientific principles underlying compiler construction, including mathematical models and optimization techniques.

Uploaded by

mtmind4mo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COMPILER CONSTRUCTION

Table of Contents
1. Introduction – p.1
2. A Simple Syntax-Directed Translator – p.39
3. Lexical Analysis – p.109
4. Syntax Analysis – p.191
5. Syntax-Directed Translation – p.303
6. Intermediate-Code Generation – p.357
7. Run-Time Environments – p.427
8. Code Generation – p.505
9. Machine-Independent Optimizations – p.583
10. Instruction-Level Parallelism – p.707
11. Optimizing for Parallelism and Locality – p.769

Appendices

 Appendix A: A Complete Front End – p.903


 Appendix B: Finding Linearly Independent Solutions – p.927

Lecture 1 – Introduction
1.1 Language Processors

 Definition of Compiler
 Difference between Compiler and Interpreter
 Java as a Hybrid (Compilation + Interpretation)
 Language Processing System
o Preprocessor
o Compiler
o Assembler
o Linker & Loader

1.2 The Structure of a Compiler

 Two parts of Compilation


o Analysis (Front-end)
o Synthesis (Back-end)
 Phases of a Compiler
o Lexical Analysis
o Syntax Analysis
o Semantic Analysis
o Intermediate Code Generation
o Code Optimization
o Code Generation
 Symbol Table Management
 Grouping of Phases into Passes
 Compiler Construction Tools

1.3 The Evolution of Programming Languages

 Early Programming (Machine Language, Assembly)


 High-Level Languages (Fortran, Cobol, Lisp)
 Generations of Languages (1st → 5th)
 Language Classifications
o Imperative
o Declarative
o Von Neumann Languages
o Object-Oriented Languages
o Scripting Languages
 Impacts on Compilers

1.4 The Science of Building a Compiler

 Compilers as Mathematical Abstractions


 Modeling in Compiler Design
o Finite State Machines & Regular Expressions
o Context-Free Grammars
o Syntax Trees
 The Science of Code Optimization
 Design Objectives for Optimizing Compilers

1.5 Applications of Compiler Technology

 Implementation of High-Level Languages


 Optimizations for Computer Architectures
o Parallelism
o Memory Hierarchies
 Design of New Computer Architectures (RISC, CISC)
 Program Translations
o Binary Translation
o Hardware Synthesis
o Database Query Interpreters
o Compiled Simulation
 Software Productivity Tools
o Type Checking
o Bounds Checking
o Memory-Management Tools

1.6 Programming Language Basics

 Static vs Dynamic Distinction


 Environments and States
o Names, Identifiers, Variables
 Static Scope and Block Structure
o Scope Rules
o Blocks and Nested Blocks

Lecture 1 – Introduction

Section 1.1 Language Processors

1. What is a Language Processor?

 A language processor is software that helps us run a program written in a


programming language.
 Human beings write code in high-level languages (C, C++, Java, Python).
 Computers only understand machine language (0s and 1s).
 A language processor converts high-level code into machine language.

👉 Without a language processor, the computer cannot understand our program.


2. What is a Compiler?

 A compiler translates the whole source program (written by the programmer) into a
complete target program (machine code).
 After compilation, the target program can run directly on the computer.

👉 Example:
If you write in C:

a = b + c;

The compiler translates it into machine instructions like:

LOAD R1, b
LOAD R2, c
ADD R1, R2
STORE a, R1

3. What is an Interpreter?

 An interpreter does not produce a separate machine code file.


 Instead, it reads your program line by line and directly executes it.

👉 Example:

 In Python, if you write:

print("Hello World")

The interpreter will execute it immediately, without creating an extra file.

4. Difference Between Compiler and Interpreter

Feature Compiler Interpreter

How it works Translates the whole program at once Translates and runs line by line

Speed of execution Fast (machine code runs directly) Slow (checks each line during execution)

Error reporting Shows all errors after compilation Shows errors immediately

Examples C, C++ Python, JavaScript


5. Hybrid Language Processing (Java Example)

 Java uses both compilation and interpretation.

1. Java code is first compiled into Bytecode (intermediate form).


2. Bytecode is run by the Java Virtual Machine (JVM) using interpretation or Just-In-
Time (JIT) compilation.

👉 Benefit: The same Java program runs on any operating system (Windows, Linux, Mac).

6. Language Processing System

When we run a program, several steps happen.

Steps in C Program Compilation:

1. Preprocessor – Handles #include and #define.


o Example: #include <stdio.h> adds the code for input/output.
2. Compiler – Translates code into assembly language.
3. Assembler – Converts assembly code into machine code (object file).
4. Linker – Combines object file with library files (e.g., printf function code).
5. Loader – Loads the final program into memory and runs it.

Flow:

Source Code → Preprocessor → Compiler → Assembler → Linker → Loader →


Execution

7. Simple Example

Suppose we write in C:

#include <stdio.h>
int main() {
printf("Hello");
return 0;
}

Step by Step:

1. Preprocessor adds stdio.h code.


2. Compiler converts program into assembly.
3. Assembler changes assembly into object code.
4. Linker attaches library functions like printf.
5. Loader loads and executes program → Output: Hello

Section 1.2 The Structure of a Compiler

1. What is the Structure of a Compiler?

 A compiler is not just one single step.


 It works in phases, each phase doing one specific job.
 These phases are divided into two main parts:
1. Analysis (Front End) → Understands the source program.
2. Synthesis (Back End) → Produces the target (machine) program.

👉 Example: When you translate English to Urdu, first you understand the sentence (analysis),
then you write it in Urdu (synthesis).

2. Two Major Parts of Compilation

1. Analysis (Front End)


o Breaks the program into smaller pieces.
o Checks grammar and meaning.
o Creates an intermediate representation (a simpler form of the program).
o Stores important information in a symbol table (like a dictionary for program
variables).
2. Synthesis (Back End)
o Uses the intermediate representation.
o Converts it into the target machine code.
o Produces optimized and executable code.

3. Phases of a Compiler

The compiler works in 6 main phases:

a. Lexical Analysis (Scanner)

 Reads the source code character by character.


 Groups characters into tokens (keywords, identifiers, numbers, symbols).
 Example:
Code → x = y + 5;
Tokens → id(x), =, id(y), +, number(5), ;

b. Syntax Analysis (Parser)

 Arranges tokens into a tree structure (syntax tree).


 Checks if the program follows grammar rules.
 Example:
Expression x = y + 5
Parse Tree shows + done first, then assignment =.

c. Semantic Analysis

 Checks meaning of the program.


 Ensures type consistency.
 Example:
x = y + 5;
o If x is a float and y is an integer, compiler automatically converts types if allowed.
o If x was a string, error occurs.

d. Intermediate Code Generation

 Creates a temporary code between source and machine code.


 Easier for optimization and translation.
 Example (Three-address code):
 t1 = y + 5
 x = t1

e. Code Optimization

 Improves the intermediate code for efficiency.


 Makes the program faster or smaller.
 Example:
Instead of
 t1 = y + 5
 x = t1

Optimized code is simply:

x = y + 5
f. Code Generation

 Converts optimized intermediate code into final machine code.


 Assigns registers and memory locations.
 Example (Machine-like instructions):
 LOAD R1, y
 ADD R1, 5
 STORE x, R1

4. Symbol Table

 A data structure used by all compiler phases.


 Stores information about variables, functions, objects.
 Example:
For code int a, b; float c;
Symbol Table will contain:
 a → integer
 b → integer
 c → float

5. Grouping into Passes

 A pass = one complete scan of the source program.


 Sometimes multiple phases are combined in a single pass.
 Example:
o One-pass compiler → Fast, used for small languages.
o Multi-pass compiler → More accurate, used for big languages like C++/Java.

6. Compiler Construction Tools

Special tools that help in building compilers:

1. Scanner Generators → Build lexical analyzers.


2. Parser Generators → Build syntax analyzers.
3. Syntax-directed translation tools.
4. Code-generator tools.
5. Data-flow analysis tools.

Diagram – Phases of Compiler


Source Program

Lexical Analysis → Tokens

Syntax Analysis → Parse Tree

Semantic Analysis → Annotated Tree

Intermediate Code Generation → IR Code

Code Optimization → Improved IR Code

Code Generation → Machine Code

Summary of Section 1.2

 Compiler has two main parts: Analysis (Front End) and Synthesis (Back End).
 Phases: Lexical, Syntax, Semantic, Intermediate Code Generation, Optimization, Code
Generation.
 Symbol table stores information about identifiers.
 Compilers may be one-pass or multi-pass.
 Tools like scanner generators and parser generators help in compiler construction.

Section 1.3: The Evolution of Programming Languages

1. Early Days of Programming

 The first computers (1940s) were programmed using machine language.


 Machine language = only 0s and 1s.
 Very hard for humans to write and understand.

👉 Example:
To add two numbers, a programmer had to write a long binary code like:

10110000 00000001
10110001 00000010
00000011 11000000

 After that, assembly language was created.


 Assembly used short mnemonics (symbols) instead of 0s and 1s.

👉 Example:
Instead of binary code, we write:

MOV R1, 5
MOV R2, 10
ADD R1, R2

This was easier but still close to machine hardware.

2. High-Level Languages (1950s–1960s)

 Next step was high-level languages → easier for humans.


 Programs could be written in something close to English.

👉 Examples:

 FORTRAN → for scientific calculations.


 COBOL → for business data processing.
 LISP → for symbolic (AI-related) computation.

These languages are still used in some areas even today.

3. Generations of Languages

Programming languages are often grouped by generations:

1. First Generation (1GL): Machine language (0s and 1s).


2. Second Generation (2GL): Assembly language.
3. Third Generation (3GL): High-level languages like C, C++, Java, Python.
4. Fourth Generation (4GL): Languages for specific tasks (SQL for databases, MATLAB
for math, PostScript for text formatting).
5. Fifth Generation (5GL): Languages based on logic and constraints (Prolog, OPS5)
used in AI.

4. Types of Programming Languages

Languages can also be grouped by style (paradigm):


1. Imperative Languages
o Program tells the computer how to do something step by step.
o Example: C, C++, Java.
o Focus: how the program works.
2. Declarative Languages
o Program tells the computer what to do, not how.
o Example: Prolog (used in AI), SQL (used in databases).
o Focus: what result is needed.
3. Von Neumann Languages
o Based on the Von Neumann computer model (memory + processor).
o Example: Fortran, C.
4. Object-Oriented Languages
o Program is built using objects (data + functions together).
o Example: Java, C++, C#, Ruby.
5. Scripting Languages
o Mainly used for quick tasks, automation, and web.
o Example: Python, JavaScript, PHP, Perl.

5. Impact of Programming Languages on Compilers

 As languages evolved, compilers had to evolve too.


 Each new feature in languages (like objects, garbage collection, scripting) needed new
techniques in compilers.
 Modern compilers not only translate but also optimize code for speed, memory, and
power saving.

👉 Example:

 Old compilers only translated code.


 Modern compilers for Java, C++ improve execution speed, manage memory
automatically, and even support parallel execution on multi-core processors.

6. Example of Language Evolution

 Machine Language (1GL):


10110000 00000001
 Assembly Language (2GL):
MOV R1, 1
 High-Level Language (3GL):
x = 1;
 Fourth Generation (4GL):
SELECT name FROM students WHERE grade > 80;
 Fifth Generation (5GL):
(AI logic, Prolog style)
student(X) :- grade(X, Y), Y > 80.

Summary of Section 1.3

 Programming started with machine language (hard).


 Then came assembly, then high-level languages (easy).
 Languages are classified by generations (1GL → 5GL) and styles (imperative,
declarative, object-oriented, scripting).
 New languages forced new compiler techniques to handle them efficiently.

Section 1.4 The Science of Building a Compiler

1. Compilers as a Science

 Building a compiler is not only engineering but also science.


 Many parts of a compiler are based on mathematical models and formal methods.
 These models ensure the compiler is correct, efficient, and reliable.

2. Mathematical Foundations in Compilers

Compilers use different theories from computer science:

a. Finite State Machines (FSM) and Regular Expressions

 Used in lexical analysis.


 They help identify tokens (keywords, numbers, identifiers).
 Example: Regular expression for an identifier:
 [a-zA-Z][a-zA-Z0-9]*

Means → identifier must start with a letter, followed by letters or numbers.


b. Context-Free Grammars (CFG)

 Used in syntax analysis (parsing).


 Grammar rules describe the structure of the language.
 Example:
 expr → expr + term | term
 term → term * factor | factor
 factor → (expr) | id

This grammar describes arithmetic expressions.

c. Syntax Trees

 Used to represent program structure in a tree form.


 Easier for later phases like optimization and code generation.
 Example:
Expression: a + b * c

Parse Tree (structure):

+
/ \
a *
/ \
b c

3. Science of Code Optimization

 Optimization is about making programs run faster or use less memory.


 Techniques use data-flow analysis, graph theory, and algebra.
 Example:
 x = (y * 2) + (y * 2);

Optimizer changes to:

temp = y * 2;
x = temp + temp;

→ Faster execution.

4. Goals of Compiler Design

When building compilers, scientists and engineers aim for:


1. Correctness → Compiler must produce correct results.
2. Efficiency → Generated code should be fast and small.
3. Portability → Same compiler can work for different machines.
4. Maintainability → Compiler code should be easy to update.
5. Scalability → Should handle large programs and complex languages.

5. Example in Real Life

Think of a translator between two human languages:

 A poor translator may translate word by word (wrong meaning).


 A scientific translator uses rules of grammar and context → correct translation.
 Similarly, a scientific compiler uses formal rules and models to ensure the program
works correctly.

✅ Summary of Section 1.4

 Compiler construction is a science that uses mathematical models.


 FSM + Regular Expressions → lexical analysis.
 Context-Free Grammars → syntax analysis.
 Syntax Trees → represent program structure.
 Optimization uses mathematics to improve performance.
 Goals: correctness, efficiency, portability, maintainability, scalability.

Section 1.5 Applications of Compiler Technology

1. Why Are Compilers Important Beyond Programming?

 At first, compilers were only made to translate programs.


 Today, compiler technology is used in many fields beyond just running code.
 It helps in system design, performance optimization, program analysis, and even
hardware development.
2. Main Applications

a. Implementation of High-Level Languages

 Without compilers, languages like C, C++, Java, and Python cannot run.
 Compiler translates these high-level languages into machine code.
 Example: A C compiler converts C program into .exe file on Windows.

b. Optimizations for Computer Architectures

 Modern CPUs are complex (multi-core, pipelines, cache memory).


 Compilers make programs run faster by optimizing code for these architectures.

Examples:

 Reordering instructions to run in parallel.


 Adjusting memory access to fit into cache.
 Vectorization → using SIMD (Single Instruction, Multiple Data) for faster calculations.

c. Design of New Computer Architectures

 When engineers design a new processor (RISC, CISC, GPUs), they need compilers to
test programs on it.
 Compiler technology helps to:
o Simulate how programs would run on new hardware.
o Identify performance problems early.

👉 Example: Before Intel or AMD release a new CPU, they test compilers on it.

d. Program Translation

Compilers are also used to translate between different forms of programs:

1. Binary Translation
o Converting code from one machine to another.
o Example: Running Windows programs on Mac/Linux using translators.
2. Hardware Synthesis
o Converting programs into hardware circuits.
o Example: VHDL/Verilog compilers translate code into hardware design for
chips.
3. Database Query Interpreters
oSQL queries are compiled into efficient execution plans.
oExample: SELECT * FROM students WHERE marks > 80; → Compiler
translates into optimized database operations.
4. Compiled Simulation
o Used in scientific research for fast simulations.
o Example: Physics simulations compiled for supercomputers.

e. Software Productivity Tools

Compiler techniques are also used in tools that improve programming:

 Type Checking Tools → Detect mismatched data types.


 Bounds Checking Tools → Prevent array overflow errors.
 Memory-Management Tools → Detect memory leaks.

👉 Example: Tools like Valgrind and Lint use compiler technology to find bugs before
execution.

3. Everyday Examples

1. Web Browsers (JavaScript Engines) → Use Just-In-Time compilers for fast execution.
2. Mobile Apps → Android uses a Dalvik/ART compiler to optimize apps.
3. Game Development → Compilers optimize code to run smoothly on graphics-heavy
hardware.
4. Cloud Computing → Compilers optimize software for distributed systems.

4. Importance in Modern Computing

 Without compiler technology, high-level languages would not be practical.


 Compilers make programs:
o Portable (same program runs on Windows, Linux, Mac).
o Efficient (optimized for hardware).
o Reliable (detect errors early).

✅ Summary of Section 1.5

 Compiler technology is not only for programming but also used in architecture design,
binary translation, database queries, hardware synthesis, and productivity tools.
 Everyday technologies (web browsers, mobile apps, games, cloud) depend heavily on
compiler techniques.
 Compilers ensure programs are fast, portable, and reliable.

Section 1.6 Programming Language Basics

1. Why Study Language Basics?

 Every programming language (C, Java, Python, etc.) has rules and concepts.
 To build a compiler, we must understand:
o How variables are named.
o How memory is managed.
o How scope and blocks work.

This section introduces those basics.

2. Static vs Dynamic Distinction

 Static = Things decided at compile-time (before program runs).


 Dynamic = Things decided at run-time (while program runs).

👉 Examples:

 In C:
 int x = 5;

Here the type of x (integer) is decided statically at compile-time.

 In Python:
 x = 5
 x = "Hello"

Here the type of x can change dynamically at run-time.

3. Environments and States


 Environment = The mapping (connection) between names and objects.
 State = The values stored in memory at a particular time.

👉 Example in C:

int a = 10;
int b = 20;

 Environment:
 a → integer variable
 b → integer variable

 State:
 a = 10
 b = 20

So, environment tells what exists, and state tells current values.

4. Names, Identifiers, and Variables

 Name / Identifier = Symbol used to represent something (variable, function, class).


 Rules for identifiers vary by language.

👉 Example:
In C:

int totalMarks;

Here:

 totalMarks is an identifier.
 It represents a variable of type integer.

Rules in C/C++ identifiers:

 Must start with a letter or underscore (_).


 Cannot start with a digit.
 Case sensitive (Age ≠ age).

5. Static Scope and Block Structure

 Scope = The part of a program where a variable can be used.


 Static Scope (Lexical Scope) = The scope is fixed at compile-time based on program
structure.

👉 Example in C:

int x = 5; // global variable


int main() {
int y = 10; // local variable
printf("%d", x); // valid
printf("%d", y); // valid
}

 Here x can be used anywhere (global scope).


 y can be used only inside main (local scope).

6. Blocks and Nested Blocks

 A block is a group of statements inside { }.


 Variables declared inside a block are local to that block.
 Nested blocks = A block inside another block.

👉 Example:

int main() {
int a = 5; // outer block
{
int b = 10; // inner block
printf("%d", a); // valid
printf("%d", b); // valid
}
printf("%d", a); // valid
printf("%d", b); // ERROR (b not visible here)
}

 a is visible everywhere inside main.


 b is only visible inside the inner block.

✅ Summary of Section 1.6

 Static vs Dynamic → decided at compile-time vs run-time.


 Environment = mapping of names to objects.
 State = current values in memory.
 Identifiers = names of variables, functions, etc.
 Scope → where a variable is valid.
 Blocks & Nested Blocks → variables exist only inside their block.
SHORT QUESTIONS

Section 1.1 – Language Processors


Short Questions (10)

1. What is the role of a language processor?


2. Define a compiler in simple words.
3. How does an interpreter execute a program?
4. Write two main differences between compiler and interpreter.
5. Why is Java considered a hybrid language processor?
6. What is the purpose of a preprocessor in the language processing system?
7. What is the difference between an assembler and a linker?
8. Arrange the steps of a language processing system in order.
9. Why do we need a loader before program execution?
10. Give one example language that mostly uses an interpreter.

Section 1.2 – The Structure of a Compiler


Short Questions (10)

1. What are the two main parts of a compiler?


2. Define analysis phase in a compiler.
3. What is the purpose of the synthesis phase?
4. List the six main phases of a compiler.
5. What is the role of the lexical analyzer?
6. How does the parser (syntax analyzer) help in compilation?
7. Define semantic analysis with an example.
8. What is the purpose of a symbol table?
9. Differentiate between a one-pass compiler and a multi-pass compiler.
10. Name two tools used in compiler construction.
Section 1.3 – The Evolution of Programming Languages
Short Questions (10)

1. What was the first generation of programming languages?


2. Why was assembly language introduced after machine language?
3. Give one example of a third-generation language (3GL).
4. What is a fourth-generation language (4GL)? Give one example.
5. Define a fifth-generation language (5GL) in simple words.
6. What is the main idea of imperative languages?
7. How are declarative languages different from imperative languages?
8. What does the term object-oriented language mean? Give an example.
9. Why are scripting languages like Python or JavaScript popular today?
10. How did the evolution of languages affect compiler design?

Section 1.4 – The Science of Building a Compiler


Short Questions (10)

1. Why is compiler construction considered a science?


2. What is the role of finite state machines in a compiler?
3. Give one use of regular expressions in lexical analysis.
4. Define a context-free grammar (CFG).
5. Why are syntax trees important in compilation?
6. What is the purpose of code optimization?
7. Give an example where optimization makes a program faster.
8. List two design goals of compiler construction.
9. What does portability mean in compiler design?
10. How is building a compiler similar to building a translator between human languages?

Section 1.5 – Applications of Compiler Technology


Short Questions (10)

1. What is the main application of compiler technology?


2. How does a compiler help in the implementation of high-level languages?
3. Give one way compilers optimize code for modern processors.
4. Why is compiler technology important in CPU architecture design?
5. What is binary translation?
6. Define hardware synthesis with an example.
7. How do compilers help in executing SQL queries?
8. Name one software productivity tool that uses compiler techniques.
9. How are web browsers related to compiler technology?
10. Why are compilers important for mobile app performance?

Section 1.6 – Programming Language Basics


Short Questions (10)

1. What is the difference between static and dynamic features in programming languages?
2. Give one example of something decided statically at compile-time.
3. Give one example of something decided dynamically at run-time.
4. What is an environment in programming language basics?
5. Define the term state in relation to program execution.
6. What is an identifier? Give an example.
7. List two rules for writing identifiers in C.
8. What does the term scope mean in programming?
9. Differentiate between a global variable and a local variable.
10. What is a nested block? Give a simple example.

MCQS

Section 1.1 – Language Processors


MCQs

Q1. A compiler translates:


a) Machine code to source code
b) Source code to machine code
c) Assembly code to binary code
d) Machine code to high-level language

Q2. An interpreter:
a) Translates the whole program at once
b) Produces an executable file
c) Executes code line by line
d) Is slower to show errors than a compiler

Q3. Which of the following is an example of a compiled language?


a) Python
b) C
c) JavaScript
d) PHP

Q4. Which of the following is an example of an interpreted language?


a) C++
b) C
c) Python
d) Assembly

Q5. Java uses which approach for execution?


a) Only compilation
b) Only interpretation
c) Both compilation and interpretation
d) None of the above

Q6. The output of a Java compiler is:


a) Machine code
b) Bytecode
c) Assembly code
d) Source code
Q7. Which component expands macros and includes header files?
a) Loader
b) Linker
c) Preprocessor
d) Assembler

Q8. Which phase converts assembly code into machine code?


a) Preprocessor
b) Assembler
c) Compiler
d) Loader

Q9. The role of the linker is to:


a) Check syntax errors
b) Combine object code with libraries
c) Generate tokens
d) Translate high-level code into assembly

Q10. The loader:


a) Translates high-level language
b) Loads executable into memory
c) Links object files
d) Optimizes intermediate code

Q11. Which is faster in execution?


a) Compiler
b) Interpreter
c) Both are same
d) None

Q12. Which system produces the final executable file?


a) Language processing system
b) Parser
c) Tokenizer
d) Debugger
Q13. Error messages are shown immediately during execution in:
a) Compiler
b) Interpreter
c) Linker
d) Loader

Q14. Which is true about compilers?


a) They execute code line by line
b) They do not generate object code
c) They are faster in execution
d) They are only used in Java

Q15. The correct order of steps in the language processing system is:
a) Compiler → Loader → Preprocessor → Assembler → Linker
b) Preprocessor → Compiler → Assembler → Linker → Loader
c) Assembler → Compiler → Loader → Linker → Preprocessor
d) Preprocessor → Assembler → Compiler → Loader → Linker

1. b
2. c
3. b
4. c
5. c
6. b
7. c
8. b
9. b
10. b
11. a
12. a
13. b
14. c
15. b
Section 1.2 – The Structure of a Compiler
MCQs

Q1. The two main parts of a compiler are:


a) Syntax and Semantics
b) Analysis and Synthesis
c) Parsing and Optimization
d) Front-end and Loader

Q2. Which part of the compiler breaks the program into tokens?
a) Syntax Analysis
b) Lexical Analysis
c) Semantic Analysis
d) Code Generation

Q3. The parser checks:


a) Tokens
b) Grammar structure
c) Memory allocation
d) Machine instructions

Q4. The semantic analyzer ensures:


a) Tokens are valid
b) Grammar is correct
c) Meaning is consistent
d) Registers are assigned

Q5. Which phase generates intermediate code?


a) Lexical Analysis
b) Code Generation
c) Intermediate Code Generation
d) Optimization
Q6. The role of code optimization is to:
a) Detect errors
b) Make code efficient
c) Allocate registers
d) Translate assembly

Q7. Final machine code is produced in which phase?


a) Code Generation
b) Intermediate Code Generation
c) Semantic Analysis
d) Preprocessing

Q8. A symbol table stores:


a) Keywords only
b) Variables and their information
c) Machine instructions
d) Error messages

Q9. A compiler that translates the program in one scan is called:


a) Multi-pass compiler
b) One-pass compiler
c) Two-phase compiler
d) Hybrid compiler

Q10. Which compiler type is used for large languages like C++?
a) One-pass compiler
b) Multi-pass compiler
c) Just-In-Time compiler
d) None

Q11. Grouping of compiler phases into one scan is called:


a) Optimization
b) Parsing
c) Passes
d) Tokenization
Q12. Which tool helps in building lexical analyzers?
a) Parser generator
b) Scanner generator
c) Optimizer
d) Code generator

Q13. Yacc is an example of:


a) Scanner generator
b) Parser generator
c) Loader
d) Optimizer

Q14. Which part of a compiler connects the front-end and back-end?


a) Code Generator
b) Intermediate Code Generator
c) Preprocessor
d) Loader

Q15. The back-end of the compiler mainly deals with:


a) Lexical Analysis
b) Syntax and Semantics
c) Code Optimization and Code Generation
d) Preprocessing

✅ Answers
1. b
2. b
3. b
4. c
5. c
6. b
7. a
8. b
9. b
10. b
11. c
12. b
13. b
14. b
15. c

Section 1.3 – The Evolution of Programming Languages


MCQs

Q1. The first generation programming language is:


a) Assembly language
b) Machine language
c) High-level language
d) Scripting language

Q2. Assembly language is also called:


a) High-level language
b) Binary code
c) Low-level language
d) Fourth-generation language

Q3. Which of the following is a third-generation language (3GL)?


a) SQL
b) C
c) Prolog
d) Machine code

Q4. Which of the following is a fourth-generation language (4GL)?


a) Java
b) SQL
c) Assembly
d) C++
Q5. Prolog is an example of:
a) 3GL
b) 4GL
c) 5GL
d) 2GL

Q6. Which generation of languages is mainly used in Artificial Intelligence (AI)?


a) 2GL
b) 3GL
c) 4GL
d) 5GL

Q7. Imperative languages focus on:


a) What result is needed
b) How a task is done step by step
c) Only memory allocation
d) Logical rules only

Q8. Declarative languages focus on:


a) Algorithm steps
b) What the result should be
c) Hardware simulation
d) Register allocation

Q9. Which of the following is an example of an object-oriented language?


a) COBOL
b) FORTRAN
c) Java
d) SQL

Q10. Which language paradigm is most suitable for database queries?


a) Imperative
b) Declarative
c) Object-oriented
d) Scripting
Q11. JavaScript belongs to which category of languages?
a) Scripting language
b) 2GL
c) Declarative language
d) Assembly

Q12. Which language was designed for scientific calculations in the 1950s?
a) COBOL
b) LISP
c) FORTRAN
d) Prolog

Q13. COBOL was mainly designed for:


a) AI applications
b) Web development
c) Business data processing
d) System programming

Q14. LISP was created for:


a) Scientific computations
b) Business data
c) Symbolic computation and AI
d) Web scripting

Q15. Which statement is correct about the evolution of programming languages?


a) Assembly was easier than machine code.
b) High-level languages are harder than assembly.
c) Machine code is more user-friendly than 4GL.
d) SQL is a third-generation language.

✅ Answers
1. b
2. c
3. b
4. b
5. c
6. d
7. b
8. b
9. c
10. b
11. a
12. c
13. c
14. c
15. a

Section 1.4 – The Science of Building a Compiler


MCQs

Q1. Compiler construction is considered a science because it relies on:


a) Random coding practices
b) Mathematical models and theories
c) Hardware simulation only
d) Programming shortcuts

Q2. Finite State Machines (FSM) are mainly used in:


a) Syntax Analysis
b) Lexical Analysis
c) Code Optimization
d) Code Generation

Q3. Regular expressions are used to describe:


a) Tokens
b) Grammar rules
c) Optimization rules
d) Machine instructions

Q4. A Context-Free Grammar (CFG) is mainly used in:


a) Lexical Analysis
b) Syntax Analysis
c) Code Generation
d) Linking

Q5. Which compiler structure represents program hierarchy?


a) Symbol table
b) Parse tree
c) Syntax tree
d) Object code

Q6. The expression a + b * c is represented in a syntax tree with:


a) + as root, * as leaf
b) * as root, + as leaf
c) + as root, * as child
d) a as root, + as leaf

Q7. Code optimization focuses on:


a) Error detection
b) Making code efficient
c) Token generation
d) Machine translation

Q8. Which mathematical concept is often used in code optimization?


a) Graph theory
b) Probability
c) Statistics
d) Geometry
Q9. Which is NOT a goal of compiler design?
a) Correctness
b) Efficiency
c) Portability
d) Increasing program length

Q10. Portability in compiler design means:


a) Program works only on one machine
b) Program runs on different machines without change
c) Program executes line by line
d) Program is shorter

Q11. Which of the following represents the meaning of the program?


a) Tokens
b) Grammar
c) Semantics
d) Assembly code

Q12. A compiler is similar to a human language translator because:


a) Both guess meaning randomly
b) Both follow grammar and context rules
c) Both ignore structure
d) Both directly produce binary code

Q13. Syntax analysis is based on:


a) Context-free grammars
b) Memory addresses
c) Assembly instructions
d) Object files

Q14. The purpose of syntax trees is to:


a) Detect type errors
b) Show program structure
c) Allocate registers
d) Translate tokens to binary
Q15. Which of the following best describes the science of building a compiler?
a) Using shortcuts for translation
b) Applying formal models for correctness and optimization
c) Writing code without rules
d) Avoiding mathematics in translation

✅ Answers
1. b
2. b
3. a
4. b
5. c
6. c
7. b
8. a
9. d
10. b
11. c
12. b
13. a
14. b
15. b

Section 1.5 – Applications of Compiler Technology


MCQs

Q1. The primary role of a compiler is to:


a) Execute programs directly
b) Translate high-level language into machine code
c) Create operating systems
d) Simulate hardware

Q2. Which of the following is an application of compiler technology?


a) Web browsing only
b) Operating system design only
c) High-level language implementation
d) None of the above

Q3. Modern compilers optimize code mainly for:


a) Slow execution
b) Modern computer architectures
c) Manual debugging
d) Low-level assembly writing

Q4. Reordering instructions for faster execution is part of:


a) Binary translation
b) Code optimization
c) Hardware synthesis
d) Linking

Q5. Which of the following uses compiler technology?


a) SQL query execution
b) Word formatting only
c) PowerPoint design
d) Image editing

Q6. Binary translation means:


a) Translating one high-level language to another
b) Converting machine code of one system to another
c) Translating source code to binary directly
d) Changing syntax rules

Q7. Hardware synthesis is:


a) Translating hardware circuits into source code
b) Translating programs into hardware circuits
c) Designing only compilers
d) Loading programs in memory
Q8. Which field relies on database query interpreters that use compiler technology?
a) Business applications
b) Game design
c) Data science and databases
d) Graphics rendering

Q9. Which is a software productivity tool using compiler concepts?


a) Type checker
b) Word counter
c) Image filter
d) Video editor

Q10. Valgrind is an example of a tool that helps in:


a) Syntax analysis
b) Memory management and error checking
c) Hardware simulation
d) Register allocation

Q11. Which of the following uses Just-In-Time (JIT) compilation?


a) Web browsers (JavaScript engines)
b) Notepad
c) Image viewers
d) Music players

Q12. Android apps are optimized using:


a) Loader
b) Dalvik/ART compiler
c) Parser
d) Binary translator

Q13. Compilers help in CPU design by:


a) Debugging hardware directly
b) Simulating programs on new architectures
c) Removing source code
d) Avoiding registers
Q14. Games rely on compiler technology mainly for:
a) Faster graphics performance
b) Story design
c) Audio mixing
d) Internet connectivity

Q15. Which of the following is NOT an application of compiler technology?


a) High-level language translation
b) Code optimization
c) Database query execution
d) Manual handwriting recognition

✅ Answers
1. b
2. c
3. b
4. b
5. a
6. b
7. b
8. c
9. a
10. b
11. a
12. b
13. b
14. a
15. d

Section 1.6 – Programming Language Basics


MCQs

Q1. A feature decided at compile-time is called:


a) Dynamic
b) Static
c) Runtime
d) Flexible

Q2. A feature decided while the program is running is called:


a) Static
b) Dynamic
c) Syntax
d) Compile-time

Q3. In C, the type of a variable is decided:


a) Dynamically at runtime
b) Statically at compile-time
c) By the loader
d) By the assembler

Q4. In Python, a variable can hold different types at different times because it is:
a) Statically typed
b) Dynamically typed
c) Fixed typed
d) Optimized

Q5. The mapping of names to objects in a program is called:


a) State
b) Environment
c) Scope
d) Symbol

Q6. The current values stored in memory during program execution are called:
a) Environment
b) State
c) Identifier
d) Scope
Q7. Which of the following is a valid identifier in C?
a) 2total
b) _value
c) @count
d) #sum

Q8. Which of the following is NOT allowed in an identifier in C?


a) Letters
b) Digits (after first character)
c) Underscore
d) Special symbols like @ or #

Q9. Which of the following is case-sensitive?


a) C identifiers
b) SQL keywords
c) HTML tags
d) XML attributes

Q10. The part of a program where a variable can be accessed is called:


a) Identifier
b) Environment
c) Scope
d) State

Q11. A variable declared inside a function is called:


a) Global variable
b) Local variable
c) Static variable
d) Dynamic variable

Q12. A variable declared outside all functions is called:


a) Global variable
b) Local variable
c) Static variable
d) Temporary variable
Q13. A block in programming is usually written using:
a) ( )
b) [ ]
c) { }
d) < >

Q14. A variable declared in an inner block but not in the outer block is accessible:
a) Everywhere in the program
b) Only inside the inner block
c) Only inside the outer block
d) Nowhere

Q15. What happens if you try to access a variable outside its scope?
a) It gives an error
b) It prints garbage value
c) It is automatically global
d) It becomes dynamic

✅ Answers
1. b
2. b
3. b
4. b
5. b
6. b
7. b
8. d
9. a
10. c
11. b
12. a
13. c
14. b
15. a
ANSWERS SHORT QUESTIONS

Section 1.1 – Language Processors


Short Questions with Answers

Q1. What is the role of a language processor?


👉 A language processor converts programs written in high-level languages into machine
language so that the computer can understand and execute them.

Q2. Define a compiler in simple words.


👉 A compiler translates the entire source code into machine code before execution.

Q3. How does an interpreter execute a program?


👉 An interpreter executes the program line by line, translating each statement into machine code
on the spot.

Q4. Write two main differences between compiler and interpreter.


👉 Compiler: translates whole program at once, faster execution.
👉 Interpreter: translates line by line, slower execution.

Q5. Why is Java considered a hybrid language processor?


👉 Because Java first compiles code into bytecode, then uses the Java Virtual Machine (JVM) to
interpret or JIT-compile it.

Q6. What is the purpose of a preprocessor in the language processing system?


👉 A preprocessor handles macros and #include statements before actual compilation starts.

Q7. What is the difference between an assembler and a linker?


👉 Assembler converts assembly code into machine code, while linker combines object files and
libraries into a single executable.
Q8. Arrange the steps of a language processing system in order.
👉 Preprocessor → Compiler → Assembler → Linker → Loader → Execution.

Q9. Why do we need a loader before program execution?


👉 The loader loads the final executable program into memory so that the CPU can run it.

Q10. Give one example language that mostly uses an interpreter.


👉 Python is an example of an interpreted language.

Section 1.2 – The Structure of a Compiler


Short Questions with Answers

Q1. What are the two main parts of a compiler?


👉 Analysis (Front End) and Synthesis (Back End).

Q2. Define analysis phase in a compiler.


👉 The analysis phase breaks the source program into pieces, checks syntax and meaning, and
creates an intermediate representation.

Q3. What is the purpose of the synthesis phase?


👉 The synthesis phase takes the intermediate representation and generates the final target
machine code.

Q4. List the six main phases of a compiler.


👉 Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code
Optimization, and Code Generation.
Q5. What is the role of the lexical analyzer?
👉 It scans the source code character by character and groups them into tokens such as keywords,
identifiers, numbers, and symbols.

Q6. How does the parser (syntax analyzer) help in compilation?


👉 The parser arranges tokens into a parse tree and checks if the program follows the grammar
rules of the language.

Q7. Define semantic analysis with an example.


👉 Semantic analysis checks the meaning of the program, e.g., verifying type compatibility.
Example: preventing assignment of a string to an integer variable.

Q8. What is the purpose of a symbol table?


👉 A symbol table stores information about identifiers (variables, functions, objects) such as their
type and scope.

Q9. Differentiate between a one-pass compiler and a multi-pass compiler.


👉 One-pass compiler: translates the program in a single scan (faster, less accurate).
👉 Multi-pass compiler: scans the program multiple times for accuracy and optimization (used for
C++, Java).

Q10. Name two tools used in compiler construction.


👉 Scanner generators and parser generators.

Section 1.3 – The Evolution of Programming Languages


Short Questions with Answers

Q1. What was the first generation of programming languages?


👉 The first generation was machine language (binary code: 0s and 1s).
Q2. Why was assembly language introduced after machine language?
👉 Because machine code was too difficult for humans; assembly made it easier by using short
symbols (mnemonics) instead of 0s and 1s.

Q3. Give one example of a third-generation language (3GL).


👉 C is an example of a third-generation language.

Q4. What is a fourth-generation language (4GL)? Give one example.


👉 4GLs are languages designed for specific tasks like database queries. Example: SQL.

Q5. Define a fifth-generation language (5GL) in simple words.


👉 5GLs are languages based on logic and constraints, often used in Artificial Intelligence (AI).
Example: Prolog.

Q6. What is the main idea of imperative languages?


👉 Imperative languages focus on how to do a task, step by step.

Q7. How are declarative languages different from imperative languages?


👉 Declarative languages focus on what result is needed, not how to achieve it.

Q8. What does the term object-oriented language mean? Give an example.
👉 Object-oriented languages organize programs using objects (data + functions). Example: Java.

Q9. Why are scripting languages like Python or JavaScript popular today?
👉 Because they are easy to use, portable, and useful for automation, web development, and quick
programming tasks.

Q10. How did the evolution of languages affect compiler design?


👉 Each new language feature (e.g., objects, AI, scripting) required compilers to become more
advanced, efficient, and adaptable
Section 1.4 – The Science of Building a Compiler
Short Questions with Answers

Q1. Why is compiler construction considered a science?


👉 Because it is based on mathematical models, formal rules, and theories that ensure
correctness and efficiency.

Q2. What is the role of finite state machines in a compiler?


👉 Finite State Machines (FSM) are used in lexical analysis to recognize tokens like identifiers,
numbers, and keywords.

Q3. Give one use of regular expressions in lexical analysis.


👉 Regular expressions describe the patterns of tokens. Example: [a-zA-Z][a-zA-Z0-9]* for
identifiers.

Q4. Define a context-free grammar (CFG).


👉 A CFG is a set of rules that describe the syntax structure of a programming language, used in
parsing.

Q5. Why are syntax trees important in compilation?


👉 Syntax trees represent the hierarchical structure of a program, making optimization and code
generation easier.

Q6. What is the purpose of code optimization?


👉 To improve the efficiency of the program by making it faster, smaller, or less memory-
consuming.

Q7. Give an example where optimization makes a program faster.


👉 Example: x = (y * 2) + (y * 2); → Optimized as temp = y * 2; x = temp + temp;.
Q8. List two design goals of compiler construction.
👉 Correctness and efficiency.

Q9. What does portability mean in compiler design?


👉 Portability means the same source code can run on different machines without modification,
thanks to the compiler.

Q10. How is building a compiler similar to building a translator between human


languages?
👉 Both use grammar and context rules to ensure correct translation from one language to
another.

Section 1.5 – Applications of Compiler Technology


Short Questions with Answers

Q1. What is the main application of compiler technology?


👉 To translate high-level programming languages into machine code so computers can execute
them.

Q2. How does a compiler help in the implementation of high-level languages?


👉 It makes languages like C, C++, and Java executable by converting them into machine code.

Q3. Give one way compilers optimize code for modern processors.
👉 By reordering instructions or using parallelism to make execution faster.

Q4. Why is compiler technology important in CPU architecture design?


👉 It allows engineers to test and simulate programs on new processor designs before building
hardware.
Q5. What is binary translation?
👉 Converting machine code from one computer architecture into another, e.g., running Windows
programs on a Mac.

Q6. Define hardware synthesis with an example.


👉 Converting programs into hardware circuits. Example: Using VHDL code to design a
microchip.

Q7. How do compilers help in executing SQL queries?


👉 They compile SQL queries into optimized execution plans for databases.

Q8. Name one software productivity tool that uses compiler techniques.
👉 Type checkers (to detect type errors) or Valgrind (for memory errors).

Q9. How are web browsers related to compiler technology?


👉 They use Just-In-Time (JIT) compilers to run JavaScript efficiently.

Q10. Why are compilers important for mobile app performance?


👉 They optimize apps for different devices, making them faster and more efficient on Android
and iOS.

Section 1.6 – Programming Language Basics


Short Questions with Answers

Q1. What is the difference between static and dynamic features in programming
languages?
👉 Static features are decided at compile-time, while dynamic features are decided at run-time.
Q2. Give one example of something decided statically at compile-time.
👉 The data type of a variable in C (e.g., int x = 5;).

Q3. Give one example of something decided dynamically at run-time.


👉 Variable type in Python (x = 5; x = "Hello";).

Q4. What is an environment in programming language basics?


👉 An environment maps names (identifiers) to their properties, such as variable type and
location.

Q5. Define the term state in relation to program execution.


👉 State refers to the current values stored in variables during execution.

Q6. What is an identifier? Give an example.


👉 An identifier is the name of a variable, function, or object. Example: totalMarks.

Q7. List two rules for writing identifiers in C.


👉 1. Must start with a letter or underscore.
👉 2. Cannot contain special characters like @ or spaces.

Q8. What does the term scope mean in programming?


👉 Scope is the region of a program where a variable can be accessed or used.

Q9. Differentiate between a global variable and a local variable.


👉 Global variable: Declared outside all functions, accessible anywhere.
👉 Local variable: Declared inside a function/block, accessible only there.

Q10. What is a nested block? Give a simple example.


👉 A nested block is a block inside another block. Example:
int main() {
int a = 5;
{
int b = 10; // nested block
}
}

TRUE / FALSE
Section 1.1 – Language Processors
True/False Questions

Q1. A compiler translates the whole program at once into machine code.

Q2. An interpreter produces a separate executable file before running a program.

Q3. Python is an example of an interpreted language.

Q4. A compiler shows all errors only after the full program is compiled.

Q5. An interpreter executes code line by line.

Q6. Java uses only interpretation, not compilation.

Q7. The output of a Java compiler is bytecode.

Q8. A preprocessor is responsible for handling #include and macros.

Q9. An assembler converts assembly code into object (machine) code.

Q10. A linker is used to combine object files and libraries into a single program.

Q11. A loader loads the executable program into memory for execution.

Q12. The language processing system steps are: Compiler → Loader → Linker → Preprocessor
→ Assembler.

Q13. Compilers generally execute faster than interpreters.

Q14. Interpreters are more suitable for quick testing and debugging.

Q15. Without a language processor, computers can directly understand C programs.


✅ Answers
1. True
2. False
3. True
4. True
5. True
6. False
7. True
8. True
9. True
10. True
11. True
12. False
13. True
14. True
15. False

Section 1.2 – The Structure of a Compiler


True/False Questions

Q1. The two main parts of a compiler are called Analysis (Front End) and Synthesis (Back End).

Q2. Lexical analysis is the process of checking grammar rules.

Q3. The parser arranges tokens into a tree structure called a parse tree.

Q4. Semantic analysis ensures that statements make logical sense, such as type checking.

Q5. Intermediate code is the final machine code produced by the compiler.

Q6. Code optimization is used to make the program faster and more efficient.

Q7. The final phase of a compiler is code generation, which produces machine code.

Q8. A symbol table stores information about identifiers, such as type and scope.

Q9. A one-pass compiler translates the entire program using multiple scans.

Q10. A multi-pass compiler is more suitable for large and complex programming languages.
Q11. Grouping compiler phases into a single scan is called passes.

Q12. A scanner generator helps in building lexical analyzers.

Q13. A parser generator is used to build syntax analyzers.

Q14. The back end of a compiler includes lexical analysis and syntax analysis.

Q15. Intermediate code acts as a bridge between the front end and back end of a compiler.

✅ Answers
1. True
2. False
3. True
4. True
5. False
6. True
7. True
8. True
9. False
10. True
11. True
12. True
13. True
14. False
15. True

Section 1.3 – The Evolution of Programming Languages


True/False Questions

Q1. The first generation of programming languages was machine language.

Q2. Assembly language uses binary digits (0s and 1s) directly for instructions.

Q3. High-level languages are closer to human language than assembly or machine code.

Q4. FORTRAN was designed mainly for business data processing.


Q5. COBOL was developed for scientific and mathematical calculations.

Q6. LISP was one of the early languages used for Artificial Intelligence.

Q7. Third-generation languages (3GL) include C, C++, and Java.

Q8. SQL is an example of a fourth-generation language (4GL).

Q9. Prolog is an example of a fifth-generation language (5GL).

Q10. Imperative languages describe what result is needed, not how to achieve it.

Q11. Declarative languages describe what result is needed, not how to achieve it.

Q12. Object-oriented languages combine data and functions into objects.

Q13. Scripting languages such as Python and JavaScript are often used for automation and web
development.

Q14. Evolution of programming languages had no effect on compiler design.

Q15. Each new programming paradigm required compilers to adapt and evolve.

✅ Answers
1. True
2. False
3. True
4. False
5. False
6. True
7. True
8. True
9. True
10. False
11. True
12. True
13. True
14. False
15. True
Section 1.4 – The Science of Building a Compiler
True/False Questions

Q1. Compiler construction is based on formal methods and mathematical models.

Q2. Finite State Machines (FSM) are mainly used in syntax analysis.

Q3. Regular expressions are used to define tokens in lexical analysis.

Q4. Context-Free Grammars (CFG) are used for parsing in syntax analysis.

Q5. Syntax trees represent the hierarchical structure of programs.

Q6. A syntax tree for a + b * c has * as the root operator.

Q7. Code optimization improves program efficiency.

Q8. Data-flow analysis and graph theory are used in compiler optimization.

Q9. The main design goal of a compiler is to make programs incorrect but faster.

Q10. Correctness is one of the important goals in compiler design.

Q11. Portability in compiler design means the same program can run on different machines.

Q12. Maintainability is not a concern in compiler design.

Q13. Syntax analysis depends on formal grammar rules.

Q14. Building a compiler is similar to language translation between humans.

Q15. Scalability means the compiler should handle large programs and complex languages.

✅ Answers
1. True
2. False
3. True
4. True
5. True
6. False
7. True
8. True
9. False
10. True
11. True
12. False
13. True
14. True
15. True

Section 1.5 – Applications of Compiler Technology


True/False Questions

Q1. The only use of compilers is to translate high-level languages into machine code.

Q2. Compilers also play a role in optimizing programs for modern processors.

Q3. Binary translation converts one machine’s code into another machine’s code.

Q4. Hardware synthesis means converting hardware circuits into C programs.

Q5. VHDL and Verilog are languages used in hardware synthesis.

Q6. Database systems use compiler techniques to process and optimize SQL queries.

Q7. Valgrind is a tool that uses compiler technology to check memory errors.

Q8. Just-In-Time (JIT) compilation is used in JavaScript engines of web browsers.

Q9. Compiler technology is not important in CPU design.

Q10. Android uses a special compiler (Dalvik/ART) to run apps efficiently.

Q11. Code optimization is only used in scientific computing, not in games.

Q12. Games benefit from compiler technology for faster graphics and performance.

Q13. Linkers and loaders are examples of compiler-related tools.

Q14. Productivity tools like type checkers rely on compiler concepts.

Q15. Compiler techniques are widely used beyond programming languages.


✅ Answers
1. False
2. True
3. True
4. False
5. True
6. True
7. True
8. True
9. False
10. True
11. False
12. True
13. True
14. True
15. True

Section 1.6 – Programming Language Basics


True/False Questions

Q1. Static features of a language are decided at compile-time.

Q2. Dynamic features of a language are decided at run-time.

Q3. In C, variable types are determined dynamically.

Q4. In Python, variables can change type during execution.

Q5. An environment maps identifiers to their properties.

Q6. The state of a program refers to the current values of variables.

Q7. An identifier can begin with a number in C.


Q8. Identifiers in C are case-sensitive.

Q9. The scope of a variable defines where it can be accessed.

Q10. A local variable is declared outside all functions.

Q11. A global variable is accessible throughout the program.

Q12. Nested blocks are not allowed in C.

Q13. Curly braces { } are used to define blocks in C.

Q14. A variable declared inside an inner block can be accessed outside that block.

Q15. Accessing a variable outside its scope causes an error.

✅ Answers
1. True
2. True
3. False
4. True
5. True
6. True
7. False
8. True
9. True
10. False
11. True
12. False
13. True
14. False
15. True

Fill in the Blanks

Section 1.1 – Language Processors


Fill in the Blanks
1. A _______ translates high-level source code into machine code.
2. An _______ executes programs line by line.
3. Python is an example of an _______ language.
4. A _______ handles macros and #include before compilation.
5. The output of a compiler is usually an _______ file.
6. An _______ converts assembly language into machine code.
7. A _______ combines object files into a single executable.
8. A _______ loads the executable into memory.
9. Java first compiles code into _______.
10. The sequence of language processors is Preprocessor → Compiler → _______ → Linker
→ Loader.

✅ Answers:

1. Compiler
2. Interpreter
3. Interpreted
4. Preprocessor
5. Object
6. Assembler
7. Linker
8. Loader
9. Bytecode
10. Assembler

Section 1.2 – The Structure of a Compiler


Fill in the Blanks

1. The two main parts of a compiler are _______ and _______.


2. Lexical analysis divides the program into _______.
3. A parser generates a _______ tree.
4. Semantic analysis checks the _______ of the program.
5. _______ code is generated as a bridge between front end and back end.
6. The phase responsible for improving performance is _______.
7. The final phase of the compiler is _______ generation.
8. A _______ table stores information about identifiers.
9. A compiler that scans code once is called a _______ compiler.
10. Parser generators are tools used to build _______.

✅ Answers:

1. Analysis, Synthesis
2. Tokens
3. Parse
4. Meaning
5. Intermediate
6. Code optimization
7. Code
8. Symbol
9. One-pass
10. Syntax analyzers

Section 1.3 – The Evolution of Programming Languages


Fill in the Blanks

1. The first generation of programming languages was _______ language.


2. Assembly language uses symbolic names called _______.
3. C is an example of a _______ generation language.
4. SQL is a _______ generation language.
5. Prolog is a _______ generation language.
6. Imperative languages describe _______ to perform tasks.
7. Declarative languages describe _______ is needed, not how.
8. Java is an example of an _______-oriented language.
9. Python and JavaScript are examples of _______ languages.
10. Each new language generation requires compilers to _______.

✅ Answers:

1. Machine
2. Mnemonics
3. Third
4. Fourth
5. Fifth
6. How
7. What
8. Object
9. Scripting
10. Adapt

Section 1.4 – The Science of Building a Compiler


Fill in the Blanks
1. Compiler construction is based on _______ methods.
2. Finite State Machines are used in _______ analysis.
3. Regular expressions describe patterns of _______.
4. Context-free grammar is used for _______ analysis.
5. Syntax trees represent the _______ of a program.
6. Code _______ improves program efficiency.
7. Data-flow analysis is useful in _______ optimization.
8. A compiler’s main goals are correctness and _______.
9. Portability means running the same program on different _______.
10. Building a compiler is similar to building a language _______.

✅ Answers:

1. Formal
2. Lexical
3. Tokens
4. Syntax
5. Structure
6. Optimization
7. Code
8. Efficiency
9. Machines
10. Translator

Section 1.5 – Applications of Compiler Technology


Fill in the Blanks

1. Compilers are used to implement _______-level programming languages.


2. Code _______ makes programs faster and smaller.
3. Binary _______ converts machine code of one system to another.
4. Hardware _______ converts programs into circuits.
5. _______ is a language used in hardware design.
6. SQL queries are processed using compiler _______.
7. Valgrind is a tool for _______ error checking.
8. JavaScript engines use _______ compilation.
9. Android uses the _______ compiler for apps.
10. Games rely on compiler technology for better _______ performance.

✅ Answers:

1. High
2. Optimization
3. Translation
4. Synthesis
5. VHDL
6. Techniques
7. Memory
8. JIT
9. Dalvik/ART
10. Graphics

Section 1.6 – Programming Language Basics


Fill in the Blanks

1. Static features are decided at _______-time.


2. Dynamic features are decided at _______-time.
3. In C, variable types are decided at _______.
4. In Python, variables are typed _______.
5. An _______ maps identifiers to properties.
6. The _______ of a program is the set of current values of variables.
7. An identifier cannot start with a _______ in C.
8. Identifiers in C are _______-sensitive.
9. The _______ of a variable defines where it can be accessed.
10. Variables inside a nested block are accessible only _______ that block.

✅ Answers:

1. Compile
2. Run
3. Compile-time
4. Dynamically
5. Environment
6. State
7. Number
8. Case
9. Scope
10. Inside

Assignments
Assignments – Lecture 1: Introduction to Compilers
Assignment 1: Difference Between Compiler and Interpreter

Write a short note (150–200 words) explaining the differences between a compiler and an
interpreter.

 Give at least 3 differences.


 Mention one language example for each.

Assignment 2: Draw the Structure of a Compiler

Draw a neat diagram of the structure of a compiler showing:

 Lexical Analysis
 Syntax Analysis
 Semantic Analysis
 Intermediate Code Generation
 Code Optimization
 Code Generation

Label each phase with its function in one short sentence.

Assignment 3: Evolution of Languages

Make a timeline chart of programming languages from Machine Language → Assembly →


3GL → 4GL → 5GL.

 Mention one example language in each generation.


 Write one strength and one weakness of each generation.

Assignment 4: Compiler Applications

Write down five real-life applications of compiler technology (outside of simple translation).

 Example: CPU design, database query execution, JIT compilers in browsers, etc.
 Explain each in 2–3 lines.
Assignment 5: Programming Language Basics Practice

Write a small C program:

#include <stdio.h>
int main() {
int x = 5; // global scope variable
{
int y = 10; // nested block variable
printf("%d %d", x, y);
}
return 0;
}

Tasks:

1. Identify the identifiers.


2. State the scope of each variable.
3. Explain which parts are static and which are dynamic.

You might also like