// File di specifica JavaCup per una calcolatrice con identificatori import java_cup.runtime.*; class Parser; //nome da attribuire al parser (classe generata) //la classe per i token avra' lo stesso nome seguito da Sym (ParserSym) action code {: SymbolTable symbolTable = new SymbolTable(); //metodo per la lettura dei valori associati agli identificatori private int leggi(String s) { return Integer.parseInt(System.console().readLine(s + "? ")); } :} /* Simboli terminali (token restituiti dallo scanner). */ terminal PIU, MENO, PER, DIVISO; terminal UNARIO, TONDA_APERTA, TONDA_CHIUSA; terminal Integer NUMERO; terminal String IDENT; /* Non terminali */ non terminal Integer expr; /* Precedenze e associativita' */ precedence left PIU, MENO; precedence left PER, DIVISO; precedence nonassoc UNARIO; /* Simbolo iniziale */ start with expr; /* Produzioni */ expr ::= expr:e1 PIU expr:e2 {: RESULT = e1 + e2; :} | expr:e1 MENO expr:e2 {: RESULT = e1 - e2; :} | expr:e1 PER expr:e2 {: RESULT = e1 * e2; :} | expr:e1 DIVISO expr:e2 {: RESULT = e1 / e2; :} | NUMERO:n {: RESULT = n; :} | IDENT:id {: Descrittore d = symbolTable.trova(id); //se l'identificatore non e' presente viene letto il valore //e creato il descrittore corrispondente if (d == null) { int value = leggi(id); d = new Descrittore(id, value); symbolTable.aggiungi(d); } RESULT = d.getValore(); :} | MENO expr:e {: RESULT = -e; :} %prec UNARIO | PIU expr:e {: RESULT = e; :} %prec UNARIO | TONDA_APERTA expr:e TONDA_CHIUSA {: RESULT = e; :} ;