init with {: System.out.println("Calculatrice et AST de base"); prompt(); :} parser code {: static void prompt() { System.out.print("> "); } // Table de symboles (static et classe parser pour visibilité) public static java.util.Map symTab = new java.util.HashMap<>() ; // Gestion des variables non initialisées public static Integer symTabGet(String v) { Integer val = symTab.get(v); if (val!=null) return val ; System.out.println("Warning : " + v + " non initialisé"); symTab.put(v,0); // initialisation implicite return 0; } :} terminal NL, LPAR, RPAR, COMMA, FMIN, FMAX; terminal PLUS, MOINS, MULT, DIV, MOD; terminal AFFECT, CLEAR; terminal Integer ENTIER; terminal String VAR; nonterminal lignes, ligne, inst; nonterminal Exp expr; nonterminal AstList args; precedence left PLUS,MOINS; precedence left MULT,DIV,MOD; lignes ::= /* vide */ {: :} | lignes ligne {: prompt(); :} NL ; ligne ::= expr:e {: System.out.println(e.toPrint()); :} | inst {: :} | error {: /* msg ? */ :} ; inst ::= CLEAR {: symTab.clear(); :} | CLEAR VAR:v {: symTab.remove(v); :} | /* vide */ {: :} ; expr ::= ENTIER:e {: RESULT = new ExpEntier(e); :} | LPAR expr:e RPAR {: RESULT = e; :} | expr:e1 PLUS expr:e2 {: RESULT = new ExpOpBin(e1, '+', e2); :} | expr:e1 MOINS expr:e2 {: RESULT = new ExpOpBin(e1, '-', e2); :} | expr:e1 MULT expr:e2 {: RESULT = new ExpOpBin(e1, '*', e2); :} | expr:e1 DIV expr:e2 {: RESULT = new ExpOpBin(e1, '/', e2); :} | expr:e1 MOD expr:e2 {: RESULT = new ExpOpBin(e1, '%', e2); :} | FMIN LPAR expr:e1 COMMA expr:e2 RPAR {: RESULT = new ExpFmin(e1, e2); :} | FMAX LPAR args:l RPAR {: RESULT = new ExpFmax(l); :} | VAR:v AFFECT expr:e {: RESULT = new ExpAff(new ExpVar(v), e); :} | VAR:v {: RESULT = new ExpVar(v); :} ; args ::= expr:e {: RESULT = new AstList(); RESULT.add(e); :} | args:e1 COMMA expr:e2 {: RESULT = e1; RESULT.add(e2); :} ;