Skip to content

Latest commit

 

History

History
138 lines (92 loc) · 3.15 KB

File metadata and controls

138 lines (92 loc) · 3.15 KB

TP2 -- Code Generation and Evaluation with ANTLR

1. What Was Implemented

This practical session focused on extending ANTLR grammars to support boolean expressions and generating executable behavior through grammar decoration.

Expr_Bool Grammar

  • Implemented a grammar named Expr_Bool to parse boolean expressions.
  • Supported operators:
    • not (highest precedence)
    • and
    • or (lowest precedence)
  • Added support for parenthesized expressions.
  • Ensured correct operator precedence: not > and > or.
  • Tested the grammar using multiple valid boolean expressions.

Program Extension

  • Modified the grammar so that a program can contain:
    • Multiple boolean expressions
    • Expressions separated by newline and/or semicolon.

Grammar Transformation to G

  • Used the teacher-provided grammar:

    S → true | false | not S | and S S | or S S

  • Decorated Expr_Bool so each expression produces an equivalent word in the language generated by grammar G.

  • Ensured programs with multiple expressions output transformed expressions line by line.

Evaluation Grammar (Eval_G)

  • Built an attributed grammar Eval_G to simulate evaluation of expressions generated in grammar G.

  • Computed the boolean value of each expression.

  • Modified output format to print results as:

    valeur_expr_N = v

    where:

    • N is the line number
    • v is the computed boolean value.
  • Enabled the compiler to act as a simulator executing boolean programs.


2. What Was Learned

Code Generation with ANTLR

  • Using grammar decoration to generate equivalent expressions.
  • Linking parsing with evaluation behavior.
  • Producing executable outputs from parsed structures.

Boolean Expression Parsing

  • Managing operator precedence in grammars.
  • Handling unary and binary logical operators.
  • Parsing nested expressions using parentheses.

Attributed Grammars

  • Using attributes to propagate computed values.
  • Decorating grammar rules to compute semantic results.
  • Combining syntax analysis and semantic evaluation.

Program Structure Handling

  • Supporting multiple expressions within one program.
  • Managing line-based execution outputs.

Simulation via Grammars

  • Using grammars as interpreters for expression evaluation.
  • Building simple execution simulators from parsers.

3. How to Run the Project

  1. Install ANTLR4 and ensure antlr4 and grun are available in the terminal.

  2. Generate lexer and parser files:

    antlr4 Expr_Bool.g4
    antlr4 G_Expr_Bool.g4
    antlr4 Eval_G.g4
  3. Compile generated Java files:

    javac *.java
  4. Run evaluation or parsing tests using:

    grun Expr_Bool start -gui

    or

    grun Eval_G start

4. Project Structure

.
├── Expr_Bool.g4
├── G_Expr_Bool.g4
├── Eval_G.g4
├── *.java          # Generated ANTLR files
├── *.class         # Compiled Java files
└── README.md

This TP reinforced the transition from grammar construction to semantic evaluation and demonstrated how parsers can be extended into executable simulators.