Advertisement
Guest User

2

a guest
Aug 20th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. public boolean checking(){ //Конечный автомат для проверки правильности введенного выражения
  2.         int numOfLexem = 0;
  3.         int balance = 0;
  4.         for(Lexem l : RPN){
  5.             numOfLexem++;
  6.             switch (currentState) {
  7.            
  8.             case BEGIN:
  9.                 System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
  10.                 switch(l.type){
  11.                 case LEFT:
  12.                     currentState = State.BEGIN;
  13.                     balance++;
  14.                     break;
  15.                 case RIGHT:
  16.                 case UNARY:
  17.                     currentState = State.ERROR;
  18.                     break;
  19.                 case OPERATION:
  20.                     currentState = State.OPERATION;
  21.                     l.type = LexemType.UNARY;
  22.                     break;
  23.                 case FUNCTION:
  24.                     currentState = State.FUNCTION;
  25.                     break;
  26.                 case NUMBER:
  27.                 case CONSTANT:
  28.                     currentState = State.OPERAND;
  29.                     break;
  30.                 }
  31.                 break;
  32.                
  33.             case FUNCTION:
  34.                 System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
  35.                 switch(l.type){
  36.                 case LEFT:
  37.                     balance++;
  38.                     currentState = State.BEGIN;
  39.                     break;
  40.                 case RIGHT:
  41.                 case UNARY:
  42.                 case OPERATION:
  43.                 case FUNCTION:
  44.                 case NUMBER:
  45.                 case CONSTANT:
  46.                     currentState = State.ERROR;
  47.                     break;
  48.                 }
  49.                 break;
  50.                
  51.             case OPERATION:
  52.                 System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
  53.                 switch (l.type) {
  54.                 case LEFT:
  55.                     balance++;
  56.                     currentState = State.BEGIN;
  57.                     break;
  58.                 case RIGHT:
  59.                 case UNARY:
  60.                 case OPERATION:
  61.                     currentState = State.ERROR;
  62.                     break;
  63.                 case FUNCTION:
  64.                     currentState = State.FUNCTION;
  65.                     break;
  66.                 case NUMBER:
  67.                 case CONSTANT:
  68.                     currentState = State.OPERAND;
  69.                     break;
  70.                 }
  71.                 break;
  72.            
  73.             case OPERAND:
  74.                 System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
  75.                 switch (l.type){
  76.                 case LEFT:
  77.                 case NUMBER:
  78.                 case CONSTANT:
  79.                 case FUNCTION:
  80.                 case UNARY:
  81.                     currentState = State.ERROR;
  82.                     break;
  83.                 case OPERATION:
  84.                     currentState = State.OPERATION;
  85.                     break;
  86.                 case RIGHT:
  87.                     balance--;
  88.                     currentState = State.OPERAND;
  89.                 }
  90.                 break;
  91.  
  92.             default:
  93.                 break;
  94.             }
  95.             if(currentState == State.ERROR){
  96.                 System.out.println("Incorrect syntax at lexem (num:" + numOfLexem + ") :  " + l);
  97.                 for(Lexem lp : RPN){
  98.                     if(lp == l)
  99.                         System.out.print(" `" + lp.toString(false) + "` ");
  100.                     else
  101.                         System.out.print(lp.toString(false));
  102.                 }
  103.                 return false;
  104.             }
  105.             if(balance!=0){
  106.                 System.out.println("Check brackets!" + numOfLexem);
  107.                 return false;
  108.             }
  109.         }
  110.         return true;
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement