Compiler Design and Construction · Unit 1 · 3 hrs
Compiler Structure and Basic Concepts
Exam-focused notes for Compiler Structure and Basic Concepts (Compiler Design and Construction, CSC376): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Compiler Structure and Basic Concepts in short
This chapter introduces the compiler as a phased translator, showing how a source program is pulled apart in analysis and rebuilt in synthesis, and it fixes the vocabulary (interpreter, pass, preprocessor, macro, symbol table, error handler) used throughout the rest of the subject.
What this unit covers
- Compiler Structure: Analysis and Synthesis Model of Compilation
- different sub-phases within analysis and synthesis phases
- Basic concepts related to Compiler such as interpreter, simple One-Pass Compiler, preprocessor, macros, symbol table and error handler
different sub-phases within analysis and synthesis phases
2081 · 5 marks
Explain different phases of compiler in brief. [5]
Show solved answerHide answer
Phases of a Compiler
A compiler operates in phases, where each phase takes a program in one representation and produces output in another representation. There are two major phases of compilation:
1. Analysis Phase (Front End)
In the analysis part, an intermediate representation is created from the given source program. It consists of the following phases:
i) Lexical Analysis (Scanning)
- The source program is read left-to-right and grouped into tokens.
- Tokens are sequences of characters with a collective meaning (e.g., constants, operators, reserved words).
- Input: Source program
- Output: Stream of tokens
ii) Syntax Analysis (Parsing)
- The tokens produced by the lexical analyzer are grouped into grammatical phrases using the grammar of the programming language.
- A parse tree or syntax tree is produced to show the hierarchical structure of the token stream.
- Input: Stream of tokens
- Output: Parse tree / Syntax tree
iii) Semantic Analysis
- Checks whether the parse tree follows the rules of the language (e.g., type checking, scope resolution).
- Ensures that the program is semantically correct.
- Input: Parse tree
- Output: Annotated parse tree
iv) Intermediate Code Generation
- An intermediate representation (e.g., three-address code) of the source program is generated.
- This code is easy to produce and easy to translate into target code.
- Input: Annotated parse tree
- Output: Intermediate code
2. Synthesis Phase (Back End)
In the synthesis part, the equivalent target program is created from the intermediate representation. It consists of the following phases:
i) Code Optimization
- Improves the intermediate code so that the output program runs faster and takes less space.
- Removes unnecessary lines of code and rearranges statements to speed up execution.
- Example:
t2 = id3 * 3.0;
id1 = id2 + t2;
ii) Code Generation
- The final phase that translates the optimized intermediate code into target machine code (or assembly code).
- Memory locations are assigned to variables and intermediate results.
- Input: Optimized intermediate code
- Output: Target machine code
Supporting Component: Symbol Table
Throughout all phases, the symbol table is used. It is a data structure that holds information about source-program constructs (identifiers), such as:
- Type
- Position in storage
- Other relevant information
The analysis phase collects information into the symbol table, and the synthesis phase uses it to generate target code.
Summary Diagram
Source Program
|
Lexical Analysis |
| | Analysis
Syntax Analysis | Phase
| | (Front End)
Semantic Analysis |
|
Intermediate Code Gen |
| |
Code Optimization | Synthesis
| | Phase
Code Generation | (Back End)
|
Target Program
Basic concepts related to Compiler such as interpreter, simple One-Pass Compiler, preprocessor, macros, symbol table and error handler
2078 · 5 marks
Define compiler and differentiate it with an interpreter. [5]
2076 · 6 marks
Discuss the importance of error handler in compiler. How is it manipulated in the different phases of compilation?[6]
2075 · 6 marks
Difference between compiler and interpreter. "Symbol table is necessary component of compiler". Justify this statement with examples.[6]
2081.1 · 5 marks
What are the roles of macros and preprocessor? Discuss about one pass and multi pass compiler. [5]
Show solved answerHide answer
Roles of Macros and Preprocessor; One Pass vs Multi-Pass Compiler
1. Roles of Macros and Preprocessor
A preprocessor is a tool that processes the source code before it is passed to the compiler. It is generally considered a part of the compiler system and produces input for the compiler proper.
Key Functions of a Preprocessor:
i) Macro Processing: A preprocessor allows a user to define macros, which are shorthands for longer constructs. When the preprocessor encounters a macro call, it replaces it with the corresponding expanded code before compilation begins.
- Example:
#define MAX 100replaces every occurrence ofMAXwith100.
ii) File Inclusion: The preprocessor handles header file inclusion by inserting the content of specified files into the program text.
- Example:
#include <stdio.h>inserts the standard I/O header file.
iii) Rational Preprocessor: These preprocessors augment older languages with more modern flow-of-control and data structuring facilities, making legacy code more manageable.
iv) Language Extensions: These preprocessors attempt to add capabilities to the language by building in macro-like constructs, effectively extending what the base language can express.
2. One Pass vs Multi-Pass Compiler
One Pass Compiler:
A compiler in which all phases are combined and executed in a single pass over the source program.
Multi-Pass Compiler:
A compiler in which different phases are grouped into multiple passes, each pass reading the output of the previous one.
Comparison Table:
| Feature | One Pass Compiler | Multi-Pass Compiler |
|---|---|---|
| Structure | All phases combined into one single pass | Different phases grouped into multiple passes |
| Intermediate Representation | Not created | Created between passes |
| Speed | Faster | Slightly slower |
| Also Called | Narrow compiler | Wide compiler |
| Memory Usage | Requires less memory | Requires more memory |
| Example | Pascal compiler | C++ compiler |
Summary:
- A one pass compiler is simpler and faster but less flexible, as it cannot easily handle forward references.
- A multi-pass compiler is more powerful and can perform better optimization since it has a complete view of the program through intermediate representations.
Compiler Structure
2076 · 6 marks
Explain briefly about different phases involved in compiler, with a block diagram[6]
Make Unit 1 stick
Practice CSC376 with flashcards & quizzes