stacks - 3

21
Stacks - 3 Nour El-Kadri ITI 1121

Upload: tam

Post on 31-Jan-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Stacks - 3. Nour El-Kadri ITI 1121. Evaluating arithmetic expressions. Stack-based algorithms are used for syntactical analysis (parsing). For example to evaluate the following expression: 1+2 * 3 − 4 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Stacks - 3

Stacks - 3

Nour El-KadriITI 1121

Page 2: Stacks - 3

2

Evaluating arithmetic expressions• Stack-based algorithms are used for

syntactical analysis (parsing).• For example to evaluate the following

expression:

1+2 * 3 − 4

• Compilers use similar algorithms to check the syntax of your programs and generate machine instructions (executable).

• To verify that parentheses are balanced: ’([])’ is ok, but not ’([)]’ or ’)((())(’.

Page 3: Stacks - 3

3

• The first two steps of the analysis of a source program by a compiler are the lexical analysis and the syntactical analysis.

• During the lexical analysis (scanning) the source code is read from left to right and the characters are regrouped into tokens, which are only successive characters that constitute a number or an identifier. Normally, the lexical analysis removes spaces from the input.

E.g.:

·10 ·+· ·2+· · ·300where “·” represent blank spaces, is transformed into the following list of tokens:

[10,+,2,+,300]• The next step is the syntactical analysis (parsing) and

consists of regrouping the tokens into grammatical units, for example the sub-expressions of RPN expressions (seen in class this week).

Page 4: Stacks - 3

4

Evaluating an arithmetic expressionLeft-to-right algorithm:

Declare L, R and OPRead LWhile not end-of-expressiondo:Read OPRead REvaluate L OP RStore result in L

at the end of the loop the result can be found in L.

Page 5: Stacks - 3

5

LimitationWithout parentheses the expression cannot be evaluated

correctly: 7 - (3 - 2)because the result of the left-to-right analysis correspond

to: (7 - 3) – 2

similarly the following expression cannot be evaluated by our simple algorithm:

7 - 3 * 2since the left-to-right analysis correspond to: (7 - 3) * 2but according to operator precedence: 7 - (3 * 2)

Page 6: Stacks - 3

6

RemarksThe left-to-right algorithm:• does not handle parentheses,• nor precedence.

2 solutions:• use a different notation,• develop more complex algorithms.

both solutions involve stacks.

Page 7: Stacks - 3

7

Notations• There are 3 ways to represent the following expression:

L OP R

• infix: this is the usual notation, the operator is sandwiched in between its operands: L OP R,

• postfix: in postfix notation, the operands are placed before the operator, L R OP. This notation is also called Reverse Polish Notation or RPN, it’s the notation used by certain scientific calculators (such as the HP-35 from Hewlett-Packard or the Texas Instruments TI-89 using the RPN Interface by Lars Frederiksen*) or PostScript programming language.

• 7 - (3 - 2) = 7 3 2 - -• (7 - 3) - 2 = 7 3 - 2 –

* www.calculator.org/rpn.html

Page 8: Stacks - 3

8

• prefix: the third notation consists of placing the operator before the operands, OP L R. The programming language Lisp uses a combination of parentheses and prefix notation: (- 7 (* 3 2)).

Page 9: Stacks - 3

9

mini-plan

1. Evaluating a postfix expression: L R OP,

2. Transforming a postfix expression into an infix: L R OP L OP R,

3. Evaluating an infix expression: L OP R

(with parentheses but no operator precedence).

Page 10: Stacks - 3

10

Evaluating a postfix expression

Until the end of the expression has been reached:

1. from left to right until the first operator,2. apply the operator to the two preceding

operands,3. replace the operator and its operands by

the result,

at the end we have result.

Page 11: Stacks - 3

11

Page 12: Stacks - 3

12

Remarks: infix vs. postfix• The order of the operands is the same for

both notations, however operators are inserted at different places:2 + (3 * 4) = 2 3 4 * +

(2 + 3) * 4 = 2 3 + 4 *

• Evaluating an infix expression involves handling operators precedence and parenthesis – in the case of the postfix notation, those two concepts are embedded in the expression, i.e. the order of the operands and operators.

Page 13: Stacks - 3

13

Evaluating a postfix expression• The algorithm necessitates a stack, Numbers, a variable

that contains the last element that was read, X, and two more variables, L and R, whose purpose is the same as before.Numbers = [

While not end-of-expressiondo:

Read XIf X isNumber, PUSH X onto NumbersIf X isOperator,

R = POP Numbers (right before left?!)L = POP NumbersEvaluate L X R; PUSH result onto Numbers

• To obtain the final result: POP Numbers.

Page 14: Stacks - 3

14

Problem

• Rather than evaluating an RPN expression, we would like to convert an RPN expression to infix (usual notation).

Do we need a new algorithm?• No, a simple modification will do, replace

“Evaluate L OP R” by “Concatenate (L OP R)”.

• Note: parentheses are essential (not all of them but some are).

• This time the stack does not contain numbers but character strings that represent sub-expressions.

Page 15: Stacks - 3

15

Postfix infixString rpnToInfix(String[] tokens)

Numbers = [X =L =R =

While not end-of-expressiondo:

Read XIf X isNumber, PUSH X onto NumbersIf X isOperator,

R = POP NumbersL = POP NumbersConcatenate ( L X R ); PUSH result onto Numbers

Page 16: Stacks - 3

16

Postfix ?While not end-of-expressiondo:

Read XIf X isNumber, PUSH X onto NumbersIf X isOperator,

R = POP NumbersL = POP NumbersProcess L X R; PUSH result onto Numbers

• We’ve seen an example where ’Process == Evaluate’, then one where ’Process == Concatenate’, but Process could also produce assembly code (i.e. machine instructions).

• This shows how programs are compiled or translated.

Page 17: Stacks - 3

17

Evaluating an infix expression• This algorithm handles parentheses

but not operator precedence (a first implementation that does not take the precedence of the operators into account).

• The algorithm assume that expressions are free of errors.

• Necessitates two stacks, S and T (a temporary stack to evaluate sub-expressions).

Page 18: Stacks - 3

18

Evaluate infix

1. Get the next token,2. If not ’)’, push the token onto S.3. If ’)’ or the end of the expression

(process subexpression):(a) until ’(’ or S is empty: remove the top element

of S, push it onto T unless it’s ’(’.(b) Process the sub-expression: “evaluate the

content of T”.(c) Push the result onto S.

Until the end of the expression.

Page 19: Stacks - 3

19

Evaluate infixEvaluate the content of T:

1. Remove the top element of T, save it into L

2. While T is not empty:(a) Remove the top element of T, save it

into OP,(b) Remove the top element of T, save it

into R,(c) “Evaluate” L OP R, save the result onto

L.

Page 20: Stacks - 3

20

infix postfix

• The algorithm for translating an infix expression to postfix uses a stack to hold the operators and parentheses.

• Initialize an empty (operators) stack• Initialize an empty postfix expression

(PE)

Page 21: Stacks - 3

21

From left to right:• Read the next token• If token is an operand then append it to PE• If token is an operator then

while top element has the same or higher priority pop the top element and append it to PE

push token onto the operators stack

• If token is left parenthesis push onto stack• If token is right parenthesiswhile top element is different from left parenthesis

pop top element and append it to PEpop left parenthesis (and discard)

while stack is not emptypop the top element and append it to PE