1 Compiler Structure And Basic Concepts

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.

Compiler Structure and Basic Concepts: notes

Compiler Structure: Analysis and Synthesis Model of Compilation

A compiler is a translator program that reads a program written in a source language (typically a high level language) and produces an equivalent program in a target language (typically machine or assembly language), while reporting any errors it finds in the source program. Compilation is organized as a sequence of phases, each of which takes a program in one representation and produces a program in another representation. Grouped at a coarser grain, every compiler has exactly two halves.

The analysis phase, also called the front end, breaks the source program into pieces and imposes a grammatical structure on it. It then uses this structure to create an intermediate representation of the source program. If the analysis phase detects that the source program is either syntactically ill formed or semantically unsound, it must provide an informative message so the user can correct the error. The analysis phase also collects information about the source program and stores it in a symbol table, which is passed along with the intermediate representation to the synthesis phase.

The synthesis phase, also called the back end, constructs the desired target program from the intermediate representation and the information in the symbol table. Because the analysis phase depends only on the source language and the synthesis phase depends only on the target machine, this two part split is what lets a single front end be retargeted to several machines (by swapping the back end) and lets several source languages share a back end (by swapping the front end). Analysis is therefore often called machine independent, and synthesis machine dependent.

Both halves communicate through the intermediate representation and through the symbol table, and both are shadowed throughout compilation by two supporting components that run in every phase rather than in one phase alone: the symbol table manager, which records every identifier along with its attributes (type, scope, storage location), and the error handler, which is invoked whenever a phase detects a fault it cannot silently repair.

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 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
Open this answer on its own page →

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]

A compiler is a translator software program that takes its input in the form of a program written in a particular (high-level) programming language and produces output in the form of a program in another language (typically machine language or object code). In other words, a compiler converts the...

Open this answer on its own page →

2076 · 6 marks

Discuss the importance of error handler in compiler. How is it manipulated in the different phases of compilation?[6]

During the compilation of a source program, errors can be encountered at any phase of compilation. An error handler is a component that is invoked whenever such an error is detected. Its importance can be summarized as follows: - It detects and reports errors with suitable, meaningful error messa...

Open this answer on its own page →

2075 · 6 marks

Difference between compiler and interpreter. "Symbol table is necessary component of compiler". Justify this statement with examples.[6]

--- Basis Compiler Interpreter --------- Translation Translates the entire source program at once into machine code Translates and executes the source program line by line Speed Faster execution after compilation Slower execution due to line-by-line processing Error Detection Reports all errors a...

Open this answer on its own page →

2081.1 · 5 marks

What are the roles of macros and preprocessor? Discuss about one pass and multi pass compiler. [5]

Show solved 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 100 replaces every occurrence of MAX with 100.

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:

FeatureOne Pass CompilerMulti-Pass Compiler
StructureAll phases combined into one single passDifferent phases grouped into multiple passes
Intermediate RepresentationNot createdCreated between passes
SpeedFasterSlightly slower
Also CalledNarrow compilerWide compiler
Memory UsageRequires less memoryRequires more memory
ExamplePascal compilerC++ 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.
Open this answer on its own page →

Compiler Structure

2076 · 6 marks

Explain briefly about different phases involved in compiler, with a block diagram[6]

A compiler translates a source program written in a high-level language into an equivalent target program (usually machine code). This translation is carried out through a series of well-defined phases, grouped into two major parts: the Analysis phase (Front End) and the Synthesis phase (Back End...

Open this answer on its own page →