Title: Compiler Chap'1 Introduction to Compiler
1CompilerChap.1 Introduction to Compiler
- Internet Management Technology Lab.
2Contents
- Introduction
- Conceptual view of the compilation process
- The phases of a compiler
3Introduction
- Definition of compiler
- A program which accepts phrases written in an
input language (source language) and produces
equivalent phrases in the desired output language
(target language) - e.g. assembler assembly ? machine language
- Characteristics of compiler
- Directed by a simple understanding of the phrase
structure (grammar) of an input language - Usually contains complex mapping algorithms
- Generates a target code for efficiency while an
interpreter does not generate a target code for
flexibility
4Conceptual View of Compilation Process
ltA language-processing systemgt
5The phases of a compiler Architecture
ltAnalysis of the source programgt
6The phases of a compiler Lexical Analysis
- Lexical Analysis Phase
- Reads in the characters from the source program
- Groups the characters into grammatically
significant chunks called lexemes or tokens - A token is a logically cohesive sequence of
characters - keyword, constant, operator,
identifier, etc. - Outputs the tokens to the syntax analysis phase
- Performed by a lexer (or scanner) and the
efficiency is one of the important issues - Example Find tokens from the following example
if( a lt 3.5 ) Â Â Â Â Â Â Â bottom a else
       bottom
7The phases of a compiler Syntax Anaysis
- Syntax Analysis phase
- Imposes a hierarchical structure on the token
stream by the language syntax - Optionally builds an Abstract Syntax Tree (AST)
- AST represents the structure of the source
program - AST is one of the Intermediate representation
form - Performed by a parser
8The phases of a compiler Intermediate Rep.
9The phases of a compiler Semantic Analysis
- Semantic Analysis Phase
- Checks the meaning of the constructs
- Most of the semantic analysis is done by a parser
- Some could be done by lexer or code generator
- Parser and part of code generator performing
semantic analysis is called constrainer by
pittman - Type checking, uniqueness
- Int a, real b
- a b
10The phases of a compiler Optimization
- Code optimization Phase
- Optional phase for performance enhancement such
as - Reducing execution time
- Saving memory space
- Reducing communication bandwidth
- Performed by a code optimizer
for(j 0 j lt MAX j) Â Â m 24 Â Â
sum sum xj m could be rewritten as
m 24 for(j 0 j lt MAX j) Â Â sum
xj m
11The phases of a compiler Code Generation
- Code Generation Phase
- Translates intermediate representation (or output
from the parser) into a target code - Performed by a code generator
- e.g.
- a a 1
- Generates the following assembly-like pseudo code
LD Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â R1, a ADDÂ Â Â Â Â Â Â Â Â Â Â Â Â Â R1,
R1, 1 STÂ Â Â Â Â Â Â Â Â a, R1
12Homework I
- Compare compiler and interpreter with regard to
the space complexity and the time complexity,
then discuss the applications areas where using
interpreters are better than compilers. - How the compiler architecture presented in class
could be modified for better flexibilities? - How each 4 modules in the architecture
contributes to the syntax analysis and semantic
analysis respectively? - What are the advantages of having the phases such
as lexical analysis phase, syntax analysis phase,
and so on be separately modularized ?