# Compilers

> Design and implementation of compiler technology including lexical analysis, parsing, code generation, and optimization for translating high-level programming languages into efficient machine code

- Skill: `neuralblitz/compilers-2` (Agent Skill)
- Install (CLI): `npx skillmds@latest add neuralblitz/compilers-2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/neuralblitz/compilers-2/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- License: MIT
- Author: NeuralBlitz (https://skillmd.com/u/neuralblitz)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/neuralblitz/compilers-2

---


# Compilers

## What I Do

I specialize in the design and implementation of compilers—programs that translate source code written in high-level programming languages into executable machine code, bytecode, or intermediate representations. My expertise spans the entire compilation pipeline: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, optimization, and target code generation. I work with parsing algorithms (LL, LR, recursive descent), code optimization techniques (data flow analysis, loop optimization, register allocation), and various compilation strategies (ahead-of-time, just-in-time, source-to-source). I also design domain-specific languages, implement interpreters, and build tooling for programming language development.

## When to Use Me

- Building a new programming language or domain-specific language (DSL)
- Implementing an interpreter or runtime for an existing language
- Optimizing performance-critical code through compilation techniques
- Creating source-to-source transpilers for language migration or polyglot systems
- Developing tooling for code analysis, linting, or refactoring
- Implementing JIT compilation for dynamic language runtimes
- Creating embedded DSLs within host languages
- Building code generation pipelines for code synthesis

## Core Concepts

1. **Lexical Analysis**: Tokenization of source code into meaningful lexemes using finite automata and regular expressions
2. **Syntax Analysis**: Parsing token streams into parse trees/abstract syntax trees using context-free grammars
3. **Semantic Analysis**: Type checking, scope resolution, and semantic validation
4. **Intermediate Representations**: Three-address code, SSA form, control flow graphs for optimization
5. **Code Optimization**: Local, global, and interprocedural optimizations for performance improvement
6. **Register Allocation**: Graph coloring and linear scan algorithms for efficient register usage
7. **Code Generation**: Target-specific code emission for various architectures (x86, ARM, RISC-V)
8. **Runtime Systems**: Memory management, exception handling, and calling conventions
9. **LR/LL Parsing**: Bottom-up and top-down parsing strategies for syntax analysis
10. **SSA Form**: Static Single Assignment for simplified data flow analysis

## Code Examples

```python
# Simple Lexer - Tokenizing source code
import re

class Lexer:
    TOKEN_SPECIFICATION = [
        ('NUMBER',   r'\d+'),
        ('IDENT',    r'[A-Za-z_]\w*'),
        ('OP',       r'[+\-*/=<>!]+'),
        ('STRING',   r'"[^"]*"'),
        ('SKIP',     r'[ \t\r\n]+'),
        ('MISMATCH', r'.'),
    ]
    
    def __init__(self, source):
        self.source = source
        self.tokens = []
        self._compile_patterns()
    
    def _compile_patterns(self):
        self.regex = '|'.join(f'(?P<{name}>{pattern})' 
                              for name, pattern in self.TOKEN_SPECIFICATION)
    
    def tokenize(self):
        position = 0
        while position < len(self.source):
            match = re.match(self.regex, self.source, position)
            if not match:
                raise SyntaxError(f'Illegal character at {position}')
            kind = match.lastgroup
            value = match.group()
            if kind == 'SKIP':
                position += len(value)
            elif kind == 'MISMATCH':
                raise SyntaxError(f'Unexpected token: {value}')
            else:
                self.tokens.append((kind, value))
                position += len(value)
        return self.tokens

# Usage
lexer = Lexer('int x = 42;')
tokens = lexer.tokenize()
print(tokens)
# [('IDENT', 'int'), ('IDENT', 'x'), ('OP', '='), ('NUMBER', '42'), ('OP', ';')]
```

```python
# Recursive Descent Parser for Arithmetic Expressions
class Parser:
    def __init__(self, tokens):
        self.tokens = tokens
        self.pos = 0
    
    def current_token(self):
        return self.tokens[self.pos] if self.pos < len(self.tokens) else None
    
    def consume(self, expected_type=None):
        token = self.current_token()
        if expected_type and token[0] != expected_type:
            raise SyntaxError(f'Expected {expected_type}, got {token[0]}')
        self.pos += 1
        return token
    
    def parse(self):
        return self.expr()
    
    def expr(self):
        node = self.term()
        while self.current_token() and self.current_token()[0] in ('PLUS', 'MINUS'):
            op = self.consume()[0]
            right = self.term()
            node = ('binop', op, node, right)
        return node
    
    def term(self):
        node = self.factor()
        while self.current_token() and self.current_token()[0] in ('MUL', 'DIV'):
            op = self.consume()[0]
            right = self.factor()
            node = ('binop', op, node, right)
        return node
    
    def factor(self):
        token = self.consume()
        if token[0] == 'NUMBER':
            return ('num', int(token[1]))
        elif token[0] == 'LPAREN':
            node = self.expr()
            self.consume('RPAREN')
            return node
        raise SyntaxError(f'Unexpected token: {token}')

# Tokens from: (3 + 4) * 5
tokens = [
    ('LPAREN', '('), ('NUMBER', '3'), ('PLUS', '+'), ('NUMBER', '4'),
    ('RPAREN', ')'), ('MUL', '*'), ('NUMBER', '5')
]
parser = Parser(tokens)
ast = parser.parse()
print(ast)
# ('binop', 'MUL', ('binop', 'PLUS', ('num', 3), ('num', 4)), ('num', 5))
```

```python
# Three-Address Code Generation
class TACGenerator:
    def __init__(self):
        self.code = []
        self.temp_counter = 0
        self.label_counter = 0
    
    def new_temp(self):
        name = f't{self.temp_counter}'
        self.temp_counter += 1
        return name
    
    def new_label(self):
        name = f'L{self.label_counter}'
        self.label_counter += 1
        return name
    
    def emit(self, op, arg1=None, arg2=None, result=None):
        self.code.append((op, arg1, arg2, result))
    
    def generate(self, node):
        node_type = node[0]
        
        if node_type == 'num':
            temp = self.new_temp()
            self.emit('CONST', node[1], None, temp)
            return temp
        
        elif node_type == 'var':
            return node[1]
        
        elif node_type == 'binop':
            left = self.generate(node[2])
            right = self.generate(node[3])
            temp = self.new_temp()
            self.emit(node[1], left, right, temp)
            return temp
        
        elif node_type == 'assign':
            value = self.generate(node[2])
            self.emit('COPY', value, None, node[1])
            return node[1]
        
        elif node_type == 'if':
            cond = self.generate(node[1])
            false_label = self.new_label()
            end_label = self.new_label()
            self.emit('IFZ', cond, None, false_label)
            self.generate(node[2])
            self.emit('GOTO', None, None, end_label)
            self.emit('LABEL', None, None, false_label)
            self.generate(node[3])
            self.emit('LABEL', None, None, end_label)
```

```python
# Basic Data Flow Analysis - Reaching Definitions
class ReachingDefinitions:
    def __init__(self, instructions):
        self.instructions = instructions
        self.gen = [set() for _ in instructions]
        self.kill = [set() for _ in instructions]
        self._compute_gen_kill()
    
    def _compute_gen_kill(self):
        definitions = {}
        for i, instr in enumerate(self.instructions):
            result = instr[3]  # result of instruction
            if result:
                self.gen[i].add((i, result))
                if result in definitions:
                    self.kill[i].add(definitions[result])
                definitions[result] = (i, result)
    
    def analyze(self):
        n = len(self.instructions)
        in_sets = [set() for _ in range(n)]
        out_sets = [set() for _ in range(n)]
        changed = True
        
        while changed:
            changed = False
            for i in range(n):
                pred_in = set()
                for j in self._predecessors(i):
                    pred_in.update(out_sets[j])
                
                new_in = pred_in
                new_out = (self.gen[i] | (pred_in - self.kill[i]))
                
                if new_in != in_sets[i] or new_out != out_sets[i]:
                    in_sets[i] = new_in
                    out_sets[i] = new_out
                    changed = True
        
        return in_sets, out_sets
    
    def _predecessors(self, i):
        preds = []
        for j, instr in enumerate(self.instructions):
            if instr[0] in ('GOTO', 'IFZ'):
                target = int(str(instr[3]).replace('L', ''))
                if target == i:
                    preds.append(j)
        return preds

# Example: x = 1; y = 2; x = y + 3
instructions = [
    ('CONST', 1, None, 'x'),
    ('CONST', 2, None, 'y'),
    ('ADD', 'y', 3, 'x'),
]
analyzer = ReachingDefinitions(instructions)
in_sets, out_sets = analyzer.analyze()
print("IN sets:", in_sets)
print("OUT sets:", out_sets)
```

## Best Practices

1. **Separate Concerns**: Keep lexer, parser, and code generator as distinct modules
2. **Use Established Patterns**: Leverage visitor patterns for AST traversal and processing
3. **Incremental Compilation**: Support partial compilation for faster edit-build-test cycles
4. **Error Recovery**: Implement robust error reporting with meaningful messages and locations
5. **Modular Optimization**: Design optimization passes as independent, composable units
6. **Test-Driven Development**: Write tests for each compilation phase before implementation
7. **Performance Budgeting**: Profile compilation stages to identify and optimize bottlenecks
8. **Semantic Versioning**: For language tools, maintain clear version compatibility
9. **Documentation**: Document grammar rules, optimization assumptions, and limitations
10. **Cross-Platform Design**: Abstract target architecture details behind clean interfaces

