23381 Compd+Chapter+1
-
Upload
adriel-santos -
Category
Documents
-
view
219 -
download
0
Transcript of 23381 Compd+Chapter+1
-
7/31/2019 23381 Compd+Chapter+1
1/18
CHAPTER 1
-
7/31/2019 23381 Compd+Chapter+1
2/18
COMPILER
A compileris a program that reads a programwritten in one language the source language
and translates it into an equivalent program in
another language the target language
COMPILERSOURCE
PROGRAM
ERROR
MESSAGES
TARGET
PROGRAM
-
7/31/2019 23381 Compd+Chapter+1
3/18
A VERY Brief Compiler History
1950s first generation of compilers; much of theearly work on compiling dealt with the translation
of arithmetic formulas into machine code.
-
7/31/2019 23381 Compd+Chapter+1
4/18
PARTS OF COMPILATION
There are two parts to compilation:1. Analysis breaks up the source program into
constituent pieces and creates an intermediate
representation of the source program
2. Synthesis constructs the desired targetprogram from the intermediate representation
-
7/31/2019 23381 Compd+Chapter+1
5/18
SOFTWARE TOOLS THAT MANIPULATE
SOURCE PROGRAMS
1. Structure Editors takes as input a sequence ofcommands to build a source program
2. Pretty Printers Analyzes a program and prints it in
such a way that the structure of the program
becomes clearly visible.3. Static Checker a static checker reads a program,
analyzes it, and attempts to discover potential bugs
without running the program.
4. Interpreters instead of producing a target programas a translation, an interpreter performs the
operations implied by the source program.
-
7/31/2019 23381 Compd+Chapter+1
6/18
PREPROCESSOR
Several programs may be required to create anexecutable target program. A source program
may be divided into modules stored in separate
files.
The task of collecting the source program is
entrusted to the PREPROCESSOR.
The preprocessor may also expand shorthands or
macros into source language statements
-
7/31/2019 23381 Compd+Chapter+1
7/18
ANALYSIS OF THE SOURCE
PROGRAM
THREE PHASES OF ANALYSIS1. Linear Analysis the stream of characters making
up the source program is read from left-to-right
and grouped into tokens
2. Hierarchical Analysis characters or tokens aregrouped hierarchically into nested collections with
collective meaning
3. Semantic Analysis certain checks are performed
to ensure that the components of a program fittogether meaningfully
-
7/31/2019 23381 Compd+Chapter+1
8/18
Lexical Analysis In a compiler, linear analysis is called lexicalanalysisorscanning.
For example, in lexical analysis the characters in theassignment statement
position := initial + rate * 60
would be grouped into the following tokens:1. The identifierposition2. The assignment symbol :=3. The identifierinitial4. The plus sign
5. The identifierrate6. The multiplication sign7. The number60
-
7/31/2019 23381 Compd+Chapter+1
9/18
Syntax Analysis
Hierarchical Analysis is called parsingorsyntaxanalysis. It involves grouping the tokens of thesource of the program into grammatical phrases
that are used by the compiler to synthesize
output.
-
7/31/2019 23381 Compd+Chapter+1
10/18
Parse Tree
-
7/31/2019 23381 Compd+Chapter+1
11/18
Semantic Analysis
The semantic analysis phase checks the sourceprogram for semantic errors and gathers type
information for the subsequent code generation
phase.
It uses the hierarchical structure determined by
the syntax analysis phase to identify the
operators and operands of expressions and
statements
-
7/31/2019 23381 Compd+Chapter+1
12/18
Phases of a Compiler
-
7/31/2019 23381 Compd+Chapter+1
13/18
Symbol Table Management
An essential function of a compiler is to recordthe identifiers used in the source program and
collect various attributes of each identifier
The attributes may provide information about the
storage allocated for the identifier
Type
Scope
Number and type of arguments
Type returned
-
7/31/2019 23381 Compd+Chapter+1
14/18
Symbol Table
A symbol table is a data structure containing arecord for each identifier with fields for the
attributes of the identifier
When an identifier in the source program is
detected by the lexical analyzer, the identifier is
entered into the symbol table.
-
7/31/2019 23381 Compd+Chapter+1
15/18
Error Detection and Reporting
Each phase can encounter errors. However, afterdetecting an error, a phase must somehow deal
with that error, so that compilation may proceed,
allowing further errors in the source program to
be detected.
-
7/31/2019 23381 Compd+Chapter+1
16/18
The Analysis Phases
-
7/31/2019 23381 Compd+Chapter+1
17/18
Intermediate Code Generation
-
7/31/2019 23381 Compd+Chapter+1
18/18
Code Optimization and Code
Generator