Skip to main content

Compilation Pipeline

The Expresiones compiler follows a traditional multi-stage compilation architecture using ANTLR 4.13.1 for lexical and syntactic analysis. The compilation process flows through four distinct phases:
1

Lexical Analysis

The ExpresionesLexer tokenizes source code into 27 token types including keywords, operators, identifiers, and literals.
2

Syntactic Analysis

The ExpresionesParser constructs an Abstract Syntax Tree (AST) following 7 grammar rules defined in Expresiones.g.
3

Semantic Analysis

The custom Visitor class traverses the AST, performing type checking, variable resolution, and symbol table management.
4

Execution

The visitor pattern enables interpretation by evaluating expressions and executing statements during tree traversal.

Architecture Diagram

Core Components

Grammar Definition

The language specification is defined in Expresiones.g using ANTLR syntax:
  • Syntactic Rules: 7 parser rules (root, instrucciones, bloque, declaracion, asignacion, condicion, expr)
  • Lexical Rules: 27 token types covering keywords, operators, identifiers, and literals
  • Language Features: Variable declarations, assignments, arithmetic expressions, logical operations, and conditional statements
The grammar file Expresiones.g is the single source of truth. Changes to the grammar require regenerating the lexer and parser using ANTLR 4.13.1.

Generated Components

ExpresionesLexer.py

Auto-generated lexer that converts source text into tokens. Contains 27 token type constants and lexical rules.

ExpresionesParser.py

Auto-generated parser that builds the parse tree from token stream. Implements 7 context classes for AST nodes.

ExpresionesVisitor.py

Base visitor class with empty implementations for all visit methods. Extended by custom implementations.

Visitor.py

Custom visitor implementation providing semantic analysis, symbol table management, and interpretation.

Data Structures

Symbol Table

The compiler maintains a symbol table as a Python dictionary mapping variable names to Simbolo objects:
class Simbolo:
    def __init__(self, nombre, tipo, valor=None):
        self.nombre = nombre  # Variable identifier
        self.tipo = tipo      # Type: 'int', 'float', or 'bool'
        self.valor = valor    # Runtime value
The symbol table is managed by the Visitor class:
class Visitor(ExpresionesVisitor):
    def __init__(self):
        self.tabla_simbolos = {}  # Symbol table

Language Features

Supported Constructs

int x = 10;
float y = 3.14;
bool flag;
Supports int, float, and bool types with optional initialization.
int result = (a + b) * c - d / 2;
Supports +, -, *, / with proper precedence (multiplication/division before addition/subtraction).
if (x > 10) {
    y = x * 2;
} else {
    y = x / 2;
}
Supports relational operators (>, <, ==, !=, >=, <=) and logical operators (&&, ||, !).

Token Types Reference

The lexer recognizes 27 token types defined in ExpresionesLexer.py:
TokenValueDescription
PROGRAMA1Keyword program
SI2Keyword if
SINO3Keyword else
TIPO4Data types: int, float, bool
LLAVE_IZQ5Left brace {
LLAVE_DER6Right brace }
PAR_IZQ7Left parenthesis (
PAR_DER8Right parenthesis )
PUNTO_COMA9Semicolon ;
ASIGNACION10Assignment =
SUMA11Addition +
RESTA12Subtraction -
MULT13Multiplication *
DIV14Division /
MAYOR15Greater than >
MENOR16Less than <
IGUAL17Equality ==
DIFERENTE18Not equal != or <>
MAYOR_IGUAL19Greater or equal >=
MENOR_IGUAL20Less or equal <=
Y_LOGICO21Logical AND &&
O_LOGICO22Logical OR ``
NO_LOGICO23Logical NOT !
ID24Identifier pattern
NUMERO25Number literal (int or float)
WS26Whitespace (skipped)
COMENTARIO27Single-line comment (skipped)

Parser Rules

The parser implements 7 grammar rules defined in ExpresionesParser.py:
  • RULE_root (0): Program entry point
  • RULE_instrucciones (1): Instructions (declarations, assignments, conditionals)
  • RULE_bloque (2): Code blocks
  • RULE_declaracion (3): Variable declarations
  • RULE_asignacion (4): Variable assignments
  • RULE_condicion (5): Conditional expressions
  • RULE_expr (6): Arithmetic expressions
See individual component pages for detailed implementation information:

Build docs developers (and LLMs) love