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 / p3 / ParserStarter.java < prev    next >
Text File  |  2005-10-19  |  5KB  |  191 lines

  1. // --------------------------- Parser ------------------------------
  2. //
  3. // Each instance of this class is a recursive descent parser for the
  4. // PCAT language.  There will only be one Parser object created.
  5. //
  6. // <Your name here> -- 01/26/03
  7. //
  8.  
  9. import java.io.*;
  10.  
  11. class Parser {
  12.  
  13.     //
  14.     // Fields
  15.     //
  16.     Lexer lexer;             // The lexer being used by this parser
  17.     int nextToken;           // The type of the current token
  18.  
  19.  
  20.     //
  21.     // Constructor
  22.     //
  23.     Parser (String [] args)
  24.         throws FatalError
  25.     {
  26.  
  27.         // Create a lexer object...
  28.         try {
  29.             if (args.length == 0) {
  30.                 lexer = new Lexer (new InputStreamReader (System.in));
  31.             } else if (args.length == 1) {
  32.                 lexer = new Lexer (new FileReader (args [0]));
  33.             } else {
  34.                 throw new FatalError ("Command line requires 0 or 1 file name");
  35.             }
  36.         } catch (FileNotFoundException fnfexn) {
  37.             throw new FatalError ("File not found: " + args [0]);
  38.         } catch (IOException e) {
  39.             throw new FatalError (e.getMessage ());
  40.         }
  41.  
  42.         // Initialize the lexer and nextToken...
  43.         nextToken = Token.EOF+1;       // Something besides EOF
  44.         scan ();
  45.     }
  46.  
  47.  
  48.  
  49.     //
  50.     // scan ()
  51.     //
  52.     // Move to the next token.  This will modify the "nextToken" and
  53.     // "lexer.attribute" variables on the side.  It will throw a
  54.     // FatalError if problems occur within the Lexer.
  55.     //
  56.     void scan ()
  57.         throws FatalError
  58.     {
  59.         if (nextToken != Token.EOF) {
  60.             nextToken = lexer.getToken ();
  61.             // System.out.println ("SCANNING: nextToken = "
  62.             //                        + Token.stringOf [nextToken]);
  63.         }
  64.     }
  65.  
  66.  
  67.  
  68.     //
  69.     // syntaxError (msg)
  70.     //
  71.     // This method prints an error message and then throws a FatalError.
  72.     // It should be called whenever an unexpected token is encountered.
  73.     //
  74.     void syntaxError (String msg)
  75.         throws FatalError
  76.     {
  77.         Main.errorCount++;
  78.         System.err.println ("Syntax error on line " + lexer.lineNumber + ": " + msg);
  79.         throw new FatalError ();
  80.     }
  81.  
  82.  
  83.  
  84.     //
  85.     // mustHave (expectedToken, msg)
  86.     //
  87.     // This method is passed an expected kind of token.  If the current
  88.     // token matches, then it will advance to the next token.  Else it will
  89.     // invoke "syntaxError".
  90.     //
  91.     void mustHave (int expectedToken, String msg)
  92.         throws FatalError
  93.     {
  94.         if (nextToken == expectedToken) {
  95.             scan ();
  96.         } else {
  97.             syntaxError (msg);
  98.         }
  99.     }
  100.  
  101.  
  102.  
  103.     //
  104.     // parseProgram ()
  105.     //
  106.     // This method parses a PCAT Program according to the grammar rule:
  107.     //
  108.     //    Program --> PROGRAM IS Body ';'
  109.     //
  110.     void parseProgram ()
  111.         throws FatalError
  112.     {
  113.         mustHave (Token.PROGRAM, "Expecting PROGRAM");
  114.         mustHave (Token.IS, "Expecting IS after PROGRAM");
  115.         parseBody ();
  116.         mustHave (Token.SEMI, "Expecting ';' after program body");
  117.         mustHave (Token.EOF, "Expecting EOF after PROGRAM IS body");
  118.     }
  119.  
  120. ...
  121.  
  122.     //
  123.     // parseIfStmt ()
  124.     //
  125.     // This method parses an IfStmt according to the grammar rule:
  126.     //
  127.     //    IfStmt  -->  IF Expr THEN { Statement }
  128.     //                    { ELSEIF Expr THEN { Statement } } 
  129.     //                    [ ELSE { Statement } ] END ';'
  130.     //
  131.     // On entry, we've already checked that nextToken is IF.
  132.     //
  133.     void parseIfStmt ()
  134.         throws FatalError
  135.     {
  136.         mustHave (Token.IF, "Compiler logic error in parseIfStmt");
  137.         System.out.println ("IF");
  138.         parseExpr ();
  139.         mustHave (Token.THEN, "Expecting THEN in IF statement following expr");
  140.         System.out.println ("THEN");
  141.         parseStmts ();
  142.         while (nextToken == Token.ELSEIF) {
  143.             System.out.println ("ELSEIF");
  144.             scan ();
  145.             parseExpr ();
  146.             mustHave (Token.THEN, "Expecting THEN after ELSEIF expr in IF statement");
  147.             parseStmts ();
  148.         }
  149.         if (nextToken == Token.ELSE) {
  150.             System.out.println ("ELSE");
  151.             scan ();
  152.             parseStmts ();
  153.         }
  154.         mustHave (Token.END, "Expecting ELSEIF, ELSE, or END in IF statement");
  155.         mustHave (Token.SEMI, "Expecting ';' after END in IF statement");
  156.         System.out.println ("ENDIF");
  157.     }
  158.  
  159. ...
  160.  
  161.     //
  162.     // parseExpr ()
  163.     //
  164.     // This method parses an Expr according to the grammar rule:
  165.     //
  166.     //    Expr  -->  Expr2  {  ('<' | '>' | '<=' | '>=' | '=' | '<>')  Expr2  }
  167.     //
  168.     //
  169.     void parseExpr ()
  170.         throws FatalError
  171.     {
  172.         parseExpr2 ();
  173.         while (true) {
  174.             if (nextToken == Token.LESS) {
  175.                 scan ();
  176.                 parseExpr2 ();
  177.                 System.out.println ("LESS");
  178.             } else if (nextToken == Token.GREATER) {
  179.                 scan ();
  180.                 parseExpr2 ();
  181.                 System.out.println ("GREATER");
  182. ...
  183.  
  184.             } else {
  185.                 break;
  186.             }
  187.         }
  188.     }
  189.  
  190. ...
  191.