{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

3 get the next token from the infix expression from

Info iconThis 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
Background 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
Background 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
Background 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
Background 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
Background 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
Background 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
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
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}