1

I have to write a method that will validate a given infix expression from a user input, which will be in a Vector ie.

I already have a portion of the method running which balances opening and closing delimiters, and now i am adding a helper method which checks the syntax of the expression, for example the following cases are what happens when the program is run, including an error:

validate (4+5) 
returns true
validate (45+)
returns false
validate 45+ 
Vector out of range. 

I believe this is happening due to my calls for v.at(i+1).type, but, after adding if statements to return false if my int i = v.size() && tt.type == OPERATOR, to check for the case that the very last token is a operator, it still gave the same vector out of range error.

Any input on why this error would occur?

bool isValArith(vector<Token> v){

    bool lol;
    Token tt;
    stack<Token> st;
    for (int i = 0; i < v.size(); i++){
        tt= v.at(i);
        if (tt.type != OPERATOR && tt.value != "[" && tt.value != "(" && 
                tt.value != "{" &&  tt.value != "}" && tt.value != ")" && 
                tt.value != "]"){
                st.push(tt);
        }

        if (tt.type == OPERATOR){
            if (st.top().type != OPERATOR && v.at(i+1).type != OPERATOR && 
                v.at(i+1).value != "[" && v.at(i+1).value != "{" && 
                v.at(i+1).value != "(" && v.at(i+1).value != "}" &&  
                v.at(i+1).value != "]" && v.at(i+1).value != ")"){
                lol = true;
                cout << "here" << endl;
            }
            else {
                cout << "Error: Invalid Expression" << endl;
                lol = false;
            }
        }
    }
    return lol;
}
2
  • It's not quite clear in your question exactly what is your expected output and what you are considering an error. Please rewrite "validate (4+5) returns true validate (45+) returns false validate 45+ Vector out of range" to make it clear which of these results you are and are not expecting. Sep 24, 2013 at 21:03
  • I dont want the vector to be out of range, i want it to also return false while validating 45+ , and am at a loss for a solution Sep 24, 2013 at 21:26

1 Answer 1

3

When you try to process an OPERATOR you look forward 1 element in your token vector. What you need to do is check to make sure you do not reference off the end of the token vector. Add an explicit check, something like:

if (i+1 >= v.size()) {
// code to handle final token being an operator
}

before the if statement that checks the next token.

2
  • Right, i understand thats the problem, but i have tried to fix it using an if statement and it wont work, im asking for suggestions on how to make the fix so that i can still have the working functionality but including if the last element in the vector is an operator Sep 24, 2013 at 21:20
  • 2
    @JakeDuncan: Fair warning... Your code is already too complicated, and this will complicate it even further. Instead of trying to represent every possible permutation of brackets, braces and parens in your code directly, consider a stack-based approach using tokens. Sep 24, 2013 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.