11.1 Compilers Flashcards
What is the purpose of a compiler
Translates source code into machine code
What are the 3 components of a compiler (and their functions)
Pipeline as follows:
Why may it be useful to break up a compiler into separate parts
Allows it to be maintained separately and reused on different platforms etc.
What are the 2 parts of a compiler front-end and their function
- Lexer => Tokens (symplify parsing stage)
- Parser => builds AST (intermediate representation) and symbol table
What is the lexer responsible for (and what are tokens)
- Lexer: Breaks program into semantic tokens (reading character by character, ignoring whitespace and comments)
- Tokens: smallest unit of meaning within program (ie words of the language)
- Tokens may be labeled based on meaning
What is the Parser responsible for (and what is the difference between statement and expression)
- Parser: Builds abstract syntax tree from tokens
- Statement: No return val ie while. Expressions ie arithmatic expr.
What would the AST for this look like
How does a recursive decent parser work?
- Program is a series of statements
- Recursively expand statement into parts
- Build AST
What is some example psuedocode for a recursive descent parser
What is a symbol table and its use (parser)
- Keep track of variable names and scope to avoid context
What is the purpose of the compiler backend / what does it do
- Intermediate rep (ie AST) => Machine Code (object files in binary, use linker to link together)
- Optimisations (best ISA instructions, ordering etc.)
- Memory allocation
What are some potential compiler backend optimisations
- Pipelining
- Ordering
- Instruction choice
What is the concept of bootstrapping (compilers)?
- Creating a compiler that can compile itself
- Ie how to make the first compiler for lang / arch
What is a Tombstone diagram (for bootstrapping)
- Shows source to output process (using compiler written in implementation
How would you bootstrap a compiler for hex-8 (show using tombstone diagram)
- Create minimal compiler in Hex 8 assembly (perhaps only subset of features)
- Use that to compile the X compiler
- Use X compiler to compile itself (good for testing and self optimisation)
- Can now create new versions of X compiler to add more features using itself.
- Can now compile from X to Hex-8