This practical session focused on integrating arithmetic and boolean expressions within a compiler generated using ANTLR and enabling their interpretation.
- Built a grammar
Expr_Calculettecapable of parsing:- Arithmetic expressions
- Boolean expressions
- Allowed programs to contain multiple expressions separated by newline or semicolon.
- Ensured compatibility between arithmetic and boolean components.
- Decorated the grammar so that each expression is interpreted during execution.
- Arithmetic expressions evaluate to numeric values.
- Boolean expressions evaluate to logical values.
- Output corresponds to evaluated results.
- Extended boolean expressions to support comparisons between arithmetic expressions.
- Implemented comparison operators:
==<><><=>=
- Comparison results produce boolean values usable inside logical expressions.
-
Added instruction:
Afficher(expr)
-
Allows explicit output of evaluated expressions.
-
Supports both arithmetic and boolean expressions.
-
Interpretation prints evaluation results when invoked.
- Combining arithmetic and boolean grammars.
- Handling expressions of different semantic types.
- Managing precedence across arithmetic and logical operators.
- Evaluating expressions directly from grammar rules.
- Propagating semantic values through attributes.
- Integrating syntax parsing with runtime evaluation.
- Converting arithmetic comparisons into boolean expressions.
- Supporting comparisons inside logical operations.
- Incrementally extending grammars with new instructions.
- Adding output instructions similar to programming languages.
- Using ANTLR grammars to construct interpreters.
- Executing expression programs directly via parsing.
-
Install ANTLR4 and ensure
antlr4andgrunare available in the terminal. -
Generate lexer and parser files:
antlr4 Expr_Calculette-ex1.g4 antlr4 Expr_Calculette-ex2.g4 antlr4 Expr_Calculette-ex3.g4 antlr4 Expr_Calculette-ex4.g4
-
Compile generated Java files:
javac *.java -
Run tests using:
grun Expr_Calculette start -gui
.
├── Expr_Calculette-ex1.g4
├── Expr_Calculette-ex2.g4
├── Expr_Calculette-ex3.g4
├── Expr_Calculette-ex4.g4
├── *.java # Generated ANTLR files
├── *.class # Compiled Java files
└── README.md
This TP demonstrated how grammars evolve from parsing expressions to building interpreters capable of evaluating mixed arithmetic and boolean programs.