Transcript
Page 1: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

Compilers ICompilers I

CNS 3490CNS 3490

Page 2: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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)

Page 3: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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

Page 4: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

Related ProgramsRelated Programs

EditorsEditorsDebuggersDebuggersProfilersProfilersProject ManagersProject Managers

Example: rcs (revision control system)Example: rcs (revision control system) IDE IDE

Integrated development environmentsIntegrated development environments

Page 5: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

Translation ProcessTranslation Process

Parser

Code Generator

Optimizer

Scanner

source code

tokens

syntax tree

executable

executable

Literal Table

Symbol Table

Error Handler

Page 6: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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

Page 7: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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

Page 8: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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

Page 9: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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)

Page 10: Compilers I CNS 3490. History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language

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.


Top Related