{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

This preview shows pages 1–13. Sign up to view the full content.

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.

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

View Full Document
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]

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

View Full Document
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

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

View Full Document
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; }

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

View Full Document
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 Left-to-right algorithm: Declare L, R and OP Read L While not end-of-expression do: Read OP Read R Evaluate L OP R Store result in L At the end of the loop the result can be found in L.

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

View Full Document
3 * 8 - 10
3 + 4 - 5 ^ L = 3 OP = R = > Read L While not end-of-expression do: Read OP Read R Evaluate L OP R Store result in L

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

View Full Document
3 + 4 - 5 ^ L = 3 OP = + R = Read L While not end-of-expression do: > Read OP Read R Evaluate L OP R Store result in L
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 106

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

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

View Full Document
Ask a homework question - tutors are online