1 / 11

STACKS AND COMPILERS

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

brenna
Télécharger la présentation

STACKS AND COMPILERS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. STACKS AND COMPILERS Group Members: Chia-Ling Chen Gayathri Venkataramani

  2. STACKS AND COMPILERS • Stack is an ADT with a designated end called top. • Operations: • push() • pop() • peep() • isEmpty()

  3. 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.

  4. 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.

  5. STACKS AND COMPILERS • Simple Infix - postfix expressions:

  6. 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

  7. STACKS AND COMPILERS • Infix: (4 + 5 * 6) * 7 • Postfix: 4 5 6 * + 7 * * + ( + ( 4 + ( 5 ( + ( 4 5 * * + ( 7 *+ 6 * * * ) * 7 6

  8. 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;

  9. 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("-"); • }

  10. 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;

  11. 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; } }

More Related