Skip to main content

Error Handling

The compiler detects and reports errors at three different phases: lexical analysis, syntax analysis, and semantic analysis. This page demonstrates common errors and how they are caught.

Error Detection Phases

Phase 1: Lexical Errors

Detected during tokenization. These errors occur when the scanner encounters invalid characters or malformed tokens.Examples: @, #, $, or other characters not in the language.

Phase 2: Syntax Errors

Detected during parsing. These errors occur when tokens are in the wrong order or structure.Examples: missing semicolons, mismatched parentheses, wrong keyword order.

Phase 3: Semantic Errors

Detected during semantic analysis. These errors occur when code is syntactically correct but logically invalid.Examples: undefined variables, division by zero.

Lexical Errors

Lexical errors occur when the scanner encounters characters that are not part of the language.

Invalid Characters

let x = 10;
let @invalid = 5;  // '@' is not valid
print x;

More Invalid Characters

Special Characters

let x = 10 $ 5;  // '$' invalid
Error: “carácter inesperado ’$’”

Hash Symbol

#define x 10;  // '#' invalid
Error: “carácter inesperado ’#’”

Backtick

let `variable = 5;  // '`' invalid
Error: “carácter inesperado ’`’”

Question Mark

let x = 5 ? 10;  // '?' invalid
Error: “carácter inesperado ’?’”
Valid characters in the language are:
  • Letters: a-z, A-Z
  • Digits: 0-9
  • Operators: +, -, *, /, =
  • Delimiters: (, ), ;
  • Whitespace: spaces, tabs, newlines
  • Comments: //
  • Underscore: _ (in identifiers)

Syntax Errors

Syntax errors occur when tokens are present but in the wrong order or missing required tokens.

Missing Equal Sign

let x 10;  // Missing '=' between x and 10
print x;

Missing Semicolon

let x = 10  // Missing semicolon
print x;
Every statement in the language must end with a semicolon ;. This is not optional!

Mismatched Parentheses

1

Missing Closing Parenthesis

let x = (10 + 5;
print x;
Error: “Se esperaba ’)’ después de la expresión. Se encontró ’;’”
2

Missing Opening Parenthesis

let x = 10 + 5);
print x;
Error: “Se esperaba una expresión. Se encontró ’)’”
3

Extra Closing Parenthesis

let x = (10 + 5));
print x;
Error: “Se esperaba ’;’ al final de la declaración. Se encontró ’)‘“

Invalid Statement Start

x = 10;  // Missing 'let' keyword
print x;
Error Message:
[FASE 2] Análisis Sintáctico...
  ✗ Error de sintaxis en línea 1, columna 1: Se esperaba 'let' o 
     'print'. Se encontró 'x'
The compiler only recognizes two types of statements:
  • let declarations
  • print statements
Any statement must start with one of these keywords.

Semantic Errors

Semantic errors occur when code is syntactically correct but violates logical rules.

Undefined Variable

let y = x + 5;  // 'x' is not defined
print y;

Self-Reference in Declaration

let x = x + 1;  // 'x' used in its own definition
print x;
Error:
[FASE 3] Análisis Semántico...
  ✗ Error semántico en línea 1, columna 9: la variable 'x' no ha 
     sido declarada
Variables cannot reference themselves in their own declaration. The variable is only added to the symbol table after its initialization expression is analyzed.

Division by Zero

let x = 10 / 0;  // Division by zero
print x;

Variable Redeclaration Warning

let x = 5;
let x = 10;  // Redeclaring 'x'
print x;
Output:
[FASE 3] Análisis Semántico...
  ✗ Advertencia en línea 2, columna 5: la variable 'x' ya fue 
     declarada anteriormente
  ✓ Completado: 1 variables verificadas

  ✗ RESULTADO: 1 error(es) encontrado(s)
Although labeled as “Advertencia” (warning), variable redeclaration is counted as an error and prevents successful compilation.

Error Recovery

The compiler attempts to recover from syntax errors and report multiple errors in a single compilation.

Multiple Errors

let x 10;        // Error 1: Missing '='
let y = z + 5;   // Error 2: 'z' undefined
print x          // Error 3: Missing ';'

Synchronization Points

When the parser encounters an error, it uses panic mode recovery:
  1. Report the error
  2. Skip tokens until a synchronization point is found:
    • Semicolon (;) - end of statement
    • Keywords (let, print) - start of new statement
  3. Continue parsing from the synchronization point
This allows the compiler to report multiple errors instead of stopping at the first one.

Common Error Patterns

Problem: Every statement must end with ;
let x = 10  // ✗ Missing semicolon
let x = 10; // ✓ Correct
The semicolon is not optional in this language.
Problem: Variables must be declared with let name = value;
x let = 10;   // ✗ Wrong order
let = x 10;   // ✗ Wrong order
let x = 10;   // ✓ Correct
Problem: Variables must be declared before use
print x;      // ✗ 'x' not declared
let x = 10;
Correct order:
let x = 10;   // ✓ Declare first
print x;      // ✓ Then use
Problem: Every ( must have a matching )
let x = (10 + 5;     // ✗ Missing ')'
let x = 10 + 5);     // ✗ Extra ')'
let x = (10 + 5);    // ✓ Balanced
Problem: Only +, -, *, / are supported
let x = 10 % 3;      // ✗ Modulo not supported
let x = 10 ** 2;     // ✗ Exponentiation not supported
let x = 10 + 3;      // ✓ Addition supported

Error Message Format

All error messages follow a consistent format:
[Type] en línea [line], columna [column]: [description]
Components:
  • Type: Error léxico, Error de sintaxis, or Error semántico
  • Line: Line number where the error occurred (1-indexed)
  • Column: Column number where the error occurred (1-indexed)
  • Description: Human-readable explanation of the error

Example

Error semántico en línea 3, columna 9: la variable 'count' no ha sido declarada
│              │         │           │                │
│              │         │           │                └─ Description
│              │         │           └────────────────── Column number
│              │         └────────────────────────────── Line number
│              └──────────────────────────────────────── "at line"
└─────────────────────────────────────────────────────── Error type
Use the line and column numbers to quickly locate errors in your source code. Most text editors can jump to a specific line:column position.

Best Practices for Avoiding Errors

Always Use Semicolons

End every statement with ; - no exceptions.

Declare Before Use

Always declare variables with let before using them.

Match Parentheses

For every (, make sure there’s a corresponding ).

Check Division

Avoid dividing by zero, even with literals.

Use Valid Characters

Stick to letters, numbers, _, and supported operators.

Follow Syntax Rules

Learn the language syntax: let name = expr; and print expr;

Build docs developers (and LLMs) love