3 get the next token from the infix expression from

Info icon This preview shows pages 5–12. Sign up to view the full content.

View Full Document Right Arrow Icon
3. Get the next token from the infix expression (from tokens ). c circlecopyrt Teresa Leyk Slide 5 Parsing & Evaluating Expressions
Image of page 5

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 4. Test a token and (a) if the token is an operand, enqueue it to postfix . (b) if the token is RPAREN , pop entries from opStack and enqueue them on postfix until a matching LPAREN is popped. Discard both left and right parentheses. (c) if the token is the END token, pop all entries that remain on opStack and enqueue them on postfix . (d) if the token is LPAREN , push it on opStack . (e) otherwise the token is an operator, therefore pop from opStack and enqueue on the postfix queue those operators whose stack priority is greater than or equal to the input priority of the current operator (corresponding to the current token). After popping these operators, the current token is pushed on the stack. (f) repeat the steps 3 and 4 until the token is equal to END . c circlecopyrt Teresa Leyk Slide 6 Parsing & Evaluating Expressions
Image of page 6
CPSC 221 Data Structures & Algorithms Parsing of Infix Expression: An Example Consider the expression in infix form: A * B +( C - D / E ) . token opStack postfix Commentary -------------------------------------------------------- # push END token A dequeue token A enqueue token on postfix * dequeue token #, * push token on stack B dequeue token AB enqueue token on postfix + dequeue token #,+ AB * pop and enqueue * , push + ( dequeue token #,+,( push token C dequeue token AB * C enqueue token on postfix - dequeue token c circlecopyrt Teresa Leyk Slide 7 Parsing & Evaluating Expressions
Image of page 7

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 token opStack postfix Commentary -------------------------------------------------------- #,+,(,- push token D dequeue token AB * CD enqueue token on postfix / dequeue token #,+,(,-,/ push token E dequeue token AB * CDE enqueue token on postfix ) dequeue token #,+ AB * CDE/- pop until ( reached # dequeue token AB * CDE/-+ pop and enqueue the rest of stack on postfix c circlecopyrt Teresa Leyk Slide 8 Parsing & Evaluating Expressions
Image of page 8
CPSC 221 Data Structures & Algorithms Class Parser class Parser { private : // enum constants enum EnumTokens { END, VALUE, LPAREN, RPAREN, EXP, MULT, DIV, PLUS, MINUS, UNARY_MINUS }; static const char * delimiters; LinkedStack opStack; // operator stack LinkedQueue postfix; // postfix queue char curVal; // current value const char * tokens; // array of tokens class Precedence { public : int input, stack; Precedence( int i=0, int s=0) : input(i), stack(s) {} }; static const Precedence precTable[]; //precedence table static const char opTable[]; // conversion table c circlecopyrt Teresa Leyk Slide 9 Parsing & Evaluating Expressions
Image of page 9

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 // class Parser continued private : // utility functions EnumTokens getToken( int c, EnumTokens prevToken = VALUE); public : Parser(string s) : opStack(), postfix() { // constructor tokens = s.c_str(); // convert to a C string opStack.push(END); // initialize opStack } // operations LinkedQueue getPostfix() { return postfix; } void printPostfix(); void toPostfix(); }; c circlecopyrt Teresa Leyk Slide 10 Parsing & Evaluating Expressions
Image of page 10
CPSC 221 Data Structures & Algorithms Class Parser Implementation // initialize constants, delimiters and precedence table const char * Parser::delimiters = "+- * /^() "; const char Parser::opTable[] = { ’\0’, ’$’, ’(’, ’)’, ’^’, ’ * ’, ’/’, ’+’, ’-’, ’~’ }; const Parser::Precedence Parser::precTable[] = { Precedence(0, -1), //END Precedence(0, 0), //VALUE Precedence(100, 0), //LPAREN Precedence(0, 99), //RPAREN Precedence(6, 5), //EXP
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern