Compilers and Compilation
Linker
Combines one or more object files and libraries, resolves symbol references (external functions and variables), and produces a single executable.
Input: Relocatable object files; Output: Executable image .
Loader
System component that loads an executable into memory, performs address relocation (fixing up symbolic addresses), handles dynamic linking, and transfers control to the program’s entry point .
Compiler optimization
Improves the performance (speed, size, resource use) of generated code without changing its semantics.
Purpose: Eliminate redundancies, reduce expensive operations (e.g., strength reduction), remove unreachable code (dead-code elimination).
Transformations: Local (within a basic block, e.g., constant folding), Global (across loops/functions, e.g., loop-invariant code motion).
Flags: -O1
(lightweight), -O2
(balanced), -O3
(aggressive), -Os
(size) .
Syntax analyzer (parser)
Consumes tokens from the lexical analyzer, checks them against a context-free grammar, and builds a parse tree (or AST) representing program structure.
Input: Token stream; Output: Parse tree or error .
Profiler
A tool that measures runtime behavior—tracking function call counts and execution times—via instrumentation or statistical sampling, to identify performance bottlenecks.
Compilation Process
Lexical Analysis – Convert source characters to tokens.
Syntax Analysis – Build parse tree from tokens.
Semantic Analysis – Type checking, symbol-table management.
Intermediate Code Generation – Produce a machine-independent representation.
Optimization – Machine-independent and machine-specific improvements.
Code Generation – Emit target machine code .
Lexical analyzer (scanner)
Reads source text, groups characters into lexemes (identifiers, keywords, literals), classifies them into token types, and passes tokens to the parser .
Errors
Lexical: Invalid characters or malformed tokens.
Syntax: Grammar violations.
Semantic: Type mismatches, undeclared identifiers, incorrect argument counts. .
Semantic analyzer
Traverses the parse tree, uses a symbol table to enforce static semantics (type checking, scope rules, type coercion), and annotates the tree or generates intermediate code .
Regular expressions
Concisely describe token patterns using union (|
), concatenation, and Kleene star (*
).
Example:
letter = [a-zA-Z]
digit = [0-9]
id = letter (letter|digit)*
``` .
Deterministic Finite Automata (DFA)
A 5-tuple (Q, Σ, δ, q₀, F) that recognizes a regular language by processing each input symbol deterministically and accepting if the final state ∈ F.
Example: A DFA for identifiers: start at q₀, on letter→q₁, then loop on letter/digit in q₁; accept in q₁ for any string matching letter(letter|digit)*
.
Performance tool overview
Includes profilers (timing), debuggers (step-through execution), memory/hot-path analyzers—each aiding in understanding and improving program behavior.
Recitation 10
Review the I/O and process-communication assignment; exam question will derive from that exercise.
Python
Modules
Load code via import module
, specific names via from module import name
, and reload with importlib.reload(module)
.
Control flow
In boolean contexts, False
, 0
, 0.0
, ''
, []
, {}
evaluate to false; all other values are true .
Comments
Start with #
for single-line; no block comments.
Reading user input
Use input(prompt)
(always returns a string); convert to numeric types as needed.
String operationslen(s)
, s.find(sub)
, s.split(sep)
, s.upper()
, s.lower()
.
String indexes
Zero-based access: s[0]
, negative indices count from the end.
Syntax
Indentation defines blocks; no semicolons or braces to terminate statements.
Arithmetic operations+
, -
, *
, /
, %
, **
(exponentiation).
For loopfor x in iterable:
iterates over elements of any sequence.
If-elseif cond: … elif cond: … else: …
; no switch
statement.
While loopwhile cond: …
; use break
and continue
for loop control.
Filesf = open(filename, mode)
; use f.read()
, f.readline()
, f.readlines()
, f.write()
, then f.close()
.
Functions
Defined with def name(args):
, use return
; default return is None
; support positional and keyword arguments.
Classesclass C:
defines a new type; methods require self
; __init__(…)
initializes instances.
Exceptions
Use try: … except ExceptionType: …
to catch; raise with raise
.
Operating system Services
Pipes: os.popen()
or subprocess
for connecting to child processes.
Signal Handling: signal.signal(signum, handler)
.
Python Threads: _thread.start_new_thread(func, args)
and locks via _thread.allocate_lock()
.
Networking Programming
Socket Module: import socket; s = socket.socket(AF_INET, SOCK_STREAM)
.
TCP Server: s.bind()
, s.listen()
, in loop conn,addr = s.accept()
, use conn.send()
, conn.recv()
.
TCP Client: s.connect((host,port))
, then s.send()
, s.recv()
.
Partial Reads/Writes: send()
may send fewer bytes; recv()
may return fewer.
Sending All Data: use sendall()
.
Data Reassembly: loop on recv()
until it returns empty string.
Timeouts: s.settimeout(seconds)
raises socket.timeout
.
Non-Blocking Sockets: s.setblocking(False)
raises exceptions on would-block.
Socket Options: s.setsockopt()
, e.g., SO_REUSEADDR
.
UDP Server/Client: use SOCK_DGRAM
with sendto()
/recvfrom()
.
Sockets and Concurrency: handle each client with threads (threading.Thread
) or processes (os.fork()
).
Threaded Server: spawn a new threading.Thread
per accept()
.
Forking Server: call os.fork()
on each new connection.
Utility Functions: socket.gethostname()
, socket.gethostbyname(host)
.