Comment deleter, Due Tuesday 2/12


Java compilers typically make multiple passes over the source code. The first pass simply deletes the comments (which simplifies subsequent passes). Your task is to implement the comment deletion pass.

Two types of comments

As you no doubt know, comments come to two flavors; line comments (which go from // to the end of the line), and block comments (from /* to */).

Phase 1 - prototype

Read in source code from a file, and echo it with the comments deleted. Ignore double-quotes; i.e. simply delete to the end of the line on a line comment and, everything between \* and *\.

Phase 2 - Strings!

Handle String literals. Comments inside double-quotes are not comments! E.g. the code,"/* la, la, la"); does not start a block comment; so you have to be make sure not to be fooled.

Sample input

        class Foo { // a class
            int x; /* x */
                M A I N
            public static void /*gasp!*/ main (String[] asdfasf) {
                new CommentFrame();
            void foo() {
                stuff("la /* // etc...");
                // nonsense("la la la");
                etc(/*"*/"this is a String../*..");
                String ouch = "\"//escaped quotes are not quotes!"; // okay, all the rest is extra credit
                worse("\/* this is not a comment!");
                /* this is a comment!
                but("this is, um... \*/*still* a comment"); // um... still... */
                whereas("this is a String \*/*not* a comment"); // and this is a comment still... although for a different reason */
                char foo = '\"'/* comment! I am a comment!*/;