Introduction To Compiler Construction
Introduction To Compiler Construction
Construction
http://www.cs.fsu.edu/~engelen/courses/COP5621
Syllabus
Textbook:Compilers:Principles,
Techniques,andToolsbyAho,Sethi,and
Ullman
Formoreuptodateinfo:
http://www.cs.fsu.edu/~engelen/courses/
COP5621
AssignmentsandSchedule
http://www.cs.fsu.edu/~engelen/courses/COP5621/assign.html
Objectives
Knowhowtobuildacompilerfora(simplified)
(programming)language
Knowhowtousecompilerconstructiontools,suchas
generatorsforscannersandparsers
Befamiliarwithvirtualmachines,suchastheJVMand
Javabytecode
BeabletowriteLL(1),LR(1),andLALR(1)grammars(for
newlanguages)
Befamiliarwithcompileranalysisandoptimization
techniques
learnhowtoworkonalargersoftwareproject!
CompilersandInterpreters
Compilation
Translationofaprogramwritteninasource
languageintoasemanticallyequivalent
programwritteninatargetlanguage
Input
Source
Program
Target
Program
Compiler
Errormessages
Output
CompilersandInterpreters
(contd)
Interpretation
Performingtheoperationsimpliedbythe
sourceprogram
Source
Program
Interpreter
Output
Input
Errormessages
TheAnalysisSynthesisModelof
Compilation
Therearetwopartstocompilation:
Analysisdeterminestheoperationsimpliedby
thesourceprogramwhicharerecordedinatree
structure
Synthesistakesthetreestructureandtranslates
theoperationsthereinintothetargetprogram
OtherToolsthatUsethe
AnalysisSynthesisModel
Editors(syntaxhighlighting)
Prettyprinters(e.g.doxygen)
Staticcheckers(e.g.lintandsplint)
Interpreters
Textformatters(e.g.TeXandLaTeX)
Siliconcompilers(e.g.VHDL)
Queryinterpreters/compilers(Databases)
Preprocessors,Compilers,
Assemblers,andLinkers
SkeletalSourceProgram
Preprocessor
SourceProgram
Compiler
TargetAssemblyProgram
Assembler
RelocatableObjectCode
Linker
AbsoluteMachineCode
Tryforexample:
gcc -v myprog.c
Librariesand
RelocatableObjectFiles
ThePhasesofaCompiler
Phase
Output
Sample
Programmer
Sourcestring
A=B+C;
Scanner(performslexical
analysis)
Tokenstring
A, =, B, +, C, ;
Andsymboltableforidentifiers
Parser(performssyntaxanalysis Parsetreeorabstractsyntaxtree
basedonthegrammarofthe
programminglanguage)
;
|
=
/ \
A
+
/ \
B
Semanticanalyzer(type
checking,etc)
Parsetreeorabstractsyntaxtree
Intermediatecodegenerator
Threeaddresscode,quads,or
RTL
int2fp B
+
t1
:=
t2
Optimizer
Threeaddresscode,quads,or
RTL
int2fp B
+
t1
Codegenerator
Assemblycode
MOVF #2.3,r1
ADDF2 r1,r2
MOVF r2,A
Peepholeoptimizer
Assemblycode
#2.3
ADDF2 #2.3,r2
MOVF r2,A
t1
t2
A
t1
A
TheGroupingofPhases
Compilerfrontandbackends:
Analysis(machineindependentfrontend)
Synthesis(machinedependentbackend)
Passes
Acollectionofphasesmayberepeatedonlyonce
(singlepass)ormultipletimes(multipass)
Singlepass:usuallyrequireseverythingtobedefined
beforebeingusedinsourceprogram
Multipass:compilermayhavetokeepentireprogram
representationinmemory
CompilerConstructionTools
Softwaredevelopmenttoolsareavailableto
implementoneormorecompilerphases
Scannergenerators
Parsergenerators
Syntaxdirectedtranslationengines
Automaticcodegenerators
Dataflowengines
Outline
Ch.1:Introduction
Ch.2:AsimpleOnePassCompilerfortheJVM
Ch.3:LexicalAnalysisandLex/Flex
Ch.4:SyntaxAnalysisandYacc/Bison
Ch.5:SyntaxDirectedTranslation
Ch.6:TypeChecking
Ch.7:RunTimeEnvironments
Ch.8:IntermediateCodeGeneration
Ch.9:CodeGeneration
Ch.10:CodeOptimization