Overview
The Expresiones language grammar is defined inExpresiones.g using ANTLR4 syntax. This grammar specifies both the syntactic structure (parser rules) and lexical tokens (lexer rules) of the language.
Grammar File Structure
The grammar file is organized into two main sections:- Syntactic Rules - Define the structure of valid programs
- Lexical Rules - Define tokens (keywords, operators, identifiers)
Root Rule
Every Expresiones program starts with theroot rule:
- The keyword
program - Opening brace
{ - One or more instructions
- Closing brace
} - End of file
Syntactic Rules
Instructions
Theinstrucciones rule defines three types of statements:
#InstrDecl, #InstrAsig, #InstrIf) that generates separate visitor methods.
Declarations
- Type specification (int, float, bool)
- Variable name
- Optional initialization with an expression
Assignments
Expressions
Expressions support arithmetic operations with proper precedence:Conditions
Boolean expressions for control flow:- Logical operators:
&&(AND),||(OR),!(NOT) - Comparison operators:
>,<,==,!=,<>,>=,<= - Parenthesized conditions
Lexical Rules
Keywords
Operators
Arithmetic:Identifiers and Literals
- ID: Must start with a letter, followed by letters or digits
- NUMERO: Integers or decimals (3.14, 42)
Whitespace and Comments
Modifying the Grammar
instrucciones
: declaracion PUNTO_COMA #InstrDecl
| asignacion PUNTO_COMA #InstrAsig
| SI PAR_IZQ condicion PAR_DER bloque (SINO bloque)? #InstrIf
| WHILE PAR_IZQ condicion PAR_DER bloque #InstrWhile
;
Implement visitor methods for new labeled rules (see Extending the Compiler).
Common Grammar Patterns
Adding a New Operator
-
Define the token:
-
Add to expression rule:
-
Handle in visitor:
Adding a New Statement Type
-
Define required tokens:
-
Create the rule:
-
Implement
visitInstrPrint()in your visitor
Adding a New Data Type
Best Practices
- Use Labels: Always add
#Labelto rule alternatives to generate specific visitor methods - Order Matters: Put more specific tokens before general ones to avoid matching conflicts
- Left Recursion: ANTLR4 handles left recursion automatically - use it for left-associative operators
- Test Incrementally: After each grammar change, test with simple programs before adding complexity
Grammar Debugging Tips
- Use grun: ANTLR provides a test rig to visualize parse trees
- Check Ambiguity: ANTLR will warn about ambiguous rules
- Validate Tokens: Ensure lexer tokens don’t overlap unexpectedly
- Test Edge Cases: Empty programs, nested structures, operator precedence
Next Steps
- Learn how to extend the compiler with new visitor methods
- Understand testing strategies for validating grammar changes