110 likes | 297 Views
STACKS AND COMPILERS. Group Members: Chia-Ling Chen Gayathri Venkataramani. STACKS AND COMPILERS. Stack is an ADT with a designated end called top. Operations: push() pop() peep() isEmpty(). STACKS AND COMPILERS. Infix Expressions Example: 1 + 2 * 3
E N D
STACKS AND COMPILERS Group Members: Chia-Ling Chen Gayathri Venkataramani
STACKS AND COMPILERS • Stack is an ADT with a designated end called top. • Operations: • push() • pop() • peep() • isEmpty()
STACKS AND COMPILERS • Infix Expressions • Example: 1 + 2 * 3 • This expression should mathematically produce result 7 since multiplication has precedence over addition. • A simple left to right operation however would produce the result 9. • A more complicated infix expression can be even more difficult to evaluate.
STACKS AND COMPILERS • Adding parenthesis in a complicated infix expression can make the order of evaluation unambiguous but not necessarily clearer. • Eg: (1-2) – ((((4^5) *3) *61 (7^ (2^2) )) • The above expression is not very clear, therefore we can use a postfix expression which does not require precedence rules and provides direct mechanism for evaluation.
STACKS AND COMPILERS • Simple Infix - postfix expressions:
STACKS AND COMPILERS • Infix to postfix conversion • Summary of various cases in the operator precedence parsing algorithm: • Operands: Immediately Output. • Close parenthesis: Pop stack symbols until an open parenthesis appears. • Operators: Pop all stack symbol of lower precedence or a right-associative symbol of equal precedence appears. The push the operator. • End of Input: Pop all remaining stack symbols
STACKS AND COMPILERS • Infix: (4 + 5 * 6) * 7 • Postfix: 4 5 6 * + 7 * * + ( + ( 4 + ( 5 ( + ( 4 5 * * + ( 7 *+ 6 * * * ) * 7 6
STACKS AND COMPILERS • Case Study: • /** • * After a token is read, use operator precedence parsing • * algorithm to process it; missing opening parentheses • * are detected here. • */ • private void processToken( Token lastToken ) • { • int topOp; • int lastType = lastToken.getType( ); //returns the type • int val1; • switch( lastType ) • { • case VALUE: • //adds to the end of queue. • postfixQue.addLast( new Long( lastToken.getValue( ) )); • return;
STACKS AND COMPILERS • Case Study: • case CPAREN: //extract till the opening parenthesis. • while( ( topOp = opStackTop( ) ) != OPAREN && topOp != EOL ) • { • val1 = ((Integer) (opStack.pop( ))).intValue() ; • if (val1 == 4) //add to que: operator corresponding to token value • postfixQue.addLast("^"); • if (val1 == 5) • postfixQue.addLast("*"); • if (val1 == 6) • postfixQue.addLast("/"); • if (val1 == 7) • postfixQue.addLast("+"); • if (val1 == 8) • postfixQue.addLast("-"); • }
STACKS AND COMPILERS • Case Study: if( topOp == OPAREN ) opStack.pop( ); // Get rid of opening parentheseis else System.err.println( "Missing open parenthesis" ); break; case EOL: //adding rest of the contents from opstack once infix expression is over while (!opStack.isEmpty()) { val1 = ((Integer) (opStack.pop( ))).intValue() ; if (val1 == 4) postfixQue.addLast("^"); if (val1 == 5) postfixQue.addLast("*"); if (val1 == 6) postfixQue.addLast("/"); if (val1 == 7) postfixQue.addLast("+"); if (val1 == 8) postfixQue.addLast("-"); } break;
STACKS AND COMPILERS • Case Study: default: // General operator case while( precTable[ lastType ].inputSymbol <= precTable[ topOp = opStackTop( ) ].topOfStack ) { val1 = ((Integer) (opStack.pop( ))).intValue() ; if (val1 == 4) postfixQue.addLast("^"); if (val1 == 5) postfixQue.addLast("*"); if (val1 == 6) postfixQue.addLast("/"); if (val1 == 7) postfixQue.addLast("+"); if (val1 == 8) postfixQue.addLast("-"); } if( lastType != EOL ) opStack.push( new Integer(lastType) ); break; } }