Compilers Flashcards
What is a compiler?
A language processor that translates a program written in some language into efficient, executable code.
Source language -> Target language - an undecidable problem!
What is a compiler responsible for?
Generate target program equivalent to source - preserve semantics.
Report errors.
What is the structure of a compiler?
Source program -> Preprocessor
Modified source program -> Compiler
Target assembly program -> Assembler
Relocatable machine code + Library files, relocatable object files -> Linker/Loader
-> Target machine code
What does the analysis (frontend) part of the compiler do?
Read + analyse source code.
Generate intermediate representation of code.
Errors reported at any stage.
Information gathered during process (e.g. type information) is stored in a symbol table.
What does the synthesis (backend) part of the compiler do?
Turns intermediate representation into target program, using symbol table.
What parts make up the frontend of the compiler?
Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generation
What is lexical analysis?
Reads source code as stream of characters, grouping to lexemes.
Extracts tokens (name, value) from lexemes and stores in symbol table.
What is syntax analysis?
Parsing - reads stream of tokens and produces syntax tree.
What is semantic analysis?
Checks syntax tree is semantically correct w.r.t. language definition (e.g. type checking) - type info stored in symbol table.
What is intermediate code generation?
Generates low-level representation of code from syntax tree.
Representation must be easy to produce and generate to translate to target code.
What parts makeup the backend of the compiler?
Intermediate code optimization (some compilers)
Code generation
Code optimization
What is intermediate code optimization?
Machine-independent, e.g. eliminate type castings.
What is code generation?
Generate sequence of machine instructions from intermediate representation.
Typically includes selecting memory locations or registers for variables.
What is code optimization?
Tries to improve the performance of the generated code.
What are lexemes?
A sequence of characters.