Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean checking(){ //Конечный автомат для проверки правильности введенного выражения
- int numOfLexem = 0;
- int balance = 0;
- for(Lexem l : RPN){
- numOfLexem++;
- switch (currentState) {
- case BEGIN:
- System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
- switch(l.type){
- case LEFT:
- currentState = State.BEGIN;
- balance++;
- break;
- case RIGHT:
- case UNARY:
- currentState = State.ERROR;
- break;
- case OPERATION:
- currentState = State.OPERATION;
- l.type = LexemType.UNARY;
- break;
- case FUNCTION:
- currentState = State.FUNCTION;
- break;
- case NUMBER:
- case CONSTANT:
- currentState = State.OPERAND;
- break;
- }
- break;
- case FUNCTION:
- System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
- switch(l.type){
- case LEFT:
- balance++;
- currentState = State.BEGIN;
- break;
- case RIGHT:
- case UNARY:
- case OPERATION:
- case FUNCTION:
- case NUMBER:
- case CONSTANT:
- currentState = State.ERROR;
- break;
- }
- break;
- case OPERATION:
- System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
- switch (l.type) {
- case LEFT:
- balance++;
- currentState = State.BEGIN;
- break;
- case RIGHT:
- case UNARY:
- case OPERATION:
- currentState = State.ERROR;
- break;
- case FUNCTION:
- currentState = State.FUNCTION;
- break;
- case NUMBER:
- case CONSTANT:
- currentState = State.OPERAND;
- break;
- }
- break;
- case OPERAND:
- System.out.println("Balance: " + balance + " " + currentState + " " + l.type);
- switch (l.type){
- case LEFT:
- case NUMBER:
- case CONSTANT:
- case FUNCTION:
- case UNARY:
- currentState = State.ERROR;
- break;
- case OPERATION:
- currentState = State.OPERATION;
- break;
- case RIGHT:
- balance--;
- currentState = State.OPERAND;
- }
- break;
- default:
- break;
- }
- if(currentState == State.ERROR){
- System.out.println("Incorrect syntax at lexem (num:" + numOfLexem + ") : " + l);
- for(Lexem lp : RPN){
- if(lp == l)
- System.out.print(" `" + lp.toString(false) + "` ");
- else
- System.out.print(lp.toString(false));
- }
- return false;
- }
- if(balance!=0){
- System.out.println("Check brackets!" + numOfLexem);
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement