Compilers ICompilers I
CNS 3490CNS 3490
HistoryHistory
WiresWires Machine LanguageMachine Language
FFBAFFBA No Translation necessaryNo Translation necessary
Assembly LanguageAssembly Language Mov r1, 0xFBMov r1, 0xFB AssemblerAssembler
CompilersCompilers X = 2X = 2 Fortran (1954-1957)Fortran (1954-1957)
Related ProgramsRelated Programs
InterpretersInterpreters Executes source program immediately rather than Executes source program immediately rather than
generating object code.generating object code. LinkersLinkers
Collects separately compiled code into one Collects separately compiled code into one executableexecutable
LoadersLoaders Often addresses need to be relocated in order for a Often addresses need to be relocated in order for a
program to be loaded into a specific spot in memoryprogram to be loaded into a specific spot in memory PreprocessorPreprocessor
Output = Source CodeOutput = Source Code
Related ProgramsRelated Programs
EditorsEditorsDebuggersDebuggersProfilersProfilersProject ManagersProject Managers
Example: rcs (revision control system)Example: rcs (revision control system) IDE IDE
Integrated development environmentsIntegrated development environments
Translation ProcessTranslation Process
Parser
Code Generator
Optimizer
Scanner
source code
tokens
syntax tree
executable
executable
Literal Table
Symbol Table
Error Handler
The ScannerThe Scanner Performs Performs Lexical AnalysisLexical Analysis ExampleExample
a[index] = 4 + 2a[index] = 4 + 2 Would be translated intoWould be translated into
aa identifieridentifier [[ left bracketleft bracket indexindex identifieridentifier ]] right bracketright bracket == assignmentassignment 44 numbernumber ++ plus signplus sign 22 numbernumber
The ParserThe Parser
Performs Performs Syntax AnalysisSyntax Analysis It determines the syntax of the source codeIt determines the syntax of the source code
Turn the expression: Turn the expression: a[index] = 4 + 2a[index] = 4 + 2
into a parse treeinto a parse tree
Code GeneratorCode Generator
Takes an annotated parse tree and turns it Takes an annotated parse tree and turns it into machine code or assembly language.into machine code or assembly language.
For exampleFor example The parse Tree forThe parse Tree for a[index] = 6 a[index] = 6 could be turned into could be turned into
Mov r0, indexMov r0, indexMul r0, 2Mul r0, 2Mov r1, &aMov r1, &aAdd r1, r0Add r1, r0Mov *r1, 6Mov *r1, 6
Major Data Structures Major Data Structures in a Compilerin a Compiler
TokensTokens Symbolic representation of one lexical component Symbolic representation of one lexical component
(one symbol)(one symbol) Syntax TreeSyntax Tree
Represents the structure of the source code being Represents the structure of the source code being parsedparsed
Symbol TableSymbol Table Collection of all of the variables, class names etc.Collection of all of the variables, class names etc.
Literal TableLiteral Table Collection of all of the literal values (especially strings)Collection of all of the literal values (especially strings)
Error HandlingError Handling
Any good compiler should respond well to Any good compiler should respond well to static or (compile-time) errors.static or (compile-time) errors.
The compilation process should terminate The compilation process should terminate gracefullygracefully
Ideally, only one error should be reported Ideally, only one error should be reported for each actual error in the source code.for each actual error in the source code.