Docsity

Compiler Construction Part 2-Compiler Construction-Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Compiler Construction Part 2-Compiler Construction-Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler ConstructionCompiler Construction

Lecture 02

BBy :

S fi T bSufi Tabassum Gul G l

OO utlinetli

  1. Introduction
  2. Phases of a compiler
  3. Cousins of compiler

4.4. ConclusionConclusion

The Translation Process/

Phases of a Compiler

Program writtenwritten Assembly in a Programming

Assembly Language Translation

Compiler g g Languages

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul 33

PhPh ases of a Compilerf C il

Program (character stream) Lexical Analyzer (Scanner) Token Stream

Lexical Analyzer (Scanner) Token Stream

g ( )

SS yntax Analyzer (Parser)t A l (P )

num ‘*’ ‘(‘ num ‘+’ num ‘)’

num * ( )

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul 77

num + num

SS yntax Analyzer (Parser)t A l (P )

int * foo(i, j, k)) int i; int j; (^) Extra parentheses { for(i=0; i j) {

Extra parentheses

fi(i>j) return j;

Missing Not an expressionincrement }

Not an expressionincrement Not a keyword

PhPh ases of a Compilerf C il

Program (character stream) Lexical Analyzer (Scanner) Token Stream

Program (character stream)

Syntax Analyzer (Parser)

Token Stream

Parse TreeParse Tree Semantic Analyzer

Parse Tree Semantic Analyzer

Parse Tree

Intermediate Representation/te ed ate ep ese tat o / Annotated Tree

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul 99

SS emantic Analyzerti A l

intint ** foo(ifoo(i , j, k)j k) int i; int j;int j; Type not declared { int

Type not declared

int x; x = x + j + N; t j

Mismatched return type

return j; U i iti li d i bl }

Uninitialized variable used Undeclared variable

Phases of a Compiler

Lexical Analyzer (Scanner)

Program (character stream) Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)

Token Stream

Semantic Analyzer

Syntax Analyzer (Parser) Parse Tree

Code Optimizer

Semantic Analyzer Intermediate Representation Code Optimizer Optimized Intermediate Representation Code Generator Assembly codeAssembly code Target Code Optimizer

Code Generator

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul 1313

Target Code Optimizer

Cousins of Compilers

CC ousins of Compileri f C il

  1. Interpreters 2 2. AAssemblers bl
  2. Linkers 4 4. LLoaders d
  3. Preprocessors
  4. Editors
  5. Debuggers
  6. Profilers
  7. Project Manager

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul 1515

II nterpreterst t

ƒ An interpreter is a language translator like a compiler.compiler.

ƒ It differs from the compiler in that it executes the source program immediately rather than generatingsource program immediately rather than generating object code that is executed after translation is complete.complete.

ƒ BASIC is a language that is more usually interpreted than compiled.interpreted than compiled.

ƒ If speed of execution is of primary concern then compiler is to be preferred compiler is to be preferred.

LL oadersd

ƒ Most often a compiler, assembler or linker will

produce code that is

  • Re-locatable

ƒ The purpose of loader is to resolve all

relocatable addresses relative to the given basel l l or starting address.g

ƒ The use of the loader makes executable code more

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul 1919

flexible.

PPreprocessors

ƒ A preprocessor is a separate program that is called

by the compiler before actual translation begins.

ƒ SuchSuch preprocessorspreprocessors cancan deletedelete

  • Comments
  • Include other files &
  • Perform macro substitutions.

EditEditors

ƒ Compiler usually accept source programs written

using any editors that will produce a standard file, such as an ASCII filesuch as an ASCII file.

ƒ Today's compilers come with editors and other

programs as IDE

ƒƒ Structure based editorsStructure based editors

15 15--OctOct--10 10 SufiSufi TabassumTabassum GulGul 2121

D bDebuggers

ƒ A debugger is a program that can be used to

determine execution errors in a compiled program

ƒƒ Bundled with IDEBundled with IDE

ƒ Step by Step execution

ƒ Can halt execution at prescribed locations called

bbreakpoints k i t

CC onclusionl i

  1. Phases of the compiler
  2. Cousins of the compiler