Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
2 Phases Passes

2 Phases Passes

|Views: 408|Likes:
Published by abhishek gera

More info:

Published by: abhishek gera on Dec 08, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





October 06Phases & Passes1
Phases & Passes
most obvious task of compiler
translate source program in source language toobject program in object language
usually a machine language or close to it
can translate from any language intoanother
will be varying degrees of efficiency
October 06Phases & Passes2
Phases & Passes
besides translation, compiler must check meaning of source code
does it make senseif it doesn’t, produce an error message orwarning messages
several transformations before an objectprogram is produced.
Syntactic Analysis, Contextual Analysis, CodeGeneration.
October 06Phases & Passes3
Between phases
Represent the source to reflect the analysis done.Abstract Syntax Tree
AST –representst the source phrase structure.
Subtrees for commands, expressions, declarations……..Leaf nodes are the identifiers, literals and operators of the source code.
October 06Phases & Passes4
Syntactic analysisContextual analysisCode GenerationsourceASTDecorated ASTObject programError reportError report
October 06Phases & Passes5
Syntactic Analysis
Sample program
Letvar n: integer;car c: char;Inbeginc:= ‘&’;n := n+1;end
October 06Phases & Passes6
Syntactic Analysis
Determining the phrase structure or
Essential as the subsequent phases depend onknowing how the program is composed.
Source is parsed to check conformation togrammer and to construct the AST (orsuitable representation).
October 06Phases & Passes7
Syntactic Analysis
In general, the AST has
Terminal nodes for the identifiers, literals andoperators.Sub trees for the phrases on the program.
Brackets and punctuation have nocounterparts
Serve only to separate and enclose phrases
Once parsed, they have no use.
October 06Phases & Passes8
Contextual Analysis
Further analysis for conformity tocontextual constraints
Scope rules –associate each applied occurrenceof an identifier with the correspondingdeclaration –detect undeclared identifiers.Type rules –infer the type of each expressionand detect type errors.
October 06Phases & Passes9
Contextual Analysis
Yields a decorated AST
Scope rules –link to declarationType rules –addition of type for expressions.
Will show up some errors which can beeasily identified.
October 06Phases & Passes10
Code Generation
Source is known to be well formed nowafter Analysis.One issue is the treatment of identifiers.
Declaration binds an identifier to some sort of entity
Const m = 7; binds m to 7 and the generator mustreplace applied occurrences of m by 7.Var b: boolean; binds b to some address decided bythe code generator and must be replaced with thisaddress.
October 06Phases & Passes11
Code Generation
May also consider the nature of the targetlanguage
Machine code or assembley for the targetmachine.
October 06Phases & Passes12
A Pass is a complete traversal of the sourceprogram (or of an internal representation).each phase transforms the program fragment-by-fragment
input some fragment, output another
single pass compiler
if each fragment goes through all phases of transformation before compilation of the next fragment starts
source compiled in single pass over source text
October 06Phases & Passes13
entire program goes through each phase beforeany proceeds to the next
multi-pass compilereach phase performs a separate pass over the program
if there are N phases, can organise the compilerinto 1 to N passesevery compiler is multi phase, but may or may notbe multi pass
efficiency of multi pass versus single passunderstanding of multi pass versus single pass
October 06Phases & Passes14
Phases & Passes
multi pass
allows for easier understandingclarity of designmemory capacities now make themadvantageous by storing intermediate outputeasier to construct
October 06Phases & Passes15
Design Issues
One pass wins.Construction and subsequent traversals is amodest time overhead ina multi-pass.Really only an issue where the AST is stored ondisk.
Multipass requires memory to store the AST.Only one module active at a time though.
October 06Phases & Passes16
Design Issues
Favours a multipass compiler.Each module takes a single functions. Coordinatingcode is simpler.
Favours a multipass approach.Syntactic Analyser constructs the AST, the othermodules can work in any convenient order.
May generate more optimal code.
Single-pass requires sequential working on code.
October 06Phases & Passes17
Design Issues
Semantics preserving tranformations(optimisations) may require analysis of thewhole code prior to code generation andforce a multipass design.Source Language Properties may restrictdesign.
Single pass only if every phrase can becompiled using information preceding it.
October 06Phases & Passes18
Example of Compilation
after syntax analysis comes object description
uses the tree and the symbol tableanalyses declarative nodes in tree producing descriptiveinformation as shownName Description
abvariable, integer, address #1variable, real, address #2printprocedure, one integer argument,address from loader

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->