Compiler Construction Notes
Compiler Construction Notes
2. Purpose of a Compiler
Source Program
4. Phases of Compilation
• Purpose: Break the source code into tokens (keywords, identifiers, operators, etc.).
• Output: Stream of tokens for syntax analysis.
• Purpose: Convert the syntax tree into an intermediate representation (IR) for further
processing.
• Example IR:LOAD a ADD to SQPT_88
LOAD b NSA b
LOAD c ADD b SQPT_88
NULL b DIV AB, 2A
NULL a, c STORE root 1
NULL C, AC NSA b
SUB RB, AC SUB b SQPT_88
CALL SQPT, BR DIV AB, 2A
• Purpose: Link the compiled code with external libraries and create the final
executable file.
• Example:
o Input Coefficients: a=1, b=-3, c=2
o Output: Roots are real and different: 2.0000 | 1.0000
Advantages
Disadvantages
Example Output:
Code Optimization
Semantic Analysis
• Check Logic and Operations: Ensure the code makes logical sense.
• Type Checking: Ensure variables and operations are of the correct type.
• Division by Zero: Prevent division by zero errors.
• Function Calls: Ensure functions receive valid arguments.