This practical session focused on generating executable code for the MVaP virtual machine from expressions involving integers, rational numbers, and boolean values.
- Implemented code generation targeting the MVaP virtual machine.
- Generated programs always terminate with a
HALTinstruction. - Ensured generated programs correctly manage stack behavior and intermediate storage.
-
Implemented rational numbers represented as:
numerator / denominator
-
Supported rational arithmetic operations:
- Addition
- Subtraction
- Multiplication
- Division
-
Implemented exponentiation using the operator
**, where the exponent is an integer. -
Enabled extraction of numerator and denominator using dedicated operations.
The language supports:
- Rational expressions with standard operators:
+,-,*,/- Unary and binary subtraction
- Proper operator precedence and parenthesis handling.
- Automatic interpretation of integers as rational numbers (e.g.,
3becomes3/1).
- Implemented boolean operators:
andornot
- Enforced operator precedence: not > and > or.
- Implemented lazy (short-circuit) evaluation in generated MVaP code.
- Supported comparisons between rational expressions:
<,<=,>,>=,==,<>
- Comparison results produce boolean values.
- Implemented input operation allowing expressions to read values at runtime.
- Behavior depends on context:
- Reads integers when required.
- Reads rationals as two integers.
- Reads booleans as integers (0 = false, positive = true).
-
Implemented instruction:
Afficher(expr);
-
Output depends on expression type:
- Integers printed directly.
- Booleans printed as 1 (true) or 0 (false).
- Rationals printed as numerator followed by denominator.
The project includes several grammar versions:
Rationnel.g4-- main grammar implementing the language.Rationnel-ex2.g4-- intermediate or extended version used for development.RationnelK.g4-- grammar version used for testing and experimentation during implementation.
These grammars were used incrementally during development and validation of code generation.
- Translating high-level expressions into virtual machine instructions.
- Managing stack operations and temporary storage.
- Ensuring correct program termination.
- Representing rational values using numerator/denominator pairs.
- Implementing arithmetic operations at machine level.
- Implementing short-circuit boolean evaluation in generated code.
- Avoiding unnecessary execution branches.
- Incremental grammar development.
- Testing grammars before integrating code generation.
- Linking parsing, semantic processing, and machine code output.
- Understanding execution behavior of stack machines.
- Generating correct instruction sequences for evaluation.
-
Install ANTLR4 and ensure
antlr4andgrunare available. -
Install the MVaP simulator and assembler.
-
Generate lexer and parser files:
antlr4 Rationnel.g4 antlr4 Rationnel-ex2.g4 antlr4 RationnelK.g4
-
Compile generated Java files:
javac *.java -
Run the compiler to generate MVaP code and assemble it using the MVaP tools.
-
Execute the assembled program using the MVaP simulator.
.
├── Rationnel.g4
├── Rationnel-ex2.g4
├── RationnelK.g4 # Used for testing and implementation
├── *.java # Generated ANTLR files
├── *.class # Compiled Java files
└── README.md
This TP completes the transition from expression parsing to full code generation targeting a virtual machine, enabling execution of rational and boolean programs compiled from ANTLR grammars.