Overview
The Parser (also called Syntax Analyzer) is the second phase of compilation. It takes the flat list of tokens from the Scanner and organizes them into a hierarchical Abstract Syntax Tree (AST) according to the language grammar.Analogy: Like diagramming a sentence in grammar class.“The cat eats fish” →
Grammar Rules
The parser follows this context-free grammar:Operator Precedence is encoded by nesting depth:
primary(highest precedence) - literals, variables, parenthesesmultiplication-*and/addition(lowest precedence) -+and-
3 + 4 * 2 parses as 3 + (4 * 2), not (3 + 4) * 2.AST Node Types
The parser constructs these node types:Expression Nodes
- NumeroLiteral
- Identificador
- ExpresionBinaria
- ExpresionAgrupada
Represents a numeric constant.Example:
Statement Nodes
- DeclaracionVariable
- SentenciaPrint
Variable declaration statement.Example:
Program Node
Parsing Algorithm
The parser uses Recursive Descent Parsing:Parsing Example
Let’s trace howlet x = 5 + 3 * 2; is parsed:
Precedence Visualization
- Correct Precedence
- Parentheses Override
- Same Precedence
Input: Evaluation:
a + b * cAST:a + (b * c)Why: _suma_resta() calls _multiplicacion_division() for right side, which consumes both b and c before returning.Error Handling
Syntax Errors
The parser reports specific, helpful error messages:Missing Semicolon
Missing Semicolon
Input:
let x = 5Error:Missing Variable Name
Missing Variable Name
Input:
let = 5;Error:Unmatched Parenthesis
Unmatched Parenthesis
Input:
let x = (5 + 3;Error:Unexpected Token
Unexpected Token
Input:
+ x = 5;Error:Error Recovery (Panic Mode)
The parser continues after errors to find multiple issues:Helper Methods
- _coincide()
- _consumir()
- _verificar()
- _avanzar()
Check if current token matches any of the given types, and consume if so:Usage:
Complete Parsing Example
- Input Code
- Token Stream
- AST (Tree View)
- Parser Output
Performance
Time Complexity
O(n) where n = token countEach token is visited exactly once.
Space Complexity
O(d) where d = max nesting depthRecursion stack depth = max expression nesting.
Source Code Reference
Implementation
File:
compfinal.pyLines: 494-950Key Classes:Expresion,Sentencia,Programa- AST node typesNumeroLiteral,Identificador,ExpresionBinaria,ExpresionAgrupada- Expression nodesDeclaracionVariable,SentenciaPrint- Statement nodesParser- Main parser implementation
parsear()- Entry point, returnsPrograma_sentencia()- Parse one statement_declaracion_variable()- Parseletstatement_sentencia_print()- Parseprintstatement_expresion()→_suma_resta()→_multiplicacion_division()→_primario()- Expression parsing with precedence
Next Steps
Semantic Analysis
See how the AST is validated for logical correctness
API Reference
Detailed API documentation for the Parser class