Basic Programs
This page demonstrates simple complete programs that showcase the compiler’s core functionality: variable declarations, arithmetic operations, and print statements.
Simple Variable Declaration
The most basic program declares a variable and prints its value.
Lexical Analysis (Tokens)
The scanner converts the source code into tokens: TIPO LEXEMA LINEA COLUMNA VALOR
----------------------------------------------------------------------------------
LET let 1 1
IDENTIFICADOR x 1 5
IGUAL = 1 7
NUMERO 10 1 9 10
PUNTO_COMA ; 1 11
PRINT print 2 1
IDENTIFICADOR x 2 7
PUNTO_COMA ; 2 8
FIN_ARCHIVO 2 9
Syntax Analysis (AST)
The parser constructs an Abstract Syntax Tree: Programa
├── DeclaracionVariable
│ ├── nombre: 'x'
│ └── valor:
│ └── NumeroLiteral(10)
└── SentenciaPrint
└── expresion:
└── Identificador('x')
Semantic Analysis
The semantic analyzer verifies that:
Variable x is declared before use ✓
No division by zero ✓
Result: 1 variable verified
Intermediate Representation (IR)
Three-address code is generated:
Execution
The interpreter executes the program:
Arithmetic Operations
A program demonstrating basic arithmetic with multiple variables.
Source Code
Tokens
AST
IR & Execution
let a = 5 ;
let b = 3 ;
let sum = a + b ;
let product = a * b ;
print sum ;
print product ;
TIPO LEXEMA LINEA COLUMNA VALOR
-------------------------------------------------------------------------------
LET let 1 1
IDENTIFICADOR a 1 5
IGUAL = 1 7
NUMERO 5 1 9 5
PUNTO_COMA ; 1 10
LET let 2 1
IDENTIFICADOR b 2 5
IGUAL = 2 7
NUMERO 3 2 9 3
PUNTO_COMA ; 2 10
LET let 3 1
IDENTIFICADOR sum 3 5
IGUAL = 3 9
IDENTIFICADOR a 3 11
SUMA + 3 13
IDENTIFICADOR b 3 15
PUNTO_COMA ; 3 16
LET let 4 1
IDENTIFICADOR product 4 5
IGUAL = 4 13
IDENTIFICADOR a 4 15
MULTIPLICACION * 4 17
IDENTIFICADOR b 4 19
PUNTO_COMA ; 4 20
PRINT print 5 1
IDENTIFICADOR sum 5 7
PUNTO_COMA ; 5 10
PRINT print 6 1
IDENTIFICADOR product 6 7
PUNTO_COMA ; 6 14
FIN_ARCHIVO 6 15
Programa
├── DeclaracionVariable
│ ├── nombre: 'a'
│ └── valor:
│ └── NumeroLiteral(5)
├── DeclaracionVariable
│ ├── nombre: 'b'
│ └── valor:
│ └── NumeroLiteral(3)
├── DeclaracionVariable
│ ├── nombre: 'sum'
│ └── valor:
│ └── ExpresionBinaria
│ ├── operador: '+'
│ ├── izquierda:
│ │ └── Identificador('a')
│ └── derecha:
│ └── Identificador('b')
├── DeclaracionVariable
│ ├── nombre: 'product'
│ └── valor:
│ └── ExpresionBinaria
│ ├── operador: '*'
│ ├── izquierda:
│ │ └── Identificador('a')
│ └── derecha:
│ └── Identificador('b')
├── SentenciaPrint
│ └── expresion:
│ └── Identificador('sum')
└── SentenciaPrint
└── expresion:
└── Identificador('product')
Intermediate Code: a = 5
b = 3
t0 = a + b
sum = t0
t1 = a * b
product = t1
print sum
print product
Execution Output: Resultado:
→ 8
Resultado:
→ 15
Complete Compilation Pipeline
A comprehensive example showing all compilation phases for a more complex program.
Source
Tokens
AST
Intermediate Code
Assembly (8086)
let a = 10 ;
let b = 20 ;
let c = a + b * 2 ;
print c ;
Execution Result: The program outputs 50 because it evaluates as 10 + (20 * 2) = 10 + 40 = 50.Notice how operator precedence is correctly handled: multiplication is performed before addition.
The compiler supports single-line comments using //.
// This is a comment and will be ignored
let x = 42 ; // Assign the answer to life
print x ; // Output the result
Comments are removed during lexical analysis and do not generate tokens.
Use comments to document your code and make it more readable. The compiler completely ignores everything after // until the end of the line.