init with {: System.out.println("Calculatrice et arbre CST"); 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 Ast expr, 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 Ast("Litteral", new Ast("ENTIER")); :} | LPAR expr:e RPAR {: RESULT = new Ast("Par", new Ast("LPAR"), e, new Ast("RPAR")); :} | expr:e1 PLUS expr:e2 {: RESULT = new Ast("Plus", e1, new Ast("PLUS"), e2); :} | expr:e1 MOINS expr:e2 {: RESULT = new Ast("Moins", e1, new Ast("MOINS"), e2); :} | expr:e1 MULT expr:e2 {: RESULT = new Ast("Mult", e1, new Ast("MULT"), e2); :} | expr:e1 DIV expr:e2 {: RESULT = new Ast("Div", e1, new Ast("DIV"), e2); :} | expr:e1 MOD expr:e2 {: RESULT = new Ast("Mod", e1, new Ast("MOD"), e2); :} | FMIN LPAR expr:e1 COMMA expr:e2 RPAR {: RESULT = new Ast("Min", new Ast("FMIN"), new Ast("LPAR"), e1, new Ast("COMMA"), e2, new Ast("RPAR")); :} | FMAX LPAR args:e RPAR {: RESULT = new Ast("Max", new Ast("FMAX"), new Ast("LPAR"), e, new Ast("RPAR")); :} | VAR:v AFFECT expr:e {: RESULT = new Ast("Aff", new Ast("VAR"), new Ast("AFFECT"), e); :} | VAR:v {: RESULT = new Ast("Var", new Ast("VAR")); :} ; args ::= expr:e {: RESULT = new Ast("Arg", e) ; :} | args:e1 COMMA expr:e2 {: RESULT = new Ast("Arglist", e1, new Ast("COMMA"), e2); :} ;