Evaluation of Expressions

Slides:



Advertisements
Similar presentations
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Advertisements

Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Stacks Example: Stack of plates in cafeteria.
Yinchong Han Linjia Jiang. Introduction There are three forms of expressions which are prefix, infix and postfix notation. The project will evaluate infix.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Arithmetic Expressions
Department of Technical Education Andhra Pradesh
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Trees Nature Lover’s View Of A Tree root branches leaves.
Infix, Postfix, Prefix.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
CS Data Structures Chapter 3 Stacks and Queues.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Trees Nature Lover’s View Of A Tree root branches leaves.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stacks  Introduction  Applications  Implementations  Complex Applications.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Stack Applications Qamar Rehman.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Stack Applications Reading: Chap. 3 Weiss.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Stacks.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures 5th Week
COMPUTER 2430 Object Oriented Programming and Data Structures I
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
Algorithms and Data Structures
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
STACK IMPLEMENTATION Adam M.B..
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
CSCE 3110 Data Structures & Algorithm Analysis
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
การวิเคราะห์และออกแบบขั้นตอนวิธี
OPERATORS in C Programming
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Evaluation of Expressions

Arithmetic Expressions (a + b) * (c + d) + e – f/g*h + 3.25 Expressions comprise three kinds of entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.). Delimiters ((, )).

Operator Degree Number of operands that the operator requires. Binary operator requires two operands. a + b c / d e - f Unary operator requires one operand. + g - h

Infix Form Normal way to write an expression. Binary operators come in between their left and right operands. a * b a + b * c a * b / c (a + b) * (c + d) + e – f/g*h + 3.25

Operator Priorities How do you figure out the operands of an operator? a + b * c a * b + c / d This is done by assigning operator priorities. priority(*) = priority(/) > priority(+) = priority(-) When an operand lies between two operators, the operand associates with the operator that has higher priority.

Evaluation Expression in C++ When evaluating operations of the same priorities, it follows the direction from left to right. C++ treats Nonzero as true zero as false !3&&5 +1 0 Priority Operator 1 Unary minus, ! 2 *, /, % 3 +, - 4 <, <=, >=, > 5 == (equal), != 6 && (and) 7 || (or)

In Class Exercise x=6, y=5 10+x*5/y+1 (x>=5)&&y<10 !x>10+!y

Tie Breaker When an operand lies between two operators that have the same priority, the operand associates with the operator on the left. a + b - c a * b / c / d

Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)

Infix Expression Is Hard To Parse Need operator priorities, tie breaker, and delimiters. This makes computer evaluation more difficult than is necessary. Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier for a computer to evaluate expressions that are in these forms.

Postfix Form The postfix form of a variable or constant is the same as its infix form. a, b, 3.25 The relative order of operands is the same in infix and postfix forms. Operators come immediately after the postfix form of their operands. Infix = a + b Postfix = ab+

Postfix Examples Infix = a + b * c Infix = a * b + c Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + /

Unary Operators Replace with new symbols. + a => a @ + a + b => a @ b + - a => a ? - a-b => a ? b -

Postfix Notation Expressions are converted into Postfix notation before compiler can accept and process them. X = A / B – C + D * E – A * C Infix => A / B – C + D * E – A * C Postfix => A B / C – D E * + A C * - (Operators come in-between operands) (Operators come after operands) Operation Postfix T1 = A / B T1 C – D E * + A C * - T2 = T1 - C T2 D E * + A C * - T3 = D * E T2 T3 + A C * - T4 = T2 + T3 T4 A C * - T5 = A * C T4 T5 - T6 = T4 - T5 T6

Postfix Evaluation Scan postfix expression from left to right pushing operands on to a stack. When an operator is encountered, pop as many operands as this operator needs; evaluate the operator; push the result on to the stack. This works because, in postfix, operators come immediately after their operands.

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a b + c d - * e f + / a b + c d - * e f + / b a

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a b + c d - * e f + / d a b + c d - * e f + / c a b + c d - * e f + / (a + b) a b + c d - * e f + / a b + c d - * e f + /

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a b + c d - * e f + / (c – d) (a + b)

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack

Infix to Postfix The order of the operands in both form is the same. An algorithm for producing postfix from infix: Fully parenthesize the expression. Move all operators so that they replace their corresponding right parentheses. Delete all parentheses.

Infix to Postfix For example: A/B-C+D*E-A*C Fully parenthesize the expression. ((((A/B)-C)+(D*E))-(A*C)) Move all operators so that they replace their corresponding right parentheses. ((((AB/)C-)(DE*)+)(AC*)-) Delete all parentheses. AB/C-DE*+AC*-

In Class Exercise Write the postfix form: A&&B+C*D

Infix to Postfix We scan an expression for the first time, we can form the postfix by immediately passing any operands to the output. For example: A+B*C => ABC*+ Next token Stack Output None Empty A + B AB * +* C ABC Since * has higher priority, we should stack *.

Infix to Postfix Example: A*(B+C)*D => ABC+*D* When we get ‘)’, we want to unstack down to the corresponding ‘)’ and then delete the left and right parentheses. Next token Stack Output None Empty A * ( *( B AB + *(+ C ABC ) ABC+ ABC+* D ABC+*D Done ABC+*D*

Infix to Postfix These examples motivate a priority-based scheme for stacking and unstacking operators. When the left parenthesis ‘(‘ is not in the stack, it behaves as an operator with high priority. whereas once ‘(‘ gets in, it behaves as one with low priority (no operator other than the matching right parenthesis should cause it to get unstacked) Two priorities for operators: isp (in-stack priority) and icp (in-coming priority) The isp and icp of all operators in Figure 3.15(or table in p.2 of this lecture) remain unchanged. We assume that isp(‘(‘) = 8 (the lowest), icp(‘(‘) = 0 (the highest), and isp(‘#’) = 8 (#  the last token)

Infix to Postfix Result rule of priorities: Operators are taken out of the stack as long as their isp is numerically less than or equal to the icp of the new operator.

Analysis of Postfix The function makes only a left-to-right pass across the input. The complexity of Postfix is Θ(n), where n is the number of tokens in the expression. The time spent on each operands is O(1). Each operator is stacked and unstacked at most once. Hence, the time spent on each operator is also O(1)

Prefix Form The prefix form of a variable or constant is the same as its infix form. a, b, 3.25 The relative order of operands is the same in infix and prefix forms. Operators come immediately before the prefix form of their operands. Infix = a + b Postfix = ab+ Prefix = +ab

Homework Sec. 3.7 Exercise 3 (Page 166) Convert infix expressions to prefix expressions