/** Name: tdparse Description: Top-down parser Arguments: Fully distinguished path to a source file to be parsed Programmer: Spencer R. Shimko Date: 03-25-2003 **/ import java.util.*; import java.io.*; /** Class: tdparse Main logical driver for parser **/ public class tdparse { // Begin: main public static void main(String arg[]){ LinkedList i_lines=new LinkedList(); // linked list of input lines LinkedList o_lines; // linked list of output lines // if not passed 1 argument quit giving correct usage if (arg.length!=1) { System.out.println("Usage: java tdparse /path/to/sourcefile"); System.exit(1); } // endif not 1 args // setup link list w/ lines from source file // the resulting linkedlist in c_lines // will be the lines read in from the sourcefile w/ no whitespace i_lines=readFile(arg[0]); o_lines=parser.parse(i_lines); // start parse stack and syntax checking display(o_lines); // display results } //end method main /* readFile: opens file and reads input Receives: filename, linklist to put source in Returns: link list containing source code */ private static LinkedList readFile(String file) { File s_file = new File(file); // file (sourcefile to be parsed) BufferedReader br; // bufferedreader for the file to be parsed LinkedList ll= new LinkedList(); // make sure file is a legitimate file... if (! s_file.isFile()){ System.out.println("Error: Source file must exist and be a valid file!"); System.exit(1); } // endif not file // create input filestream for reading try{ br = new BufferedReader(new FileReader(s_file)); // read from file and assign to ll // eof is reached when readline returns null try { // this catches .getLast excpetion when file is empty do{ //read line into ll try { // // get the line and trim and replace whitespace // and convert to char array for ease of use ll.add(br.readLine()); } catch(IOException e) { System.out.println("Error: Reading from sourcefile!."); } } while( ll.getLast() != null ); //end until reading from sourcefile Object temp=ll.removeLast(); /// remove null pointer } catch (NoSuchElementException e){ System.out.println("Error: Sourcefile appears to be empty!"); System.exit(1); } } catch(IOException e){ System.out.println("Error: opening source file!"); System.exit(1); } //end try/catch file open return ll; } //end method readFile /** display: displays link list of results receives: ll of results returns: nada **/ private static void display(LinkedList results){ System.out.println("==========RESULTS=========="); for (int x=0; x