Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Jul 16, 2024

Compiler Design Notes

Author Sanjana kumari
Table of contents
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
23 Jul, 2024 @ 01:30 PM
Prerita Agarwal
Data Specialist @

Compiler Design is the process of creating a software tool that translates high-level programming languages into machine code. It involves lexical analysis, syntax parsing, semantic checks, optimization, and code generation.

og image

What is a Compiler?

A compiler is a software program which converts code written in a high-level programming language into machine code that a computer can run. It is responsible for improving the code. It will ensure that your code is effective and performs as you expect. Whereas an interpreter directly executes code written in a high-level programming language. It does not need compilation to machine code. It will read and run code line by line.

The compilation process has many phases. After analysis, the compiler will generate code. The will be in the form of an object file. It also performs many improvements to enhance the output of your generated code. You can run the compiler output directly on the target machine, or it may need to be connected with other libraries available.

what is a compiler


Types of Compilers

There are two types of compilers

  1. Single-pass compilers
  2. Two-pass (or multipass) compilers 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

1. Single-pass compilers

They read the source code only once from beginning to end. It translates source code into object code in one pass. They are simple and fast. They still need to be more effective in error-checking and giving better code.

Single-pass compilers

2. Two-pass (or multipass) compilers 

They read the source code twice or more. They analyze it in one pass and give the output in the next. They are more advanced than single-pass compilers. They provide better error messages and improved code. They are slower and more complex.

two pass compiler

Compilation Process

The compilation process involves converting source code written in a high-level programming language into machine code that a computer runs. The process has many phases and tools.

compilation process

Here's a summary of the compilation process:

  • Preprocessing: The preprocessor will process the source code. It will carry out macro expansion and conditional compilation operations. In macro expansion, the preprocessor will substitute macro names with their values, perform argument substitution if required and recursively expand nested macros. In this, the preprocessor will create some reusable code snippets and improve the code's readability.
  • Compilation: The compiler will convert the preprocessed code into assembly or machine code.
  • Assembly: The assembler will convert the assembly language into object files. The object file has machine code and data.
  • Linking: The linker will link object files and libraries together to make an executable file. The file will run on the target machine.

The compiler will also check the code for errors. It will create specific warnings if it detects any problems.

Parsing in Compiler Design

Parsing analyses a string of symbols according to a set of rules. A string of symbols is a sequence of characters that will be parsed according to a particular grammar or syntax. It transforms the string into a framework. This framework is an abstract syntax tree (AST). Mainly it is used to analyze source code. It gives an AST. AST is used for type checking or code generation. The choice of parsing algorithm depends on the type of grammar you are using, the input language's complexity, and the parser's performance needs.
Parsing has two types.

  • Top-Down: It is a recursive descent way that starts with the start symbol of a grammar. The grammar will be divided into sub-problems. The parser will recursively call itself to match the input against each sub-problem.
  • Bottom-UpIt starts with the input. It builds up the parse tree by combining symbols according to the grammar rules. It can handle left-recursive rules in grammar.

The next topic of discussion in our compiler design notes is the phases of the compilation process.

Phases of Compilation Process

The phases of the compilation process can vary depending on the specific compiler and programming language you are using. It mainly includes: 

phases of compilation process

1. Lexical Analysis

It involves dividing the source code into individual tokens, such as keywords, identifiers, operators, and literals.

2. Syntax Analysis

It involves arranging the tokens into a nested structure to check for proper syntax and follow programming language rules. It gives an abstract syntax tree (AST).

3. Semantic Analysis

It involves checking the AST for semantic correctness. It includes type-checking and name resolution. This phase ensures that the code makes sense and can work properly.

4. Intermediate Code Generation

It involves converting the code into an intermediate representation so that further improvements can occur.

5. Optimization

The intermediate code is made better to increase its performance. It involves reducing the number of instructions or minimizing memory accesses.

6. Code Generation

The improved intermediate code is converted into a runnable machine or bytecode. It involves taking the optimized intermediate code and converting it into low-level machine language which can run on the target hardware. The code generation phase first has instruction selection, then register allocation and finally, schedules the instructions. The output from this phase is an executable program that can perform the operations specified in the source code.

Linking is optional. The compiled code may be linked to other libraries. Let’s move to the optimization ways in our compiler design notes.

Optimization in Compiler Design

We aim to give a code that runs faster or uses less memory. Optimization is the process of improving the productivity of code that is compiled. You can have many optimization ways in compiler design.

  • Local optimization techniques: They work on small blocks of code. It includes constant folding and subexpression removal. Constant folding makes the constant expressions simple during compile-time by calculating their values instead of calculating them at runtime. Subexpression removal involves first identifying and then eliminating common subexpressions within an expression.
  • Global optimization techniques: They work on the entire program. It includes loop optimization and register allocation. Register allocation involves deciding which variables we will store in processor registers. The CPU uses the small and fast storage locations inside the processor to perform data calculations.
  • Peephole optimization:  It is a way that targets specific series of instructions. We convert repeated arithmetic operations into better operations. We replace multiple instructions with a single instruction that gives the same result.
  • Data flow analysis: You can use it to analyze the behaviour of variables in a program. We gather information on how data flows through a computer program. We can also use it to track the way values of variables change over time as a program runs.
  • Control flow analysis: You can use it to identify branches and loops in a program. We determine the control flow of a program. We outline the path a program is taking as it is running.

We generally make errors. In our compiler design notes, we will be heading towards the common mistake one should avoid. 

Common Mistakes in Compiler Design

These are some common mistakes you must avoid during the compiler design:

  • It would be best if you did not implement basic syntax checking. It will give you errors that are difficult to detect and debug.
  • Be aware of the importance of generating efficient machine code. It can make your programs slower.
  • Handle all expected input cases. Otherwise, your programs can crash or behave unexpectedly. 
  • If you pay attention to the complexities of the target hardware and operating system, your program will perform well.
  • Please refrain from assuming that the input language is easy to parse when it may be very complex.
  • Avoid misunderstanding the input language's semantics, which can give incorrect translations.
  • It would help if you had proper error reporting. Otherwise, it will be difficult to understand the source of errors in the code.
  • Keep updating the compiler regularly to keep up with ongoing environmental changes.

Tools for Building and Testing Compiler

There are resources present for building and testing a compiler:

  1. Lex and Yacc: These popular parser generator tools can help you create a parser and scanner for your compiler.
  2. LLVM: It is a widespread open-source compiler infrastructure that gives many tools for building and optimizing your code.
  3. Testing frameworks: There are many testing frameworks available that can help you test your compiler. JUnit, NUnit, and pytest are good examples.
  4. Integration tools: You can use tools like Jenkins, Travis CI, or GoCD to automatically build and test your compiler as code changes are made.
  5. IDEs: Integrated development environments like IntelliJ IDEA, Visual Studio, Eclipse, and Xcode provide valuable features for building and debugging compilers.
  6. Online compilers: Several online compilers like, Ideone, and JDoodle give an online compiler environment for testing and running code.

Applications of Compiler Design

It is used in implementing high-level programming languages. It helps in optimizations for computer architectures. We can design and evaluate new computer architectures. Also used in the performance tuning of software. It is used in industrial automation, control systems, Embedded systems and IoT devices. Also, have a significant role in video game development and Web development and optimization.

The next part of our compiler design notes discusses the compiler and interpreter.

Difference between Compiler And Interpreter

DefinitionTranslates entire source code into machine code before execution.Translates source code line-by-line during execution.
Execution SpeedGenerally, faster execution time as code is already compiled.Slower execution time as each line is translated and executed sequentially.
Error DetectionErrors are detected and rethe ported after compilation of the entire code.Errors are detected and reported line-by-line during execution.
OutputGenerates an executable file (machine code).Does not generate an intermediate machine code file; executes directly.
Memory UsageRequires more memory to store the compiled code.Typically uses less memory as it doesn't store compiled code.
RecompilationNeeds recompilation if source code is changed.No need for recompilation; directly interprets changes in the source code.
DevelopmentMore suitable for production environments where performance is critical.More suitable for development and debugging due to immediate error feedback.
ExamplesC, C++, JavaPython, JavaScript, Ruby

Frequently Asked Questions

What are the compiler design tools?

Compiler design tools include Lex and Yacc (or Flex and Bison), ANTLR, LLVM, GCC, and IDEs like Visual Studio and Eclipse.

What are the main phases of compiler design?

The main phases of compiler design include lexical analysis, syntax analysis, semantic analysis, intermediate code generation, optimization, code generation, and code linking and assembly.

What are the three main parts of a compiler?

The three main parts of a compiler are the frontend (handling lexical, syntax, and semantic analysis), the middle-end (performing optimizations), and the backend (responsible for code generation and assembly).

What are the three functions of a compiler?

  1. Translation: Converts high-level programming code into machine code or intermediate representations.
  2. Optimization: Enhances the efficiency of the generated code.
  3. Code Generation: Produces executable machine code.


This article discusses compiler design applications, types, and phases. It explains how to avoid errors and optimize your compiler design. The article also includes flowcharts to help understand compiler design in a better manner. Refer to other compiler articles to improve your understanding of the compiler design.

Refer to our guided paths on Code360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses, refer to the mock test and problems look at the interview experiences and interview bundle for placement preparations.

Next article
Top Down and Bottom Up Parsing