Skip to main content

Your First Program

Let’s create and compile a simple Expresiones program that demonstrates variable declarations, arithmetic operations, and conditional logic.
1

Create a Program File

Create a new file called programa.txt with the following content:
program{
    int valorUno; 
    int valorDos; 
    int resultado;
    int exito; 
    int error;

    valorUno = 5; 
    valorDos = 10; 
    resultado = valorUno + valorDos;

    if(resultado > 10){
        exito = 1; 
    } else {
        error = 0; 
    }
}
Every Expresiones program must start with the program keyword followed by a block enclosed in curly braces.
2

Run the Compiler

Navigate to your source directory and run the compiler:
cd workspace/source
python Prueba.py
A file dialog will open. Select your programa.txt file.
3

View the Output

The compiler will execute your program and display the symbol table:
Iniciando ejecución...
Declaración: valorUno (int) = None
Declaración: valorDos (int) = None
Declaración: resultado (int) = None
Declaración: exito (int) = None
Declaración: error (int) = None
Asignación: valorUno = 5
Asignación: valorDos = 10
Asignación: resultado = 15
Asignación: exito = 1

--- TABLA DE SÍMBOLOS FINAL ---
ID              | Tipo       | Valor     
----------------------------------------
valorUno        | int        | 5
valorDos        | int        | 10
resultado       | int        | 15
exito           | int        | 1
error           | int        | None
The symbol table shows all declared variables with their final types and values.

Understanding the Output

The Expresiones compiler provides two types of output:

Execution Trace

As the compiler processes your code, it prints execution information:
  • Declaración: Shows when a variable is declared with its type and initial value
  • Asignación: Shows when a variable is assigned a new value

Symbol Table

The final symbol table displays:
  • ID: Variable name
  • Tipo: Variable type (int, float, or bool)
  • Valor: Final value after execution (or None if never assigned)

Advanced Example

Here’s a more complex example demonstrating all language features:
program {
    // Declarations with initialization
    int x = 10;
    int y = 5;
    float pi = 3.14;
    bool activo = 1;
    int resultado = 0;

    // Arithmetic with operator precedence
    // 10 + 5 * 2 = 20
    resultado = x + y * 2;

    // Conditional with logical operators
    if (resultado > 15 && activo == 1) {
        x = x + 100;
        
        // Nested conditional
        if (x >= 110) {
            y = y * y; // 5 * 5 = 25
        } else {
            y = 0;
        }
    } else {
        resultado = 999;
    }

    // Parentheses for grouping
    int calculoFinal = (x - 90 + y) / 5;
    
    float area = pi * 10;
}
This example is from entrada.txt in the source repository and demonstrates:
  • Variable declarations with initialization
  • Arithmetic operations with correct precedence
  • Logical operators (&&, ||, !)
  • Nested conditionals
  • Parentheses for expression grouping
  • Mixed integer and floating-point arithmetic

Language Features

Data Types

Expresiones supports three data types:
int contador = 42;
int resultado;

Operators

Arithmetic: +, -, *, / Relational: >, <, ==, != (or <>), >=, <= Logical: && (and), || (or), ! (not)

Control Flow

Conditional statements with optional else clause:
if (x > 10) {
    y = 1;
} else {
    y = 0;
}

Comments

Single-line comments start with //:
// This is a comment
int x = 10; // Inline comment

Common Patterns

Semantic Errors: If you try to use a variable before declaring it or declare the same variable twice, the compiler will report a semantic error.

Variable Declaration

// Declaration without initialization
int x;

// Declaration with initialization
int y = 10;

Complex Conditions

// Combining multiple logical operators
if ((x > 5 && y < 10) || !activo) {
    resultado = 100;
}

Arithmetic Expressions

// Operator precedence: * and / before + and -
resultado = x + y * 2;        // y is multiplied first

// Use parentheses to override precedence
resultado = (x + y) * 2;      // addition happens first

Next Steps

Explore the Grammar

Learn about the complete grammar definition in Expresiones.g

Visitor Pattern

Understand how the Visitor pattern is used to traverse the AST

Language Reference

Dive into the complete language syntax and semantics

Examples

Explore more code examples and patterns

Running Without GUI

If you want to run the compiler without the file dialog (useful for automation), you can modify Prueba.py:16-18:
def main():
    # ruta = seleccionar_archivo()
    ruta = "entrada.txt"  # Hardcode the file path
    if not ruta: return

Build docs developers (and LLMs) love