Question: Stuck on last of my Java program's requirements to get my code to ignore single and multi line comments in a given text file. Any

Stuck on last of my Java program's requirements to get my code to ignore single and multi line comments in a given text file. Any help would be appreciated. 4 source files, 1 txt file; 8 pictures of all.

6. Modify the Java code for the Scanner to preprocess Java-style comments (no tokens) a. Single line comments that begin with // b. Multi-line comments that begin with /* and end with */.

Token.java

Stuck on last of my Java program's requirements to get my code

TokenType.java

to ignore single and multi line comments in a given text file.

CS390_Scanner.java

Any help would be appreciated. 4 source files, 1 txt file; 8

Scanner.java

pictures of all. 6. Modify the Java code for the Scanner topreprocess Java-style comments (no tokens) a. Single line comments that begin with// b. Multi-line comments that begin with /* and end with */.Token.java TokenType.java CS390_Scanner.java Scanner.java Prog1.txt 3 4 5 6 7 8 public

Prog1.txt

class Token { /** * This token's type (see TokenType) */ public

3 4 5 6 7 8 public class Token { /** * This token's type (see TokenType) */ public TokenType type; /** * This token's lexeme */ public String lexeme; /** * Instantiate this token with the given lexeme and type * * 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @param lexeme the lexeme for this token * @param type the lexeme's type public Token (String lexeme, TokenType type) { this.lexeme = lexeme; this.type = type; } } 0 0 0 07 WNA Ppublic enum TokenType { ASSIGN // Lexeme: ":=" COLON, // Not defined in Scott's grammar DIVIDE, EOF // Not defined in Scott's grammar 6 7 8 9 10 11 12 13 14 15 16 EQUAL, ERROR, ID, NUMBER, MINUS, MULTIPLY, PLUS, READ, WRITE; } 1 2 3 4 import java.io.IOException; public class CS390_Scanner { /** * Default location of the source file to scan. public static final String SOURCE = "C:/Users/rickb/Documents/Progl.txt"; /** * Entry point that begins scanning * * @param args not used A 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public static void main(String[] args) { try { Scanner scanner = new Scanner (SOURCE) ; for (Token token : scanner.scanAll()) System.out.println(token.lexeme + 11 : "T + token.type); } catch (IOException e) { System.out.println(e.toString()); } 2 import java.io. BufferedReader, 3 import java.io.FileReader: import java.io.IOExceplion; 5 import java.io. PushbackReader: import java.util.ArrayList; 7 8 9 public class Scanner ! 10 11 * True, if line breaks in the source file are defined with a carriage 12 * return line feed sequence (i.e. Windows), false if a single CR 13 character is used (.e. Unix) 14 15 private static final boolean CR_1.F = true; 16 17 19 * Maximum number of characters allowed in one source file line. 19 20 private statie final int MAX_LINE_SIZE = 256; 21 22 23 * The Input source program's file path 24 25 private final String sourceFile: 26 27 28 Used to read the source file a char at a time (with peek ahead). 29 30 private final PushbackReader buffer; 31 32 33 The current lexeme being read from the source file buffer. 34 (from 0 to end Poa) 35 */ 36 private final chart) lexeme: 37 38 39 Current position of the last character in the lexeme being read. 40 */ 41 private int endos = 0; 42 43 44 * True, in the middle of scanning a lexeme waiting for a delimites. 45 */ 16 private boolean islexeme; 47 48 A /* 49 * The current line number being read in the input source file. 50 */ 51 private int lineNo = 0; 52 532 54 * Initialize this Scanner. 55 56 * @param path source file (0.g. "C:/Users/Rickb/Documents/Prog.txt") 57 * throws IOException an unexpected non-recoverable error 58 */ 59. public Scanner (String path) throws ICException { 60 sourceFile = path; 61 lexeme = new char[MAX_LINE_SIZE]: 63 64 buffer = new PushbackReader (new BufferedReader (new FileReader(path))); 05 } 66 67 68 *Return the next Token in the input circe file 69 70 * return a Token with TokenType and Lexeme 71 throws IOException an unexpected non-recoverable error occurred 72 / 73 B public Token next() throws IOException { 74 i elexeme = false; 75 76 while (true) [ 77 char ch nextChar(); 78 79 switch (ch) { 80 case '10: 91 if (islexeme) 82 return scanLexome(): 03 else 84 return new Token("", TokenType.EOF); else return new Token("", TokenType.EOF); 83 84 95 80 87 88 89 00 91 92 93 94 95 case 'n': case 'r': case case 255: // non-breaking space caset: if (isexemo) return scanLexeme(); else return next(); 57 98 09 case ':': return seancolon(); case '-!! // single char tokens case case '/'; case case '': return scanSingleCharToken (ch); default: islexeme true; } 201 102 203 104 105 106 107 108 T09 110 111 112 113 114 115 116 117 ) * Return the current line number being read in the source file. * return the current line number 7 public int getLineNo() { return lineNo; 3 /** * Read and return the next character in the source file. return the next char or '10', if EOR */ */ private char nextChar() throws TOException intch = buffer.read(); switch (ch) caso 'n'; // ASCII Line feed, LE or caser: // carriage return, CR if ((CR_LF) && (ch == 'r')) { next char(); } else { lineNo++; ) return (char) ch; 119 120 121 122 123 124 125 126 127 120 129 130 131 132 133 134 125 136 137 130 139 140 241 142 143 144 145 140 142 248 149 230 101 152 153 154 155 156 15% 158 159 180 161 162 163 164 165 case: case 255: return (char) ch; // space // non breaking space case 1: return '\0: // Java read nothing, so // we're at EOF default: lexeme[endPos++] = (char) ch; return (char) ch; } } /** * As a delimiter was encountered during scanning, determine * and return the Token for the current lexeme. + * Ereturn a Token Encapsulating the current lexeme * private Token scanLexeme() { String lexemestr = String.copyvalueof(lexeme, 0, endpos): end Poa = 0; Istexame false; 1663 if (lexemeStr.equals IgnoreCare ("READ")) { if (lexemestr.equal. Ignorecase ("READ")) { return new Token (lexemeStr, TokenType. READ); } else if (lexemestr.equals tgnorecase ("WRTTR")) { return new Token (lexemeStr, TokenType.WRITE); } else { try ! Integer.parseInt(lexemeStr); // is lexeme a number? return new Token (lexemestr, TokenType.NUMBER); } catch (Number Format Exception e) { // no, it's an ID return new Token (lexemestr, TokenType. ID); 1 ) 1 3 . /** * If we're not in the middle of reading another token, return a token for the given single character, otherwise return the * token we're in the middle of reading and push the character + back into the input buffer. * @param ch a single character token (see TokenType) * private Token scanSingleCharToken(char ch) throws IOException { if (i alexeme) { // Middle of reading another token buffer.unread((int) ch); endPos--; return scanLexeme(); } else { endPos = 0; 165 166 167 168 169 170 171 1172 173 1/4 175 1.76 177 178 179 180 181 182 1 A3 194 185 186 107 188 189 190 191 192 193 194 195 196 197 199 199 200 201 202 203 204 205 208 207 1208 209 210 211 212 213 214 215 216 217 1218 219 220 221 222 223 224 2.25 228 227 228 229 230 231 232 233 234 235 236 237 238 239 |240 241 switch ((int) ch) case 'E': return new Token (String.valueof (ch), TokenType.EQUAL) : case + : return new Token (String.valueOf (ch), TokenType. PLUS); case: return new Token(String.valueOf (ch), TokenType.MINUS); case '/' : return new Token (String.valueof (ch). TokenType.DIVIDE); case: return new Token(String.valueof (ch), TokenType.MULTIPLY); // case '(': // return new Token (5tring.valueof(ch), TokenType.TFAREN); // // return new Token(String.valueof(ch), Token'type. KPAREN); default: return new Token (String.valueof (ch), TokenType. ERROR); /** * As a single colon has been read, if were within a lexeme, treat it as La delimiter, otherwise check for an assignment statement or error. * return token that is an ID, ASSIGN, or ERROR * athrows IOException an unexpected non recoverable error occurred * private Token scanColon() throws IOException ! if (isLexeme)! return scanLexeme(): ) else if (next.char() == -1) endPoy = 0; return new Token (":", TokenType. ASSIGN); } else { end Pos = 0; return new Token(":", TokenType. ERROR); ) } 1242 243 244 205 246 247 /** * A debugging utility for batch scanning the entire source file * * @return a list of tokens in the source file @throws IOException an unexpected non-recoverable error occurred */ public ArrayList scanAll() throws IOException { ArrayList tokens = new ArrayList(); Token token; 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 do { token = next(); tokens.add (token); } while (token.type != TokenType.EOF); return tokens; } ******* File Description: This file provides test statements HMOOOO * * ***********/ 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 ********************************* *********************** // This tests single line comment, above tests multi-line A = B * C A := C/D; // Test single line comment at end of statement D = E/F // now is the time // Multi-line comment on single line /* What ever happened */ G = (A + B + C) L = G* 6 // single line comment within multi-line comment /* test // */ write !A; // Test */ test write (B* C); /* Test */ D := 45; // Test single line comment within larger multi-line comment /* * test multi-line comment processing * this insures that // is properly * handled when inside a comment */ read A; C = cos (A); read B; // test /* test */ within single line comment 11 Test bool bool L; bool G; L := true; G := false 7/ test 'int' int sum; int A; int B; sum := A + B ; // sum A and B /* write out sum divided by 2 */ write sum / 2; write (sum / 2); read beta; C := cos (A + B); DISCRIMINAT POS := (-B + SQRT (B*B-4*A*C))/(2*A); 3 4 5 6 7 8 public class Token { /** * This token's type (see TokenType) */ public TokenType type; /** * This token's lexeme */ public String lexeme; /** * Instantiate this token with the given lexeme and type * * 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @param lexeme the lexeme for this token * @param type the lexeme's type public Token (String lexeme, TokenType type) { this.lexeme = lexeme; this.type = type; } } 0 0 0 07 WNA Ppublic enum TokenType { ASSIGN // Lexeme: ":=" COLON, // Not defined in Scott's grammar DIVIDE, EOF // Not defined in Scott's grammar 6 7 8 9 10 11 12 13 14 15 16 EQUAL, ERROR, ID, NUMBER, MINUS, MULTIPLY, PLUS, READ, WRITE; } 1 2 3 4 import java.io.IOException; public class CS390_Scanner { /** * Default location of the source file to scan. public static final String SOURCE = "C:/Users/rickb/Documents/Progl.txt"; /** * Entry point that begins scanning * * @param args not used A 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public static void main(String[] args) { try { Scanner scanner = new Scanner (SOURCE) ; for (Token token : scanner.scanAll()) System.out.println(token.lexeme + 11 : "T + token.type); } catch (IOException e) { System.out.println(e.toString()); } 2 import java.io. BufferedReader, 3 import java.io.FileReader: import java.io.IOExceplion; 5 import java.io. PushbackReader: import java.util.ArrayList; 7 8 9 public class Scanner ! 10 11 * True, if line breaks in the source file are defined with a carriage 12 * return line feed sequence (i.e. Windows), false if a single CR 13 character is used (.e. Unix) 14 15 private static final boolean CR_1.F = true; 16 17 19 * Maximum number of characters allowed in one source file line. 19 20 private statie final int MAX_LINE_SIZE = 256; 21 22 23 * The Input source program's file path 24 25 private final String sourceFile: 26 27 28 Used to read the source file a char at a time (with peek ahead). 29 30 private final PushbackReader buffer; 31 32 33 The current lexeme being read from the source file buffer. 34 (from 0 to end Poa) 35 */ 36 private final chart) lexeme: 37 38 39 Current position of the last character in the lexeme being read. 40 */ 41 private int endos = 0; 42 43 44 * True, in the middle of scanning a lexeme waiting for a delimites. 45 */ 16 private boolean islexeme; 47 48 A /* 49 * The current line number being read in the input source file. 50 */ 51 private int lineNo = 0; 52 532 54 * Initialize this Scanner. 55 56 * @param path source file (0.g. "C:/Users/Rickb/Documents/Prog.txt") 57 * throws IOException an unexpected non-recoverable error 58 */ 59. public Scanner (String path) throws ICException { 60 sourceFile = path; 61 lexeme = new char[MAX_LINE_SIZE]: 63 64 buffer = new PushbackReader (new BufferedReader (new FileReader(path))); 05 } 66 67 68 *Return the next Token in the input circe file 69 70 * return a Token with TokenType and Lexeme 71 throws IOException an unexpected non-recoverable error occurred 72 / 73 B public Token next() throws IOException { 74 i elexeme = false; 75 76 while (true) [ 77 char ch nextChar(); 78 79 switch (ch) { 80 case '10: 91 if (islexeme) 82 return scanLexome(): 03 else 84 return new Token("", TokenType.EOF); else return new Token("", TokenType.EOF); 83 84 95 80 87 88 89 00 91 92 93 94 95 case 'n': case 'r': case case 255: // non-breaking space caset: if (isexemo) return scanLexeme(); else return next(); 57 98 09 case ':': return seancolon(); case '-!! // single char tokens case case '/'; case case '': return scanSingleCharToken (ch); default: islexeme true; } 201 102 203 104 105 106 107 108 T09 110 111 112 113 114 115 116 117 ) * Return the current line number being read in the source file. * return the current line number 7 public int getLineNo() { return lineNo; 3 /** * Read and return the next character in the source file. return the next char or '10', if EOR */ */ private char nextChar() throws TOException intch = buffer.read(); switch (ch) caso 'n'; // ASCII Line feed, LE or caser: // carriage return, CR if ((CR_LF) && (ch == 'r')) { next char(); } else { lineNo++; ) return (char) ch; 119 120 121 122 123 124 125 126 127 120 129 130 131 132 133 134 125 136 137 130 139 140 241 142 143 144 145 140 142 248 149 230 101 152 153 154 155 156 15% 158 159 180 161 162 163 164 165 case: case 255: return (char) ch; // space // non breaking space case 1: return '\0: // Java read nothing, so // we're at EOF default: lexeme[endPos++] = (char) ch; return (char) ch; } } /** * As a delimiter was encountered during scanning, determine * and return the Token for the current lexeme. + * Ereturn a Token Encapsulating the current lexeme * private Token scanLexeme() { String lexemestr = String.copyvalueof(lexeme, 0, endpos): end Poa = 0; Istexame false; 1663 if (lexemeStr.equals IgnoreCare ("READ")) { if (lexemestr.equal. Ignorecase ("READ")) { return new Token (lexemeStr, TokenType. READ); } else if (lexemestr.equals tgnorecase ("WRTTR")) { return new Token (lexemeStr, TokenType.WRITE); } else { try ! Integer.parseInt(lexemeStr); // is lexeme a number? return new Token (lexemestr, TokenType.NUMBER); } catch (Number Format Exception e) { // no, it's an ID return new Token (lexemestr, TokenType. ID); 1 ) 1 3 . /** * If we're not in the middle of reading another token, return a token for the given single character, otherwise return the * token we're in the middle of reading and push the character + back into the input buffer. * @param ch a single character token (see TokenType) * private Token scanSingleCharToken(char ch) throws IOException { if (i alexeme) { // Middle of reading another token buffer.unread((int) ch); endPos--; return scanLexeme(); } else { endPos = 0; 165 166 167 168 169 170 171 1172 173 1/4 175 1.76 177 178 179 180 181 182 1 A3 194 185 186 107 188 189 190 191 192 193 194 195 196 197 199 199 200 201 202 203 204 205 208 207 1208 209 210 211 212 213 214 215 216 217 1218 219 220 221 222 223 224 2.25 228 227 228 229 230 231 232 233 234 235 236 237 238 239 |240 241 switch ((int) ch) case 'E': return new Token (String.valueof (ch), TokenType.EQUAL) : case + : return new Token (String.valueOf (ch), TokenType. PLUS); case: return new Token(String.valueOf (ch), TokenType.MINUS); case '/' : return new Token (String.valueof (ch). TokenType.DIVIDE); case: return new Token(String.valueof (ch), TokenType.MULTIPLY); // case '(': // return new Token (5tring.valueof(ch), TokenType.TFAREN); // // return new Token(String.valueof(ch), Token'type. KPAREN); default: return new Token (String.valueof (ch), TokenType. ERROR); /** * As a single colon has been read, if were within a lexeme, treat it as La delimiter, otherwise check for an assignment statement or error. * return token that is an ID, ASSIGN, or ERROR * athrows IOException an unexpected non recoverable error occurred * private Token scanColon() throws IOException ! if (isLexeme)! return scanLexeme(): ) else if (next.char() == -1) endPoy = 0; return new Token (":", TokenType. ASSIGN); } else { end Pos = 0; return new Token(":", TokenType. ERROR); ) } 1242 243 244 205 246 247 /** * A debugging utility for batch scanning the entire source file * * @return a list of tokens in the source file @throws IOException an unexpected non-recoverable error occurred */ public ArrayList scanAll() throws IOException { ArrayList tokens = new ArrayList(); Token token; 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 do { token = next(); tokens.add (token); } while (token.type != TokenType.EOF); return tokens; } ******* File Description: This file provides test statements HMOOOO * * ***********/ 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 ********************************* *********************** // This tests single line comment, above tests multi-line A = B * C A := C/D; // Test single line comment at end of statement D = E/F // now is the time // Multi-line comment on single line /* What ever happened */ G = (A + B + C) L = G* 6 // single line comment within multi-line comment /* test // */ write !A; // Test */ test write (B* C); /* Test */ D := 45; // Test single line comment within larger multi-line comment /* * test multi-line comment processing * this insures that // is properly * handled when inside a comment */ read A; C = cos (A); read B; // test /* test */ within single line comment 11 Test bool bool L; bool G; L := true; G := false 7/ test 'int' int sum; int A; int B; sum := A + B ; // sum A and B /* write out sum divided by 2 */ write sum / 2; write (sum / 2); read beta; C := cos (A + B); DISCRIMINAT POS := (-B + SQRT (B*B-4*A*C))/(2*A)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!