Programming assignment 2

To turn in this assignment, please submit a single zip file of your Java file(s) to Moodle.
Question 0.1
Download the JavaAnalyzer code provided for this assignment. Try to read and understand the code as much as possible before moving on.
Question 0.2
Read and understand the first few screens of Sun's java tutorial on regular expressions. At a minimum, you need to compile and run the test harness they provide, and understand the sections on string literals, predefined character classes, and boundary matchers. (Thanks to Russell Toris, who provided a new version of the test harness that compiles on the Macs in Tome.) You also need to understand the use of the backslash character as an escape character in java regular expressions. Specifically, if you don't understand the use of double backslashes in the code Pattern.compile(console.readLine("\\s\\w*\\s"), or the actual meaning of this regular expression, please seek help from the instructor or a classmate.
Question 1. (80 points.)
As stated in the JavaDoc for the JavaAnalyzer class, a typical output is:

fields:
line 14: private String label;

constructors:
line 27: public DfaVertex(String label)

methods:
line 43: public void addTransition(String symbol, String dest)
line 57: public String getTransition(String symbol)
line 64: public String getLabel()

(This output was produced by using the input file DfaVertex.java from programming assignment 1.)

Let us agree to call a field, constructor, or method ordinary if it is declared using the simplest possible set of keywords: an access modifier, a datatype or return type if required, a name, and a parameter list if required. Specifically, ordinary fields and methods do not have additional keywords such as static or final. Additionally, ordinary declarations don't have any comments or other tricky syntax. For this question, you need only find matches for the ordinary declarations. Thus, the task for this question is to add code to the JavaAnalyzer class that will print out all the ordinary declarations in a manner similar to the output given above. You must use regular expressions to find the appropriate lines. To assist with grading, please do not alter any existing code, and only add code in places marked by the string "PROGASSIGNMENT2".

Question 2. (20 points.)
Improve your code from question 1 to cope with declarations that are not ordinary. Try to cover as many cases as you can think of. One obvious thing to account for is the optional use of keywords such as static and final. Also, consider what might happen if a literal string or a single-line comment contained a string that looked exactly like a declaration. You may assume that multi-line comments (i.e. /* ... */) are not present, and that declarations do not span multiple lines.
Extra credit. (20 points.)
(If you attempt the extra credit, please submit the attempt as a separate file.) Alter the code so that it works correctly even when multi-line comments are present, and when declarations span multiple lines. Your general approach should still use regular expressions, but don't worry too much about efficiency or memory limitations. In particular, it's fine to slurp the entire input file into memory before starting your processing. (This would be a very bad way to write a compiler, but we are not concerned about that here.) It's fine to edit the existing code for this part of the assignment.