{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Precedence3 4 mult precedence3 4 div precedence1 2

Info iconThis preview shows pages 11–19. Sign up to view the full content.

View Full Document Right Arrow Icon
Precedence(3, 4), //MULT Precedence(3, 4), //DIV Precedence(1, 2), //PLUS Precedence(1, 2), //MINUS Precedence(8, 7) //UNARY_MINUS }; c circlecopyrt Teresa Leyk Slide 11 Parsing & Evaluating Expressions
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms Parser::EnumTokens Parser::getToken( int c, EnumTokens prevToken) { if (c == ’\0’) return END; // token is a delimiter if (c == ’^’) return EXP; else if (c == ’/’) return DIV; else if (c == ’ * ’) return MULT; else if (c == ’(’) return LPAREN; else if (c == ’)’) return RPAREN; else if (c == ’+’) return PLUS; else if (c == ’~’) return UNARY_MINUS; else if (c == ’-’) { if (prevToken == VALUE || prevToken == RPAREN) return MINUS; else return UNARY_MINUS; } curVal = c; // token is a variable or number return VALUE; } c circlecopyrt Teresa Leyk Slide 12 Parsing & Evaluating Expressions
Background image of page 12
CPSC 221 Data Structures & Algorithms void Parser::printPostfix() { LinkedQueue newpost; char item; while (! postfix.isEmpty()) { item = static_cast < char >(postfix.dequeue()); newpost.enqueue(item); cout << item; } cout << endl; postfix = newpost; } c circlecopyrt Teresa Leyk Slide 13 Parsing & Evaluating Expressions
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms void Parser::toPostfix( ) { int op; char c; EnumTokens newToken = END; int tokenIndex = 0; do { //do-while loop c = tokens[tokenIndex++]; while (c == ’ ’) c = tokens[tokenIndex++]; newToken = getToken(c, newToken); switch (newToken) { case VALUE: postfix.enqueue(curVal); break ; c circlecopyrt Teresa Leyk Slide 14 Parsing & Evaluating Expressions
Background image of page 14
CPSC 221 Data Structures & Algorithms // Parser::toPostfix continued case RPAREN: op = opStack.top(); while (op != LPAREN) { postfix.enqueue(opTable[op]); opStack.pop(); op = opStack.top(); } opStack.pop(); //remove LPAREN break ; case END: while (! opStack.isEmpty() ) { op = opStack.top(); postfix.enqueue(opTable[op]); opStack.pop(); } break ; case LPAREN: opStack.push(newToken); break ; c circlecopyrt Teresa Leyk Slide 15 Parsing & Evaluating Expressions
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms // Parser::toPostfix continued case EXP: case DIV: case MULT: case PLUS: case MINUS: case UNARY_MINUS: op = opStack.top(); while (precTable[newToken].input <= precTable[op].stack) { postfix.enqueue(opTable[op]); opStack.pop(); op = opStack.top(); } opStack.push(newToken); break ; default : cerr << "Invalid token: " << newToken << endl; newToken = END; } } while (newToken != END); } c circlecopyrt Teresa Leyk Slide 16 Parsing & Evaluating Expressions
Background image of page 16
CPSC 221 Data Structures & Algorithms #include "Parser.h" #include <iostream> #include <string> using namespace std; int main() { char line[100]; string str; while ( true ) { cout << "Enter expression: "; cin.get(line, 100); cin.ignore(100, ’\n’); str = string(line); //convert line to a string if (str == "quit") break ; cout << "Infix expression: " << str << endl; Parser par(str); // new parser par.toPostfix(); // convert to postfix form par.printPostfix(); // print } return 0; } c circlecopyrt Teresa Leyk Slide 17 Parsing & Evaluating Expressions
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CPSC 221 Data Structures & Algorithms Evaluating Postfix Expressions If we have an expression in postfix form, we can evaluate it substituting for variables their values (if there are any variables) and after evaluation we get a number (value of this expression). To evaluate any expression (say 123 + * 45 - / ) we repeatedly get tokens from the postfix expression.
Background image of page 18
Image of page 19
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}