CS200 Lab 3, Scanning and Parsing
Overview
The goals of this lab are:
- Using the Scanner class for some low-level Parsing
- Going over pre- and post- conditions
- Going over some exercises from Rosen pertaining to grammars.
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 integers. Thus,
"* a b" is no longer legal. "* 10 3" will now be legal and "* 10 3 2"
will be illegal.
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 file.
Afterward, go over the additional exercises on your own.
© 2014 CS200 Colorado State University. All Rights Reserved.