Java Lister
July 31st, 2007 by RickSo, I decided to pick up a book about writing compilers — Writing Compilers and Interpreters.
I figured it would be good for me to at least know what’s involved in writing a compiler. Of course, the first thing in the book is a program to just list the contents of a text file with line numbers and paginated. The lister program, as well as much of the rest of the code, is in C (at least in my edition).
C of course is not my favorite language, so I thought I’d quickly port the lister over to Java, just to see what would happen:
1 // package 2 3 // Imports go here. 4 import java.io.FileReader; 5 import java.io.LineNumberReader; 6 import java.io.IOException; 7 import java.text.SimpleDateFormat; 8 import java.util.Date; 9 import java.util.GregorianCalendar; 10 11 12 /** 13 * List class 14 */ 15 16 public class List { 17 public static final char FORM_FEED_CHAR = ‘f’; 18 19 public static final int MAX_FILE_NAME_LENGTH = 32; 20 public static final int MAX_SOURCE_LINE_LENGTH = 256; 21 public static final int MAX_PRINT_LINE_LENGTH = 80; 22 public static final int MAX_LINES_PER_PAGE = 50; 23 public static final int DATE_STRING_LENGTH = 26; 24 25 int lineNumber = 0; 26 int pageNumber = 0; 27 int nestLevel = 0; 28 int lineCount = MAX_LINES_PER_PAGE; 29 30 StringBuffer sourceBuffer; 31 StringBuffer printBuffer; 32 String date; 33 34 String fileName; 35 36 LineNumberReader sourceReader; 37 38 public static void main (String[] args) { 39 if (args.length < 1) { 40 System.err.println(“Need to supply the filename.”); 41 System.exit(1); 42 } 43 44 List myList = new List(args[0]); 45 46 while(myList.getSourceLine()){} 47 } 48 49 public List (String newFileName) { 50 // Set the date string 51 SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy.MMMMM.dd hh:mm aaa”); 52 Date timer = new GregorianCalendar().getTime(); 53 date = dateFormat.format(timer); 54 55 // Set the file name 56 this.fileName = newFileName; 57 58 // Initialize the buffers 59 sourceBuffer = new StringBuffer(); 60 printBuffer = new StringBuffer(); 61 62 try { 63 // After this, the file should be ready for action. 64 sourceReader = 65 new LineNumberReader(new FileReader(this.fileName)); 66 } catch (IOException e) { 67 System.err.println (“Problem opening “ + fileName + “.”); 68 e.printStackTrace(); 69 System.exit(1); 70 } 71 } 72 73 boolean getSourceLine() { 74 if (printBuffer.length() > 0) { 75 printBuffer.delete (0, printBuffer.length()); 76 } 77 78 if (sourceBuffer.length() > 0) { 79 sourceBuffer.delete (0, sourceBuffer.length()); 80 } 81 82 boolean endOfFile = false; 83 try { 84 String nextLine = sourceReader.readLine(); 85 endOfFile = (nextLine == null); 86 if (!endOfFile) { 87 sourceBuffer.append (nextLine); 88 } 89 } catch (IOException e) { 90 System.err.println (“Problem reading from “ + fileName + “.”); 91 e.printStackTrace(); 92 } 93 94 if( !endOfFile ){ 95 lineNumber++; 96 printBuffer.append(lineNumber + ” “); 97 printBuffer.append(nestLevel + “: “); 98 printBuffer.append(sourceBuffer); 99 100 // Output this line. 101 printLine(printBuffer); 102 } 103 104 return !endOfFile; 105 } 106 107 void printLine (StringBuffer line) { 108 StringBuffer saveBuffer = null; 109 110 if(++lineCount > MAX_LINES_PER_PAGE) { 111 printPageHeader(); 112 lineCount = 1; 113 } 114 115 if (line.length() > MAX_PRINT_LINE_LENGTH) { 116 System.out.println (line.subSequence (0, MAX_PRINT_LINE_LENGTH -1)); 117 System.out.println (” “ + line.subSequence ( MAX_PRINT_LINE_LENGTH, line.length() -1 )); 118 } else { 119 System.out.println (line); 120 } 121 } 122 123 void printPageHeader() { 124 System.out.println (FORM_FEED_CHAR); 125 System.out.println (“Paget“ + ++pageNumber + “t“ + fileName + “t“ + date + “nn“); 126 } 127 }
After seeing this, I’m beginning to think that C may not be so bad. Note that I didn’t even try dealing with formatting my output; and I’m not sure if using StringBuffers over char arrays is such a good idea.
We’ll have to see how the rest of the book goes.
Posted in Programming |