CSC
13. parser

# Precedence3 4 mult precedence3 4 div precedence1 2

• Notes
• 26
• 100% (1) 1 out of 1 people found this document helpful

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

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

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

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
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

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

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
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

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

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
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

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

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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern