CS200 Lab 3, Scanning and Parsing

Overview

The goals of this lab are as follows:

The Scanner Class and String Parsing.

The Scanner class does some low-level parsing. It can give you many different things, including tokens, lines, integers, reals, etc.

Create a new project in eclipse, e.g. titled "R3".
Import Parse.java into the src directory of your eclipse project. This class parses simple prefix expressions where "+" , "-" , "*", and "/" are the only legal binary operators and "a" , "b" , and "c" are the only legal operands. For example, "* a b" is legal while "* a b c" is not legal.
Modify this code so that the only legal operands are any integer. Thus, "* a b" is no longer legal. "* 10 3" will now be legal and "* 10 3 2" will be illegal.

Bonus:

  1. Download DequeParse.java
  2. Write a comment above main explaining how it's strategy to parsing is different from the previous example
  3. Change it so that it uses integers as operands too
  4. Implement a new method to recognize postfix, feel free to change names to your liking
  5. Write a comment below main explaining why implementing an infix parser would be different (and harder)

Pre- and Post- Conditions

These are conditions that apply to running a method or a code segment.
The precondition tells users what must be true before they call a method. The post-condition tells users what will be true after they call the method.
These conditions may be "true" meaning that there is no meaningful condition that applies (true is always true).
For example, System.out.println(String s) might have as its precondition that there must be a console to print to, and its post-condition might be that s has been printed.

In Parse.java leave a comment at the top of the "ID(String indent)" method that describes the pre- and post- conditions of this method.

Grammars

The TA will go over a couple exercises on the board from this PDF file. Afterwards go over the additional exercises on your own.
© 2014 CS200 Colorado State University. All Rights Reserved.