Compilers (CS362 2004S)

Tokenizing with java.io.StreamTokenizer

Summary: In today's lab, you will explore a predictive parser for simple expressions that evaluates expressions while parsing them.

Contents:

Preliminaries

Collaboration: Feel free to work on this lab in pairs or trios.

Turning It In: Email me your results.

Grading: I expect that you will gain more from doing this lab than from me grading this lab. I may simply scan through your answers to see if you had any particularly valuable insights.

Supporting Files:

Background

The file expression.ss contains a predictive parser for simple single-digit expressions that evaluates the expressions as it parses them. You should make a copy of the file and read through it to give yourself some sense of the procedures it contains.

Exercises

1. Load expression.ss in DrScheme. Evaluate a variety of expressions to verify that the evaluate procedure works correctly. You should try expressions that involve precedence and associativity. You should also try erroneous expressions.

2. Update expression.ss to support infix max and min operators. You can choose appropriate characters to represent those operators. You may assume that they have the same precedence and associativity as the additive operators.

3. Update expression.ss to return a Scheme expression equivalent to the infix expression, rather than returning the value of the infix expression. One strategy is to change the portions of the code in which a value is computed. Another is to change operator.

4. Update expression.ss to support numbers with more than one digit. You'll probably need a new procedure to parse numbers.

5. Update expression.ss to support exponentiation. You'll need to add a new nonterminal (e.g., ExpExp) and modify code in a variety of places.

 

History

Monday, 30 September 2002 [Samuel A. Rebelsky]

Tuesday, 1 October 2002 [Samuel A. Rebelsky]

Wednesday, 3 March 2004 [Samuel A> Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 5 11:46:46 2004.
The source to the document was last modified on Wed Mar 3 21:04:22 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2004S/Labs/expressions.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu