Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // gramatyka:
- // wyrazenie ::= literal | '(' wyrazenie ')' | unarny wyrazenie | wyrazenie binarny wyrazenie
- // unarny ::= '+' | '-'
- // binarny ::= '+' | '-' | '*' | '/' | '^' | '%'
- // literal ::= cyfra { cyfra }*
- // cyfra ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
- import java.util.Map;
- import java.util.HashMap;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.BufferedReader;
- class Parser {
- static class SyntaxErrorException extends Exception {
- SyntaxErrorException(){ }
- SyntaxErrorException(String s){ super(s); }
- }
- public enum Expression {
- PLUS(0) { public double eval(double a, double b){ return a; } },
- MINUS(3) { public double eval(double a, double b){ return -a; } },
- ADD(1) { public double eval(double a, double b){ return a+b; } },
- SUB(1) { public double eval(double a, double b){ return a-b; } },
- MUL(2) { public double eval(double a, double b){ return a*b; } },
- DIV(2) { public double eval(double a, double b){ return a/b; } },
- POW(5){ public double eval(double a, double b){ return Math.pow(a,b); } },
- MOD(2){ public double eval(double a, double b){ return a%b; } };
- final int priority;
- abstract public double eval(double a, double b);
- Expression(int p){ priority = p; }
- }
- static int _index=0;
- static String _operands[];
- static Map<String, Expression> _unary = new HashMap<String, Expression>();
- static Map<String, Expression> _binary = new HashMap<String, Expression>();
- static {
- _unary.put("+", Expression.PLUS);
- _unary.put("-", Expression.MINUS);
- _binary.put("+", Expression.ADD);
- _binary.put("-", Expression.SUB);
- _binary.put("*", Expression.MUL);
- _binary.put("/", Expression.DIV);
- _binary.put("^", Expression.POW);
- _binary.put("%", Expression.MOD);
- }
- private Parser(){ }
- static public void setExpression(String expression){
- for(String o : _binary.keySet())
- expression = expression.replace(o, " " + o + " " );
- expression = expression.replace("(", " ( ");
- expression = expression.replace(")", " ) ");
- _operands = (expression+" $").trim().split("( )+");
- _index = 0;
- }
- static double convert(String token) throws SyntaxErrorException {
- try {
- peek();
- return Double.parseDouble(token);
- } catch(NumberFormatException e){
- String c = check();
- if(c.equals("$"))
- c = "konca wyrazenia";
- throw new SyntaxErrorException("Nieoczekiwany symbol: '" +(token.equals("$") ? "koniec wyrazenia" : token )+"' w poblizu '"+ c +"'");
- }
- }
- // zwraca kolejny element z operandow
- static String peek() throws SyntaxErrorException {
- if(_index >= _operands.length)
- throw new SyntaxErrorException("Zbyt krotkie wyrazenie");
- return _operands[_index++];
- }
- static String check(){
- if(_index >= _operands.length) return "$";
- else return _operands[_index-1];
- }
- static double primary(String token) throws SyntaxErrorException {
- if(token.equals("(")){
- double result = subexpr(0);
- if(!check().equals(")"))
- throw new SyntaxErrorException("Brak nawiasu zamykajacego w poblizu '"+ check()+"'");
- peek();
- return result;
- } else return convert(token);
- }
- static double subexpr(int limit) throws SyntaxErrorException {
- double result;
- String token = peek();
- Expression op = _unary.get(token);
- if(op != null) result=op.eval(subexpr(op.priority), 0);
- else result=primary(token);
- while((op = _binary.get(check())) != null && op.priority>limit)
- result=op.eval(result, subexpr(op.priority));
- return result;
- }
- static public double parse(String expr) throws SyntaxErrorException {
- setExpression(expr);
- if(_operands[0].equals("$")) throw new SyntaxErrorException("Brak wyrazenia, aby wyjsc nacisnij Ctrl+D");
- double result = subexpr(0);
- if(_index < _operands.length)
- throw new SyntaxErrorException("Zbyt dlugie wyrazenie");
- return result;
- }
- }
- public class Kalkulator {
- static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- static String read(){
- try {
- return in.readLine();
- } catch (IOException ioe) {
- System.out.println(ioe.getMessage());
- System.exit(1);
- }
- return null;
- }
- public static void main(String args[]){
- String expr;
- while((expr = read()) != null){
- System.out.print("= ");
- try {
- System.out.println(Parser.parse(expr));
- } catch(Parser.SyntaxErrorException see) {
- System.out.println(see.getMessage());
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment