0% found this document useful (0 votes)
70 views27 pages

Pert 1 - Introduction To Compiler

The document provides an introduction to compiler techniques, including: 1. It explains the basic concepts of compilation including the functions of a compiler to translate a program from a source language to a target language, and the main stages of compilation: analysis, synthesis, and symbol table management. 2. It outlines the content to be covered such as the evolution of programming languages, understanding compilation, compiler classification, the language processing system, and the roles of related tools like the preprocessor, assembler, and linker. 3. It describes the analysis phase which consists of lexical analysis, syntax analysis, and semantic analysis to check the program components fit together meaningfully.

Uploaded by

Ardo Legawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views27 pages

Pert 1 - Introduction To Compiler

The document provides an introduction to compiler techniques, including: 1. It explains the basic concepts of compilation including the functions of a compiler to translate a program from a source language to a target language, and the main stages of compilation: analysis, synthesis, and symbol table management. 2. It outlines the content to be covered such as the evolution of programming languages, understanding compilation, compiler classification, the language processing system, and the roles of related tools like the preprocessor, assembler, and linker. 3. It describes the analysis phase which consists of lexical analysis, syntax analysis, and semantic analysis to check the program components fit together meaningfully.

Uploaded by

Ardo Legawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to Compiler

Week 1

COMP6276 – Compilation Techniques


Learning Outcome

At the end of this Session, the student will be expected


to:
• LO1: Explain the basic concepts of compilation techniques that
include the functions, stages of compilation, the components of the
compilation and compiler tool-making, as well as the theory
automata and grammar related to compilation process
Content Outline

• The Evolution of Programming Language


• Understanding of compilation
• Grouping of phases
• Cousins of the compiler
• Language Processing System
• Stages compilation
• Sample translation
• Compiler construction tools
The Evolution of Programming
Languages
• The first electronic computers appeared in the 1940's and were
programmed machine language by sequences of 0's and 1's that
explicitly told the computer what operations to execute and in what
order.

• The operations themselves were very low level: move data from one
location to another, add the contents of two registers, compare two
values, and so on.

• Needless to say, this kind of programming was slow, tedious, and


error prone. And once written, the programs were hard to
understand and modify.
Understanding of Compilation

What is Compiler? (1)

Own language Translator Other Languages

Source Language Compiler Target language

• The process of translation in everyday life


• The process of compiling with a computer
• Compiler is a program that reads a program written in a language
(source language) and translate it into another language (target
language)
Understanding of Compilation

What is Compiler? (2)


• Source language of the high level language such as PASCAL, C, C + +,
FORTRAN, COBOL, etc.

• The target language can be either Assembly language, machine


language, or other language of the source language.

• For example, translation from FORTRAN to C language, PASCAL


language to Assembly, or combinations that may occur from the
languages mentioned above.
Understanding of Compilation

What is Compiler? (3)


-. Fortran
-. Pascal
-. C Error Messages
-. C++
-. Etc.

Compiler -. Assembly
-. Machine Language
-. Etc.
Compiler Classification

Single-pass Multi-pass Debugging


Compiler Compiler Compiler
Passes the parts of each Processed the source Generate debugging
compilation unit only code/abstract syntax tree information when you
once. several times compile it

Load-and-go Optimizing
Compiler Compiler
The intermediate forms of the program are Tries to minimize or maximize some
generally kept in RAM, not saved to file attributes of an executable computer
system program.

Depending on how they have been constructed or what function they are
supposed to perform
General Description of Compiler

Source Target
Compiler
Program Program

Error messages
A Language Processing System
skeletal Source program

Preprocessor
source program

Compiler
A typical “Compilation”
target assembly program

Assembler

relocatable machine code


Library, relocatable
Object files
Loader / Link-editor

absolute machine code


Cousin of Compiler

• Preprocessor
 To incorporate skeletal program that becomes an input to the
compiler, and perform the functions of processing macros, file
inclusion, rational preprocessor, and language extensions.

• Assembler
 perform processing on the output of the compiler that produces
binary codes that do not have the memory address (memory
addressing).

• Loader and link-editor


 merge between re-locatable machine code with the library or re-
locatable object files to get absolute machine code.
Preprocessor functions

• Macro processing
 Allow user to define macros that are shorthand for longer
constructs

• File inclusion
 Preprocessor would be to include the header files into the
program.

• Rational preprocessor
 Preprocessor can improve capability or add facilities of the
language that had long (older language).

• Language extensions
 Combine commands that written in other languages into the
program in the form of a built-in macros.
Model of Compilation

Synthesis

Source program Intermediate


representation

Intermediate
representation Target program
The Phase of Compiler
Source program

lexical analyzer Analysis Phase

syntax analyzer

semantic analyzer
symbol-table manager Error
handler
intermediate code
Record generator
identifiers &
attributes code optimizer
(symbol table)
Synthesis Phase
code generator

Target program(Object code)


Analysis of the Source Program

In compiling, analysis consists of three


phases:
• Linear Analysis/ Lexical analysis/ Scanning:
 The stream of character is read from left to right and grouped
into tokens which has a collective meaning.

• Hierarchical analysis/ Syntax Analysis/ Parsing:


 Tokens are grouped hierarchically into nested collections with
collective meaning.

• Semantic analysis:
 Certain check are performed to ensure the components of a
program fit together meaningfully.
Analysis Phase
Linear analysis / lexical analysis / scanning

• Character streaming which group the sequences of characters having


a collective meaning (tokens).

position := initial + rate * 60 ;


Identifier position
Assignment symbol :=
Identifier initial
The plus sign (+)
Identifier rate
The multiplication sign (*)
The number 60
Analysis Phase
Hierarchical analysis / syntax analysis /
parsing
• Characters/tokens are grouped into grammatical phrases that are
used by compiler to synthesize output (usually represented by a
parse tree).

Assignment statement

identifier := Expression

position expression + expression


identifier expression * expression
initial identifier identifier
The hierarchical structure of a program
is usually expressed by recursive rules. rate 60
Analysis Phase
Semantic analysis

• Certain checks are performed to ensure that the components of the


program fit together meaningfully.

Example:
 Type checking
 Definite assignment (requiring all local variables to be initialized
before use)
 Object binding (associating variable and function references with
their definitions)
 Rejecting incorrect programs
 Issuing warnings
Synthesis Phase

• Intermediate Code Generation


 A representation as a program for an abstract machine.
 The properties: easy to produce and easy to translate into the
target program.

• Code Optimization
 Attempt to improve the intermediate code, so that faster-running
machine code will result.

• Code Generation
 The generation of target code, consisting normally of relocatable
machine code or assembly code.
The Phase of a Compiler
Symbol Table Management
• An essential function of a compiler is to record the identifiers used in
the source program and collect information about various attributes
of each identifier.

• A symbol table is a data structure containing a record for each


identifier, that allow us to find the record for each identifier quickly
and to store or retrieve data from that record quickly.

• An identifier is detected in the lexical analyzer phase.

• The remaining phases enter information about identifiers into the


symbol table and use this information in various ways.
The Phase of a Compiler
Error Detection and Reporting
• Each phase can encounters errors.

• A phase must somehow deal with the error, so that compilation can
proceed, allowing further errors in the source program to be
detected.

• The syntax and semantic analysis phases usually handle a large


fraction of the errors detectable by the compiler.
Translation Example
position := initial +rate * 60

lexical analyzer

id1 := id2 + id3 * 60

syntax analyzer
Symbol Table :=
1 position…….. id1 +
2 initial…….. id2 *
3 rate…….. id3 60
4
semantic analyzer

:=
id1 +
id2 *
id3 inttoreal
60
intermediate code generator
Translation Example

intermediate code generator

temp1 := inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3

code optimizer

temp1 := id3 * 60.0


id1 := id2 + temp1

code generator

MOVF id3, R2
MULF #60.0, R2
MOVF id2 ,R1
ADDF R2, R1
MOVF R1, id1
Compiler Construction Tools

1. Parser generator
 Tools that generate Syntax Analyzer (parser), from input-based
Context Free Grammar.

2. Scanner generator
 Tools generate lexical analyzer (scanner), from regular
expression based specifications.

3. Syntax-directed translation engine


 Tools that can generate a set of routines that can 'walk' (run) in
the parse tree and produces intermediate code.
Compiler Construction Tools

4. Automatic code generator


 Tools to retrieve a collection of rules that defines the translation
of each operation from the intermediate language into machine
language of the target machine.

5. Data flow engine


 Tools to gather information about how values are transmitted
from one part of the program to other programs, for the
purposes of "data flow analysis" in the code optimization phase.
Thank You
References

• Aho, A.V., Ravi, S., & Ullman, J.D. (2007). Compiler : Principle,
techniques and tools. 2nd. Addison-Wesley. New York. ISBN :
0321491696, Chapter 1.

• http://tinf2.vub.ac.be/~dvermeir/courses/compilers/compilers.pdf

• http://www.cp.eng.chula.ac.th/~piak/teaching/prolang/2013/ppt/1-intr
o.ppt

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy