I have chosen to have your program analyze Java programs, but once you have completed this assignment, you should be able to use the StreamTokenizer class to analyze source code for any programmng language.
The steps to do this include:
cs300> java PrintLexemes junk.java Sample.java tower.lsp Unable to read junk.java File: Sample.java Statement 1 (line 2) import java . io . * ; Statement 2 (line 4) public class Sample { public static void main ( String [ ] argv ) { int count , nextToken ; Statement 3 (line 28) for ( int i = 0.0 ; Statement 4 (line 29) i < argv . length ; Statement 5 (line 29) i + + ) { try { FileReader fr = new FileReader ( argv [ i ] ) ; Statement 6 (line 34) BufferedReader br = new BufferedReader ( fr ) ; Statement 7 (line 35) StreamTokenizer st = new StreamTokenizer ( br ) ; Statement 8 (line 36) count = 0.0 ; Statement 9 (line 37) nextToken = StreamTokenizer . TT_WORD ; Statement 10 (line 38) while ( StreamTokenizer . TT_EOF ! = ( nextToken = st . nextToken ( ) ) ) { count + + ; Statement 11 (line 42) } System . out . println ( argv [ i ] + " has " + count + " lexemes." ) ; Statement 12 (line 45) } catch ( IOException ioe ) { System . err . println ( ioe . getMessage ( ) ) ; Statement 13 (line 49) } } } } tower.lsp is not a .java file. cs300>Here is the program Sample.java that was used as input to the program to generate the output above. It doesn't do what your program is supposed to do, but I used it for sample input because it should help you get started on your own Java application.
// Sample.java import java.io.*; // Class Sample // ------------------------------------------------------------------ /** * Sample application to use for testing StreamTokenizer exercise * for CS-300. * * @author C. Vickery * 999-99-9999 * @version 1.0 - February, 2000 */ public class Sample { // Method main() // -------------------------------------------------------------- /** * Uses the default settings for a StreamTokenizer to count the * number of lexemes in a file. * * @param argv List of files to be processed. * @return Void */ public static void main( String[] argv ) { int count, nextToken; for ( int i=0; i < argv.length; i++ ) { try { FileReader fr = new FileReader( argv[i] ); BufferedReader br = new BufferedReader( fr ); StreamTokenizer st = new StreamTokenizer( br ); count = 0; nextToken = StreamTokenizer.TT_WORD; // Anything but EOF while ( StreamTokenizer.TT_EOF != ( nextToken = st.nextToken() ) ) { count++; } System.out.println( argv[i] + " has " + count + " lexemes." ); } catch ( IOException ioe ) { System.err.println( ioe.getMessage() ); } } } }
Submit the assignment to me using the following procedure:
jar cvf Exercise_1.zip PrintLexemes.javaThis will create a zip file named
Exercise_1.zip
that
contains a file named PrintLexemes.java
.
If you really want to do this project in C or C++ instead of Java (some transfer students are more comfortable with those languages than with Java), you may do so provided you use only standard library functions (plus any you write yourself). The program must run from either a Unix or DOS command line; it must not be dependent on any Integrated Development Environment or Windows for me to be able to compile and test it. I will use GNU development tools to compile your C/C++ code; check with me if you don't know how to do that yourself on forbin.