Compilers Flashcards
4 stages of compilers / translation
- Lexical analysis
- Syntax analysis
- Code generation
- Code optimisation
Lexical analysis
The process of breaking a string of program into a token list
- Spaces and comments are removed
- Goes through the code and tries to identify the following elements:
- Identifiers (Variable names)
- Keywords (Names built into the programming language e.g. if , while)
- Separators(Punctuation)
- Operators(+ , - , . =…….)
- Literals(numbers)
- Comments
These are formed in pairs knows as Tokens
Lexemes
Invisible blocks of text that the compiler recognises
Syntax analysis
Takes the tokenized code and applies the rules of the language to it
- Produces parse tree (also knows as an abstract syntax tree)
- Unnecessary details have been removed
- Here you will get error reports for incorrect code
-Tokens checked against rules
Code generation
Abstract syntax tree is converted into object code (almost like machine code)
-addresses and scopes of variables are established
Optimasation
Object code is converted into Optimised object code
- Requires to be parsed again (identify any inefficiencies)
Why this process is slow
Libraries
Pre written code that can be called to add functionality to the programs
Linkers
Copy compiled library code into the program
- adds to file size
- If library is referenced multiple times , the library code will be copied several times (leads to bloated programs)
- If libraries are updated the program will still keep the older version
Loaders
Copies a reference of the compiled library code
- have not copied code so can not bloating code is not a problem
- several references will not increase file size
- updates to library can make code unstable
Translators
Converts from source code (high / low level) to object code (machine / executable)
Intermediate code
Code that is halfway between machine code and object code
- This is independent of the processor architecture so can be runacross different machines and operating systems.