Skip to main content

Overview

This guide showcases a complete Expresiones program that combines arithmetic operations, nested conditionals, logical operators, and multiple data types to demonstrate the full capabilities of the language.

Complete Example Program

Here’s a comprehensive program from entrada.txt that demonstrates all major features:
program {
    // 1. Declaraciones e inicializaciones
    int x = 10;
    int y = 5;
    float pi = 3.14;
    bool activo = 1; // En lógica, 1 es True
    int resultado = 0;

    // 2. Aritmética con jerarquía (Multiplicación antes que suma)
    // 10 + 5 * 2 = 20
    resultado = x + y * 2;

    // 3. Condicional con lógica compuesta (AND, OR, NOT)
    if (resultado > 15 && activo == 1) {
        // Bloque verdadero
        x = x + 100;
        
        // 4. Condicional Anidado
        if (x >= 110) {
            y = y * y; // 5 * 5 = 25
        } else {
            y = 0;
        }
    } else {
        // Este bloque no debería ejecutarse
        resultado = 999;
    }

    // 5. Prueba de paréntesis y flotantes
    // (10 + 25) / 5 = 7
    int calculoFinal = (x - 90 + y) / 5;
    
    float area = pi * 10;
}

Program Execution Walkthrough

1
Step 1: Variable Initialization
2
The program begins by declaring and initializing five variables:
3
┌─────────────┬──────────┬───────────┐
│ Identifier  │ Type     │ Value     │
├─────────────┼──────────┼───────────┤
│ x           │ int      │ 10        │
│ y           │ int      │ 5         │
│ pi          │ float    │ 3.14      │
│ activo      │ bool     │ 1 (true)  │
│ resultado   │ int      │ 0         │
└─────────────┴──────────┴───────────┘
4
Step 2: Arithmetic with Operator Precedence
5
The expression resultado = x + y * 2 is evaluated:
6
Evaluation order:
7
  • y * 25 * 2 = 10 (multiplication first)
  • x + 1010 + 10 = 20 (then addition)
  • resultado = 20
  • 8
    resultado = 20
    
    9
    Step 3: Compound Conditional Evaluation
    10
    The condition resultado > 15 && activo == 1 is checked:
    11
    Evaluation:
    12
  • resultado > 1520 > 15true
  • activo == 11 == 1true
  • true && truetrue
  • 13
    The if block will execute.
    14
    Step 4: First If Block Execution
    15
    Inside the true branch, x = x + 100 executes:
    16
  • x = 10 + 100 = 110
  • 17
    x = 110
    
    18
    Step 5: Nested Conditional
    19
    A nested condition checks x >= 110:
    20
  • 110 >= 110true
  • 21
    The nested if block executes:
    22
  • y = y * yy = 5 * 5 = 25
  • 23
    y = 25
    
    24
    Step 6: Complex Expression with Parentheses
    25
    The expression calculoFinal = (x - 90 + y) / 5 is evaluated:
    26
    Evaluation order:
    27
  • x - 90110 - 90 = 20 (parentheses first)
  • 20 + y20 + 25 = 45
  • 45 / 5 = 9
  • 28
    calculoFinal = 9
    
    29
    Step 7: Float Arithmetic
    30
    Finally, area = pi * 10 is calculated:
    31
  • 3.14 * 10 = 31.4
  • 32
    area = 31.4
    

    Final Symbol Table

    After complete execution, the symbol table contains:
    ┌──────────────┬──────────┬───────────┐
    │ Identifier   │ Type     │ Value     │
    ├──────────────┼──────────┼───────────┤
    │ x            │ int      │ 110       │
    │ y            │ int      │ 25        │
    │ pi           │ float    │ 3.14      │
    │ activo       │ bool     │ 1 (true)  │
    │ resultado    │ int      │ 20        │
    │ calculoFinal │ int      │ 9         │
    │ area         │ float    │ 31.4      │
    └──────────────┴──────────┴───────────┘
    

    Key Concepts Demonstrated

    The program uses three different data types:
    • int: For whole numbers (x, y, resultado, calculoFinal)
    • float: For decimal numbers (pi, area)
    • bool: For true/false values (activo)
    Each type is stored and manipulated according to its characteristics.
    The program demonstrates proper evaluation order:
    1. Parentheses ()
    2. Multiplication and Division * /
    3. Addition and Subtraction + -
    4. Comparison operators > < >= <= == !=
    5. Logical AND &&
    6. Logical OR ||
    Example: x + y * 2 evaluates multiplication before addition.
    The program features a conditional inside another conditional:
    if (resultado > 15 && activo == 1) {
        x = x + 100;
        
        if (x >= 110) {  // Nested condition
            y = y * y;
        } else {
            y = 0;
        }
    }
    
    This allows complex decision-making based on multiple criteria.
    The program uses && (AND) to combine multiple conditions:
    if (resultado > 15 && activo == 1)
    
    Both conditions must be true for the block to execute.
    The program shows how parentheses control evaluation in complex expressions:
    calculoFinal = (x - 90 + y) / 5;
    
    Without parentheses, the division would only apply to y.

    Control Flow Diagram

    Expected Console Output

    When this program is compiled and executed, the output would show:
    === Expresiones Compiler Output ===
    
    Parsing completed successfully.
    
    Symbol Table:
    ┌──────────────┬──────────┬───────────┐
    │ Identifier   │ Type     │ Value     │
    ├──────────────┼──────────┼───────────┤
    │ x            │ int      │ 110       │
    │ y            │ int      │ 25        │
    │ pi           │ float    │ 3.14      │
    │ activo       │ bool     │ 1         │
    │ resultado    │ int      │ 20        │
    │ calculoFinal │ int      │ 9         │
    │ area         │ float    │ 31.4      │
    └──────────────┴──────────┴───────────┘
    
    Execution completed.
    

    Variations and Experiments

    Experiment 1

    Change the initial values:
    int x = 5;
    int y = 3;
    
    Expected changes:
    • resultado = 5 + 3 * 2 = 11
    • Condition 11 > 15 is false
    • Else block executes: resultado = 999
    • x remains 5, y remains 3

    Experiment 2

    Disable activo flag:
    bool activo = 0;
    
    Expected changes:
    • First condition becomes false (due to AND)
    • Else block executes
    • No nested conditional runs
    • x remains 10, y remains 5

    Experiment 3

    Modify the nested condition:
    if (x >= 200) {  // Changed from 110
        y = y * y;
    
    Expected changes:
    • x = 110, but condition needs 200
    • Else branch executes: y = 0
    • calculoFinal = (110 - 90 + 0) / 5 = 4

    Experiment 4

    Use OR instead of AND:
    if (resultado > 15 || activo == 1)
    
    Expected changes:
    • Condition is true if EITHER part is true
    • More permissive than AND
    • If block executes more often

    Best Practices Illustrated

    Comments for Clarity: The program includes Spanish comments explaining each section. Good documentation helps others understand your code.
    Meaningful Variable Names: Names like resultado, activo, and calculoFinal clearly indicate their purpose.
    Initialization: All variables are initialized before use, preventing undefined behavior.
    Deep Nesting: While this program has only one level of nesting, avoid going too deep. More than 3 levels of nested conditionals can become hard to maintain.

    Common Pitfalls

    Problem:
    resultado = x + y * 2;  // What order?
    
    Solution: Remember multiplication happens first. Use parentheses if unsure:
    resultado = x + (y * 2);  // Explicit
    
    Problem:
    if (resultado > 15 && activo == 1)  // Both must be true
    
    Using AND when you meant OR (or vice versa) changes the logic completely.Solution:
    • Use && when ALL conditions must be true
    • Use || when ANY condition can be true
    Problem:
    calculoFinal = 45 / 5;  // Works: 9
    calculoFinal = 46 / 5;  // Truncates: 9, not 9.2
    
    Solution: If you need decimal results, use float division or cast to float.

    Next Steps

    Basic Arithmetic

    Review fundamental arithmetic operations

    Conditionals

    Deep dive into if-else statements

    Language Reference

    Complete syntax reference

    Architecture Overview

    Understand the compiler internals

    Build docs developers (and LLMs) love