home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / compilers / p2 / Main.java < prev    next >
Text File  |  2005-10-11  |  4KB  |  109 lines

  1. // -------------------------------- Main --------------------------------
  2. //
  3. // Compiler Project 2 - Lexical Analyzer Main
  4. //
  5. // This class is never instantiated.  It includes the "main" method to exercise
  6. // the lexical analyzer.  Ultimately, this class will contain methods that
  7. // relate to the overall behavior of the compiler.
  8. //
  9. // Harry Porter -- 10/10/05
  10. //
  11.  
  12. import java.io.*;
  13.  
  14. class Main {
  15.  
  16.     //
  17.     // Static Fields
  18.     //
  19.     static int errorCount = 0;     // How many errors, so far
  20.     static Lexer lexer;            // The lexer
  21.  
  22.  
  23.     //
  24.     // Main ()
  25.     //
  26.     // In a loop, scan and print tokens until EOF is encountered.
  27.     //
  28.     public static void main (String [] args) {
  29.         String previousAbcString = null;
  30.         try {
  31.  
  32.             // Create the lexer object...
  33.             try {
  34.                 if (args.length == 0) {
  35.                     lexer = new Lexer (new InputStreamReader (System.in));
  36.                 } else if (args.length == 1) {
  37.                     lexer = new Lexer (new FileReader (args[0]));
  38.                 } else {
  39.                     throw new FatalError ("Command line requires 0 or 1 file name");
  40.                 }
  41.             } catch (FileNotFoundException fnfexn) {
  42.                 throw new FatalError ("File not found: " + args[0]);
  43.             } catch (IOException e) {
  44.                 throw new FatalError (e.getMessage ());
  45.             }
  46.  
  47.  
  48.             // Read and print tokens until EOF is encountered...
  49.             while (true) {
  50.                 int token = lexer.getToken ();
  51.                 if (token == Token.EOF) break;
  52.                 printToken (token);
  53.                 // Make sure the sValues for equal ID's are set to the identical object;
  54.                 // This test relates to file "tst/test5.pcat"...
  55.                 if (token == Token.ID && lexer.sValue.equals ("abc")) {
  56.                     if (previousAbcString == null) {
  57.                         previousAbcString = lexer.sValue;
  58.                     } else if (previousAbcString != lexer.sValue) {
  59.                         System.err.println ("Cannonical form of string is not used");
  60.                     }
  61.                 }
  62.         
  63.             }
  64.  
  65.         } catch (LogicError e) {
  66.             // The next stmt will print the message (if any) and info about where
  67.             // the error was thrown.
  68.             e.printStackTrace ();
  69.         } catch (FatalError e) {
  70.             if (e.getMessage () != null) {
  71.                 System.err.println (e.getMessage ());
  72.             }
  73.         }
  74.         if (errorCount > 0) {
  75.             System.err.println (errorCount + " error(s) were detected!");
  76.             System.exit (1);
  77.         }
  78.     }
  79.  
  80.  
  81.  
  82.     //
  83.     // printToken (token) --> String
  84.     //
  85.     // This method prints this token, in a form such as:
  86.     //     "9      INTEGER    12345"
  87.     //
  88.     static void printToken (int token) {
  89.         if (token == Token.INTEGER) {
  90.             System.out.println (lexer.lineNumber + "\t"
  91.                   + Token.stringOf [token] + "\t" + lexer.iValue);
  92.         } else if (token == Token.REAL) {
  93.             System.out.println (lexer.lineNumber + "\t"
  94.                   + Token.stringOf [token] + "\t" + lexer.rValue);
  95.         } else if (token == Token.STRING) {
  96.             System.out.println (lexer.lineNumber + "\t"
  97.                   + Token.stringOf [token] + "\t\"" + lexer.sValue + "\"");
  98.         } else if (token == Token.ID) {
  99.             System.out.println (lexer.lineNumber + "\t"
  100.                   + Token.stringOf [token] + "\t\"" + lexer.sValue + "\"");
  101.         } else {
  102.             System.out.println (lexer.lineNumber + "\t" + Token.stringOf [token]);
  103.         }
  104.     }
  105.  
  106.  
  107.  
  108. }
  109.