13. parser

# 13. parser - CPSC 221 Data Structures Algorithms Parsing...

This preview shows pages 1–6. Sign up to view the full content.

CPSC 221 Data Structures & Algorithms Parsing and Evaluating Arithmetic Expressions In infix notation an arithmetic operator ( + , - , * , / ) appears between two operands (numbers, variables) to which it is being applied. Infix notation may require parentheses to specify a desired order of operations. E XAMPLE : A * ( B + C ) / ( D - F ) . In postfix notation (also RPN or Reverse Polish Notation) arithmetic operator is placed directly after two operands to which it applies. It does not require parentheses. E XAMPLE : ABC + * DF - / . In prefix notation arithmetic operator is placed directly before two operands to which it applies. It does not require parentheses. E XAMPLE : / * A + BC - DF . c circlecopyrt Teresa Leyk Slide 1 Parsing & Evaluating Expressions

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

View Full Document
CPSC 221 Data Structures & Algorithms An algorithm of converting infix notation to postfix notation: 1. Completely parenthesize the infix expression to specify the order of all operations. 2. Move each operator to the space held by its corresponding right (closing) parenthesis. 3. Remove all parentheses. To find a value of an arithmetic expression, a compiler must 1. Convert the expression from infix form into postfix form. 2. Apply an evaluation algorithm to the postfix form. c circlecopyrt Teresa Leyk Slide 2 Parsing & Evaluating Expressions
CPSC 221 Data Structures & Algorithms Infix to Postfix Conversion We need the following class members. 1. str is an object of class string containing the infix expression divided into tokens. It consists of individual tokens: (a) operator tokens ( + , - , * ,/, , ) where denotes the exponentiation operator, and is a unary minus (b) operand tokens (variables and constants) (c) left parenthesis token ( LPAREN ) and right parenthesis token ( RPAREN ) (d) end token ( END ) which delimits the end of the infix expression 2. tokens is an array (vector) containing tokens from the input string str . 3. opStack is a stack (of integers) containing operator tokens, LPAREN , and END . c circlecopyrt Teresa Leyk Slide 3 Parsing & Evaluating Expressions

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

View Full Document
CPSC 221 Data Structures & Algorithms 4. postfix is a queue (of characters or strings) containing the expression in postfix notation. 5. The function toPostfix converts a given expression (string) in infix form to postfix form. 6. The function printPostfix prints out the expression in postfix form converted by t oPostfix . Note that printPostfix removes tokens from the postfix queue, so after printing the postfix expression the postfix queue is empty. How do you think it can be corrected? c circlecopyrt Teresa Leyk Slide 4 Parsing & Evaluating Expressions
CPSC 221 Data Structures & Algorithms Description of The Algorithm 1. Define a table precTable whose index is a token enumeration (for operators, operands, parentheses, or END token), and which returns an integer (priority). E XAMPLE : Token: + - * / ( ) operand END input priority: 1 1 3 3 6 100 0 0 0 stack priority: 2 2 4 4 5 0 99 0 - 1 2. Initialize opStack by pushing the END token.

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

View Full Document
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