11Stack - ITI 1121. Introduction to Computing II * Marcel...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 5, 2011 Abstract Abstract data type: Stack Stack-based algorithms * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. 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 )((())(. 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 successive characters that constitute numbers or identifiers. One of the tasks of the lexical analyser is to remove spaces from the input. E.g.: 10 + 2 + 300 where represent blank spaces, is transformed into the following list of tokens: [10,+,2,+,300] The next step is the syntactical analysis ( parsing ) and consists in regrouping the tokens into grammatical units, for example the sub-expressions of RPN expressions (seen in class this week). In the next slides, we look at simple examples of lexical and syntactical analysis. public class Test { public static void scan( String expression ) { Reader reader = new Reader( expression ); while ( reader.hasMoreTokens() ) { System.out.println( reader.nextToken() ); } } public static void main( String args ) { scan( " 3 + 4 * 567 " ); } } // > java Test // INTEGER: 3 // SYMBOL: + // INTEGER: 4 // SYMBOL: * // INTEGER: 567 public class Token { private static final int INTEGER = 1; private static final int SYMBOL = 2; private int iValue; private String sValue; private int type; public Token( int iValue ) { this.iValue = iValue; type = INTEGER; } public Token( String sValue ) { this.sValue = sValue; type = SYMBOL; } public int iValue() { ... } public String sValue() { ... } public boolean isInteger() { return type == INTEGER; } public boolean isSymbol() { return type == SYMBOL; } } LR Scan public static int execute( String expression ) { Token op = null; int l = 0, r = 0; Reader reader = new Reader( expression ); l = reader.nextToken().iValue(); while ( reader.hasMoreTokens() ) { op = reader.nextToken(); r = reader.nextToken().iValue(); l = eval( op, l, r ); } return l; } eval( Token op, int l, int r ) public static int eval( Token op, int l, int r ) { int result = 0; if ( op.sValue().equals( "+" ) ) result = l + r; else if ( op.sValue().equals( "-" ) ) result = l - r; else if ( op.sValue().equals( "*" ) ) result = l * r; else if ( op.sValue().equals( "/" ) ) result = l / r; else System.err.println( "not a valid symbol" ); return result; } Evaluating an arithmetic expression: LR Scan...
View Full Document

This note was uploaded on 03/02/2012 for the course ITI 1121 taught by Professor Samaan during the Winter '10 term at University of Ottawa.

Page1 / 106

11Stack - ITI 1121. Introduction to Computing II * Marcel...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online