Overview
TheExpresionesLexer class is automatically generated by ANTLR 4.13.1 from the grammar file Expresiones.g. It performs lexical analysis by converting raw source code into a stream of tokens that the parser can understand.
This class is generated code. Do not modify
ExpresionesLexer.py directly. Instead, update the lexical rules in Expresiones.g and regenerate using ANTLR.Class Definition
~/workspace/source/ExpresionesLexer.py:75
Token Type Constants
The lexer defines 27 token type constants used throughout the compilation process:Keywords
Grammar Definition: Keywords
Grammar Definition: Keywords
From
Expresiones.g:34-37:Delimiters
Grammar Definition: Delimiters
Grammar Definition: Delimiters
From
Expresiones.g:39-43:Operators
Arithmetic Operators
Grammar Definition: Arithmetic Operators
Grammar Definition: Arithmetic Operators
From
Expresiones.g:44-49:Relational Operators
Grammar Definition: Relational Operators
Grammar Definition: Relational Operators
From
Expresiones.g:51-56:Logical Operators
Grammar Definition: Logical Operators
Grammar Definition: Logical Operators
From
Expresiones.g:58-60:Identifiers and Literals
Grammar Definition: Identifiers and Literals
Grammar Definition: Identifiers and Literals
From
Expresiones.g:62-65:Lexer Configuration
The lexer maintains several configuration arrays defined inExpresionesLexer.py:109-131:
Channel Names
Mode Names
Literal Names
Symbolic Names
Rule Names
Tokenization Process
Pattern Matching
Using the serialized ATN (Augmented Transition Network), the lexer matches character sequences against lexical rules.
Token Generation
When a pattern matches, a token object is created with:
- Token type (one of the 27 constants)
- Text content
- Line and column position
- Channel (DEFAULT or HIDDEN)
Filtering
Tokens on the HIDDEN channel (whitespace and comments) are filtered from the parser’s view.
Tokenization Examples
Example 1: Variable Declaration
Input:Example 2: Arithmetic Expression
Input:Example 3: Conditional Statement
Input:Example 4: Comments and Whitespace
Input:Comments and whitespace are automatically filtered out. They match rules with
-> skip directives in the grammar.Special Token Patterns
Identifier Pattern
FromExpresiones.g:62:
- Must start with a letter (uppercase or lowercase)
- Can contain letters and digits
- Examples:
x,variable1,myVar,count2 - Invalid:
1var(starts with digit),my-var(contains hyphen)
Number Pattern
FromExpresiones.g:63:
- Integer: one or more digits
- Float: digits, followed by decimal point, followed by one or more digits
- Examples:
42,3.14,0.5,100 - Invalid:
.5(no leading digit),5.(no trailing digits)
Comment Pattern
FromExpresiones.g:65:
- Single-line comments only
- Start with
// - Continue until end of line
- Automatically skipped by lexer
Lexer Constructor
FromExpresionesLexer.py:133-138:
- input: Character stream to tokenize
- output: Output stream for lexer messages
- checkVersion: Ensures ANTLR runtime version matches generator version
- _interp: ATN simulator that executes the lexical rules
Error Handling
The lexer automatically handles lexical errors when it encounters:- Invalid characters not matching any rule
- Malformed number literals
- Unexpected EOF
For custom error handling, implement a custom error listener and attach it to the lexer instance.