Fall, 2015 Prof. Jungkeun Park

Similar documents
Infix to Postfix Conversion

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

STACKS AND QUEUES CHAPTER 3

Chapter3 Stacks and Queues

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

CHAPTER 3 STACKS AND QUEUES

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Informatics Ingeniería en Electrónica y Automática Industrial

Data Structure - Stack and Queue-

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues

Expressions and Precedence. Last updated 12/10/18

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Review of the C Programming Language for Principles of Operating Systems

Information Science 1

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Lecture 02 C FUNDAMENTALS

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Operators and Expressions:

Operators. Java operators are classified into three categories:

JAVA OPERATORS GENERAL

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Review of the C Programming Language

Operators & Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

Prof. Navrati Saxena TA: Rochak Sachan

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

A flow chart is a graphical or symbolic representation of a process.

Operators in java Operator operands.

Data Types and Variables in C language

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Department of Computer Science

Principles of Programming Languages COMP251: Syntax and Grammars

Structures, Operators

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Arithmetic Operators. Portability: Printing Numbers

Engineering Computing I

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Operators in C. Staff Incharge: S.Sasirekha

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

UNIT 3 OPERATORS. [Marks- 12]

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Programming for Engineers Iteration

CSE 230 Intermediate Programming in C and C++

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Expression and Operator

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Fundamentals of Programming

Computers Programming Course 6. Iulian Năstac

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Embedded Controller Programming 2

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Prepared by: Shraddha Modi

Fundamental of Programming (C)

C Programming Language (Chapter 2 of K&R) Variables and Constants

C/C++ Programming Lecture 7 Name:

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

UNIT- 3 Introduction to C++

Variables and Operators 2/20/01 Lecture #

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

The New C Standard (Excerpted material)

Data Structures Week #3. Stacks

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Java Notes. 10th ICSE. Saravanan Ganesh

More Programming Constructs -- Introduction

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

A Simple Syntax-Directed Translator

Fundamentals of Programming Session 20

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Data types, variables, constants

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 3: Operators, Expressions and Type Conversion

Left to right design 1

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Programming and Data Structures

CSC 1214: Object-Oriented Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

C Programming Class I

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

OBJECT ORIENTED PROGRAMMING

Formal Languages and Automata Theory, SS Project (due Week 14)

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Discussion of project # 1 permutations

BBM 201 DATA STRUCTURES

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

3. Java - Language Constructs I

CSE 401 Midterm Exam 11/5/10

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Transcription:

Data Structures t and Algorithms Stacks Application Infix to Postfix Conversion Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea in Univ. of North Texas. http://www.cs.unt.edu/~rada/csce3110

Applications Infix to Postfix conversion o [Evaluation of Expressions] 2

Evaluation of Expressions X = a / b - c + d * e - a * c a = 4, b = c = 2, d = e = 3 Interpretation 1: ((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1 Interpretation 2: (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.666662 (3 4) 2 (4/3) ( 2 2 How to generate the machine instructions corresponding to a given expression? precedence rule + associative rule 3

Token Operator Precedence 1 Associativity ( ) function call 17 left-to-right [ ] ->. array element struct or union member -- ++ increment, decrement 2 16 left-to-right -- ++ decrement, increment 3 15 right-to-left! - -+ &* logical not one s complement unary minus or plus address or indirection sizeof size (in bytes) (type) type cast 14 right-to-left * / % mutiplicative 13 Left-to-right + - binary add or subtract 12 left-to-right << >> shift 11 left-to-rightto 4

> >= relational 10 left-to-right < <= ==!= equality 9 left-to-right & bitwise and 8 left-to-right ^ bitwise exclusive or 7 left-to-right bitwise or 6 left-to-right && logical and 5 left-to-right logical or 4 left-to-right?: conditional 3 right-to-left = += -= assignment 2 right-to-left /= *= %= <<= >>= &= ^= =, comma 1 left-to-right to 5

Infix user 2+3*4 a*b+5 (1+2)*7 a*b/c (a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c Postfix compiler 234*+ ab*5+ 12+7* ab*c/ abc-d+/ea-*c* ab/c-de*ac*- Postfix: no parentheses, no precedence 6

Token Stack Top [0] [1] [2] 6 6 0 2 / 3 6 6/2 6/2 2 3 1 0 1-4 2 6/2-3 6/2-3 6/2-3 4 4 2 0 1 2 * + 6/2-3 6/2-3+4*2 4*2 1 0 7

Infix to Postfix Assumptions: operators: +, -, *, /, % operands: single digit integer #define MAX_STACK_SIZE 100 /* maximum stack size */ #define MAX_EXPR_SIZE 100 /* max size of expression */ typedef enum{1paran, rparen, plus, minus, times, divide, mod, eos, operand precedence; int stack[max_stack_size]; _ /* global stack */ char expr[max_expr_size]; /* input string */ 8

Evaluation of Postfix Expressions int eval(void) { /* evaluate a postfix expression, expr, maintained as a global variable, \0 is the the end of the expression. The stack and top of the stack are global variables. get_token is used to return the token type and the character symbol. Operands are assumed to be single character digits */ precedence token; char symbol; int op1, op2; int n = 0; /* counter for the expression string */ int top = -1; token = get_token(&symbol, &n); while (token!= eos) { if (token == operand) push(&top, symbol- 0 ); /* stack insert */ 9

else { /* remove two operands, perform operation, and return result to the stack */ op2 = pop(&top); /* stack delete */ op1 = pop(&top); switch(token) { case plus: push(&top, op1+op2); break; case minus: push(&top, op1-op2); break; case times: push(&top, op1*op2); break; case divide: push(&top, op1/op2); break; case mod: push(&top, op1%op2); token = get_token (&symbol, &n); return pop(&top); /* return result */ 10

precedence get_token(char *symbol, int *n) { /* get the next token, symbol is the character representation, which is returned, the token is represented by its enumerated value, which is returned in the function name */ *symbol =expr[(*n)++]; switch (*symbol) { case ( : return lparen; case ) : return rparen; case + : return plus; case - : return minus; case / : return divide; case * : return times; case % : return mod; case \0 : return eos; default : return operand; /* no error checking, default is operand */ 11

Infix to Postfix Conversion (Intuitive Algorithm) (1) Fully parenthesized expression a / b - c + d * e - a * c --> ((((a / b) - c)+(d*e)) (a * c)) (2) () All operators replace their corresponding right parentheses. ((((a / b) - c) + (d * e)) (a * c)) / - + * - * (3) Delete all parentheses. ab/c-de*+ac*- two passes 12

The orders of operands in infix and postfix are the same. a + b * c, * > + Token Stack Top Output t [0] [1] [2] a + b * c eos + -1 + 0 + * 0 + * 1 1-1 a a ab ab abc abc*= 13

a * 1 (b +c) * 2 d Token Stack [0] [1] [2] Top * a -1 * 1 *1 1 ( 0 ( * 1 ( 1 1 b * 1 ( + 1 + 1 * ( + 2 c * 1 2 ) * 2 match ) 0 * 2 * 2 * 0 1 = * 2 d * 2 0 eos 0 Output a a a ab ab abc abc+ abc+* 1 abc+* 1 d abc+* 1 d* 2 14

Rules (1) Operators are taken out of the stack as long as their in-stack precedence is higher than or equal to the incoming precedence of the new operator. (2) () ( has low in-stack precedence, and high incoming precedence. ( ) + - * / % eos isp 0 19 12 12 13 13 13 0 icp 20 19 12 12 13 13 13 0 15

precedence stack[max_stack_size]; /* isp and icp arrays -- index is value of precedence lparen, rparen, plus, minus, times, divide, mod, eos */ static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0; static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0; isp: in-stack precedence icp: incoming precedence 16

Infix to Postfix void postfix(void) { /* output the postfix of the expression. The expression string, the stack, and top are global */ char symbol; precedence token; int n = 0; int top = 0; /* place eos on stack */ stack[0] = eos; for (token = get_token(&symbol, token(&symbol &n); token!= eos; token = get_token(&symbol, &n)) { if (token == operand) printf ( %c, symbol); else if (token == rparen ){ 17

Infix to Postfix (cont d) /*unstack tokens until left parenthesis */ while (stack[top]!= lparen) print_token(delete(&top)); pop(&top); p p /*discard the left parenthesis */ else{ /* remove and print symbols whose isp is greater than or equal to the current token s icp */ while(isp[stack[top]] >= icp[token] ) print_token(delete(&top)); push(&top, token); while ((token = pop(&top))!= eos) print_token(token); token(token); print( \n ); 18