System software and language processors
Overview of system software and language processors: definitions, compiler phases, assemblers, linkers/loaders, macro processors, and examples of tokenizing, intermediate code and linking.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
System software and language processors let engineers turn human ideas (source programs) into running machines (executables) and manage machine resources. You will use these concepts when writing low-level code, debugging runtime problems, building toolchains (assemblers, compilers, linkers), or understanding performance and correctness of programs.
Key ideas
All bolded words are defined before first use.
System software: software that provides services to other software and users (examples: operating systems, assemblers, linkers, loaders, debuggers). Use: manages hardware and supports application execution.
Language processor: a program that translates or executes programs written in a programming language. Types: assembler, compiler, interpreter, macro processor, linker/loader.
Assembler: translates assembly language (mnemonics and operands) to machine code. A one-pass assembler reads source once, generates code and resolves symbols using forward references handled by e.g. forward reference tables. A two-pass assembler first collects symbol addresses (pass 1), then generates machine code (pass 2). Assumption: memory for symbol table and object code is available.
Macro processor: expands macros (named code templates) before assembly/compilation. It handles positional and keyword parameters, and built-in facilities like nested expansion and conditional expansion.
Compiler: translates high-level language source to lower-level code (assembly/machine code or another language). Exact definition: a compiler is a translator that accepts a program in one language and produces a program in another language that, when executed, is intended to have equivalent behaviour under a specified semantics.
Interpreter: directly executes source or intermediate code without producing a separate executable file. Often used for scripting languages.
Linker: combines object modules, resolves external symbol references and produces a single relocatable or absolute image.
Loader: places an executable image into memory and starts execution; performs runtime relocation and may perform dynamic linking.
Relocation: adjustment of addresses in object code so code/data can run at a different memory location than originally assumed.
Symbol table: a data structure mapping identifiers to attributes (address, type, scope). Compilers and linkers use symbol tables heavily.
Intermediate code: a representation between source and machine code (examples: three-address code, quadruples). It is often machine-independent and useful for optimization.
Compiler phases (exact statements; assume source is free of lexical errors unless stated):
- Lexical analysis (scanner): converts character stream to a stream of tokens (atomic syntactic units). Output: tokens with attributes (lexeme, token type). Assumption: regular languages describe token classes.
- Syntax analysis (parser): builds a parse tree or abstract syntax tree (AST) from tokens using a grammar. Use context-free grammars for programming languages.
- Semantic analysis: checks types, scope rules, and produces intermediate representation; may build symbol table entries. Exact checks: type compatibility rules, definite assignment, etc.
- Intermediate-code generation: produce machine-independent code (e.g., three-address code). Assumption: AST is available.
- Code optimization: transform intermediate code to improve speed/size (local/global, loop optimizations, peephole). Must preserve semantics.
- Code generation: map optimized intermediate code to target machine instructions and allocate registers.
- Assembly and linking: assembler and linker translate and combine generated code into an executable image.
Grammar transformations commonly required in exams: left recursion elimination and left factoring. Exact step-by-step methods are below (see examples).
Parser families: top-down parsers (LL(k), recursive descent) and bottom-up parsers (LR(0), SLR(1), LR(1), LALR(1)). Exact property: LR parsers can recognize a larger class of grammars than LL parsers, given equal k; LR parsers handle left recursion directly.
For assemblers and linkers, show symbol resolution: object modules contain symbol definitions, symbol references, and relocation entries. The linker builds a global symbol table, assigns final addresses, and patches relocation entries.
Describe diagrams to draw:
- Compiler block diagram: draw boxes left-to-right labelled Source Program -> Lexical Analyzer -> Syntax Analyzer -> Semantic Analyzer -> Intermediate Code Generator -> Optimizer -> Code Generator -> Assembler -> Linker -> Loader -> Executable. Draw arrows between boxes. Under each box write its main output (tokens, parse tree/AST, annotated AST, intermediate code, optimized IR, target assembly/machine code, object module, executable image, loaded image/process start).
- Linker picture: draw Module A and Module B boxes, list their defined symbols and external references. Show Global Symbol Table built, address assignment, and patched object to create final image.
Formulas
Use syntax and short formula-like lines useful in this topic.
FIRST(X) — the set of terminals that begin strings derivable from grammar symbol X. Applies when computing parser tables. No units.
FOLLOW(A) — the set of terminals that can appear immediately to the right of nonterminal A in some sentential form. Used in LL(1) parsing table construction.
x := y op z — generic form for a single three-address code statement; x,y,z are names or temporaries. No units.
SymbolAddress = LoadAddress(section) + Offset(symbol) — absolute address of a symbol within a loaded section (bytes).
PatchedValue(abs) = SymbolAddress + Addend — absolute relocation patched value at the location (bytes).
PatchedValue(pcrel) = SymbolAddress + Addend - P — PC-relative relocation; P is the address of the place (as defined by the ISA/ABI) (bytes).
Size(Object) = sum(Size(section_i)) — for placement/linking calculations, treat total module size as sum of loadable section sizes (ignoring headers/metadata/alignment). Units: bytes.
Worked examples
Example 1 — Lexical -> Parse -> Intermediate code trace
Source: x = y + 2 * z;
Step 1 (Lexical): tokens
- IDENT(x)
- ASSIGN(=)
- IDENT(y)
- PLUS(+)
- NUMBER(2)
- MUL(*)
- IDENT(z)
- SEMI(;)
Step 2 (Syntax): build a parse tree for the expression using standard expression grammar with precedence (draw a tree): root is assignment, left child IDENT(x), right child is expression; for expression create subtree: plus node with left IDENT(y) and right multiply node (2 * IDENT(z)).
Step 3 (Intermediate code) — three-address code (generate temporaries):
- t1 := 2 * z
- t2 := y + t1
- x := t2
Show constant folding optimization if 2 was an expression: no change here. If y was 0 and optimizer has algebraic rules it might simplify; but only safe optimizations allowed.
Step 4 (Code generation sketch) — assume registers R1,R2,R3, and assembly-like target:
- LOAD R1, z ; R1 := z
- MUL R1, #2 ; R1 := R1 * 2
- LOAD R2, y ; R2 := y
- ADD R2, R1 ; R2 := y + (2*z)
- STORE x, R2 ; x := R2
End result: machine code sequence emitted, with relocatable addresses for LOAD/STORE if variables are in memory.
Example 2 — Simple linking and relocation
Module A (size 100 bytes) defines symbol foo at offset 20 and references external symbol bar. Module B (size 80 bytes) defines bar at offset 8 and references foo.
Step 1: Linker builds Global Symbol Table:
- foo -> A.base + 20
- bar -> B.base + 8
Assume A is placed at address 0x1000, size 100 -> B placed at 0x1000 + 100 = 0x1064 (100 decimal = 0x64).
So addresses:
- foo -> 0x1000 + 0x14 = 0x1014
- bar -> 0x1064 + 0x08 = 0x106C
Step 2: Patching relocation entries
- In A, the reference to bar is patched with 0x106C.
- In B, the reference to foo is patched with 0x1014.
Final: single image with correct absolute addresses. Show work: compute decimal addresses if needed, keep units as bytes.
Example 3 — Left recursion elimination (typical exam derivation)
Given grammar: A -> A alpha | beta (direct left recursion). Eliminate left recursion using standard transformation.
Step 1: Replace by: A -> beta A' A' -> alpha A' | epsilon
Explain: A' is a new nonterminal. Draw derivation showing original left-recursive derivation and transformed derivation produce same language. Assumption: alpha is non-empty sequence of grammar symbols.
Example 4 — Left factoring (typical exam derivation)
Given grammar: S -> id = E | id ( L ) | id
Step 1: Factor the common prefix: S -> id S' S' -> = E | ( L ) | epsilon
Explain: Left factoring defers the decision until enough input is seen by introducing a new nonterminal to capture alternatives after the shared prefix.
Common mistakes
- Not distinguishing between linking (resolving symbols) and loading (placing in memory and starting execution).
- Mixing up FIRST and FOLLOW sets when building LL(1) tables — compute FIRST of right-hand side, only use FOLLOW if epsilon in FIRST.
- Omitting relocation when computing final addresses in linking problems.
- Thinking optimization can change program semantics: only semantics-preserving transformations allowed (within defined behaviour). Forgetting undefined behaviour exceptions (e.g., signed integer overflow).
For your exam
Question types you will see: definitions (define assembler/linker/compiler), short notes (compiler phases, symbol table structure), derivations (eliminate left recursion, compute FIRST/FOLLOW), algorithms (construct LL(1) parse table, convert RE to DFA), numerical linking/relocation problems (compute final addresses and patched values), program traces (tokenize, parse, produce three-address code), and diagram-based questions (compiler pipeline, linker flow).
What full-marks answers include:
- Clear definitions of every bold term, with an example.
- Diagrams labelled exactly as requested (see "Key ideas" diagram descriptions).
- For derivations, show each substitution step (e.g., left recursion elimination or FIRST/FOLLOW computation) and state any assumptions (no epsilon except where shown).
- For numerical problems, list given data, show intermediate calculations with units (bytes), and final addresses/values.
- For algorithm questions, show step-by-step table or stack trace (e.g., parse stack or shift-reduce sequence).
Quick check
- What is the difference between relocation and linking?
- Give the three main outputs of the lexical analyzer.
- How do you eliminate direct left recursion for A -> A a | b ?
Answers: 1) Linking resolves symbol definitions across modules and builds the combined image; relocation is the action of adjusting addresses so code/data refer to correct final addresses. 2) Tokens (type), lexeme/text, and token attributes (e.g., numeric value or symbol-table index). 3) Replace with A -> b A' ; A' -> a A' | epsilon.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?