import java.io.*; import java_cup.runtime.*; // defines Symbol // ********************************************************************** // Main program to test the C-- scanner. // // The file to be scanned should be given as a command-line argument. // The program opens the file and calls the scanner until the EOF token // is returned. For each token, the line and character numbers are printed // (to System.out), followed by the token name. If the token has a value // (is a literal or an identifier), the value is printed, too. // ********************************************************************** public class P2 { public static void main(String[] args) { // check for command-line arg if (args.length != 1) { System.err.println("please supply name of file to be scanned."); System.exit(-1); } // open input file FileReader inFile = null; try { inFile = new FileReader(args[0]); } catch (FileNotFoundException ex) { System.err.println("File " + args[0] + " not found."); System.exit(-1); } // create and call the scanner Yylex scanner = new Yylex(inFile); try { Symbol token = scanner.next_token(); while (token.sym != sym.EOF) { System.out.print(((TokenVal)token.value).linenum + ":" + ((TokenVal)token.value).charnum + " "); switch (token.sym) { case sym.PLUS: System.out.println("PLUS"); break; case sym.INTLITERAL: System.out.println("INTLITERAL (" + ((IntLitTokenVal)token.value).intVal + ")"); break; default: System.out.println("unexpected token: " + token.sym); System.exit(-1); } token = scanner.next_token(); } } catch (IOException ex) { System.err.println("unexpected IOException thrown by the scanner"); System.exit(-1); } } }