Grammar Overview
The Mini-Compilador Educativo implements a simple imperative language with variable declarations, arithmetic expressions, and print statements. The grammar is designed for educational clarity while demonstrating core compilation concepts.EBNF Specification
The following grammar is extracted from the source code (
compfinal.py and readme.md) and defines the complete syntax accepted by the compiler.Grammar Notation
EBNF Symbols Explained
EBNF Symbols Explained
::=- “is defined as”|- alternation (or)*- zero or more repetitions+- one or more repetitions (not used in this grammar)?- optional (zero or one)()- grouping"..."- terminal symbol (literal keyword or operator)UPPERCASE- token type (terminal from lexer)
Token Types
The lexer recognizes the following token categories:Keywords (Reserved Words)
let
Declares a new variableType:
Usage:
TipoToken.LETUsage:
let x = 10;Outputs expression valueType:
Usage:
TipoToken.PRINTUsage:
print x + 5;leo
Reserved word (no operation)Type:
Note: Recognized but ignored
TipoToken.LEONote: Recognized but ignored
diego
Reserved word (no operation)Type:
Note: Recognized but ignored
TipoToken.DIEGONote: Recognized but ignored
Literals
- Numbers
- Identifiers
Token Type:
Pattern:
Value Type: Integer
TipoToken.NUMEROPattern:
[0-9]+Value Type: Integer
Operators
+ (Addition)
Type:
Precedence: 1 (lowest)
Associativity: Left-to-right
TipoToken.SUMAPrecedence: 1 (lowest)
Associativity: Left-to-right
- (Subtraction)
Type:
Precedence: 1 (lowest)
Associativity: Left-to-right
TipoToken.RESTAPrecedence: 1 (lowest)
Associativity: Left-to-right
* (Multiplication)
Type:
Precedence: 2 (higher)
Associativity: Left-to-right
TipoToken.MULTIPLICACIONPrecedence: 2 (higher)
Associativity: Left-to-right
/ (Division)
Type:
Precedence: 2 (higher)
Associativity: Left-to-right
Note: Integer division (floor)
TipoToken.DIVISIONPrecedence: 2 (higher)
Associativity: Left-to-right
Note: Integer division (floor)
= (Assignment)
Type:
Context: Only in
Not: Used for comparison
TipoToken.IGUALContext: Only in
let statementsNot: Used for comparison
Delimiters
| Symbol | Token Type | Purpose | Example |
|---|---|---|---|
( | PAREN_IZQ | Group expressions | (5 + 3) |
) | PAREN_DER | Close grouping | (5 + 3) * 2 |
; | PUNTO_COMA | Terminate statements | let x = 5; |
Special Tokens
FIN_ARCHIVO- Marks end of inputERROR- Invalid character encountered
Grammar Rules Explained
Program Structure
main function or program wrapper.
Statements
- Variable Declaration
- Print Statement
- Creates new variable in current scope
- Evaluates expression on right-hand side
- Assigns result to variable
- Variables can be redeclared (warning issued)
Expressions
Primary Expressions (Highest Precedence)
- Numeric literals:
42 - Variable references:
x - Parenthesized expressions:
(5 + 3)
Multiplicative Operators
2 + 3 * 4 parses as 2 + (3 * 4) = 14Operator Precedence Table
| Precedence | Operators | Associativity | Example | Result |
|---|---|---|---|---|
| 1 (highest) | () | - | (5 + 3) * 2 | 16 |
| 2 | * / | Left-to-right | 10 / 2 / 5 | 1 |
| 3 (lowest) | + - | Left-to-right | 10 - 3 + 2 | 9 |
All binary operators are left-associative, meaning
a op b op c is evaluated as (a op b) op c.Example: 10 - 3 - 2 = (10 - 3) - 2 = 5Syntax Examples
Valid Programs
Common Syntax Errors
Missing Semicolon
Missing Semicolon
Invalid Variable Declaration
Invalid Variable Declaration
Missing Assignment Operator
Missing Assignment Operator
Unmatched Parentheses
Unmatched Parentheses
Using Reserved Words as Identifiers
Using Reserved Words as Identifiers
Comments
Comment Syntax:
// begins a comment that extends to end of lineImplementation: Comments are handled in the lexer. When // is detected, the scanner advances to the next newline without generating tokens.Source: compfinal.py:319-327Semantic Constraints
Beyond syntax, the semantic analyzer enforces:Variable Declaration
Variables must be declared with
let before use.Division by Zero
Literal division by zero is caught at compile time.
Only literal zeros are detected. Runtime division by zero (e.g.,
10 / x where x is 0) is not checked.Grammar Design Rationale
- Simplicity
- Precedence by Structure
- Left Recursion Elimination
- Extensibility
The grammar is intentionally minimal to focus on core compilation concepts:
- No control flow (if/while/for)
- No functions or procedures
- No data types beyond integers
- No arrays or data structures
Parse Tree Example
For the input:let x = 5 + 3 * 2;
5 + (3 * 2) due to precedence rules.
The grammar successfully handles operator precedence and associativity through its structure, eliminating the need for a separate precedence-climbing algorithm.