home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / jikepg12.zip / jikespg / examples / expr / Scanner.java < prev    next >
Encoding:
Text File  |  1999-11-04  |  3.9 KB  |  130 lines

  1. // $Id: Scanner.java,v 1.3 1999/11/04 14:02:18 shields Exp $
  2. // This software is subject to the terms of the IBM Jikes Compiler
  3. // License Agreement available at the following URL:
  4. // http://www.ibm.com/research/jikes.
  5. // Copyright (C) 1983, 1999, International Business Machines Corporation
  6. // and others.  All Rights Reserved.
  7. // You must accept the terms of that agreement to use this software.
  8.  
  9. //
  10. // The Scanner object
  11. //
  12. class Scanner implements exprsym
  13. {
  14.     int next_byte;
  15.     Option option;
  16.     LexStream lex_stream;
  17.  
  18.     Scanner(Option option, LexStream lex_stream)
  19.     {
  20.         this.lex_stream = lex_stream;
  21.         this.option = option;
  22.     }
  23.  
  24.     //
  25.     //
  26.     //
  27.     void skip_spaces() throws java.io.IOException
  28.     {
  29.         while (next_byte != '\n' && Character.isSpace((char) next_byte))
  30.             next_byte = System.in.read();
  31.         return;
  32.     }
  33.  
  34.     //
  35.     //
  36.     //
  37.     String scan_symbol() throws java.io.IOException
  38.     {
  39.         StringBuffer buffer = new StringBuffer();
  40.         while (next_byte != '\n' && (! Character.isSpace((char) next_byte)))
  41.         {
  42.             buffer.append((char) next_byte);
  43.         }
  44.  
  45.         return buffer.toString();
  46.     }
  47.  
  48.     //
  49.     //
  50.     //
  51.     void scan() throws java.io.IOException
  52.     {
  53.         //
  54.         // Do not use token indexed at location 0.
  55.         //
  56.         Token start_token = new Token();
  57.         start_token.kind = 0;
  58.         start_token.name = "";
  59.         lex_stream.tokens.addElement(start_token);
  60.  
  61.         next_byte = System.in.read();
  62.         for (skip_spaces(); next_byte != '\n'; skip_spaces())
  63.         {
  64.             Token token = new Token();
  65.             switch(next_byte)
  66.             {
  67.                 case '+':
  68.                      token.kind = TK_PLUS;
  69.                      token.name = "+";
  70.                      break;
  71.                 case '-':
  72.                      token.kind = TK_MINUS;
  73.                      token.name = "-";
  74.                      break;
  75.                 case '*':
  76.                      token.kind = TK_STAR;
  77.                      token.name = "*";
  78.                      break;
  79.                 case '/':
  80.                      token.kind = TK_SLASH;
  81.                      token.name = "/";
  82.                      break;
  83.                 case '(':
  84.                      token.kind = TK_LPAREN;
  85.                      token.name = "(";
  86.                      break;
  87.                 case ')':
  88.                      token.kind = TK_RPAREN;
  89.                      token.name = ")";
  90.                      break;
  91.                 default:
  92.                      StringBuffer buffer = new StringBuffer();
  93.                      if (! Character.isDigit((char) next_byte))
  94.                      {
  95.                          token.kind = TK_ERROR;
  96.                          while (next_byte != '\n' && (! Character.isSpace((char) next_byte)))
  97.                          {
  98.                              buffer.append((char) next_byte);
  99.                              next_byte = System.in.read();
  100.                          }
  101.                          System.out.println("The token \"" + buffer.toString() + "\" is illegal"); 
  102.                      }
  103.                      else
  104.                      {
  105.                          token.kind = TK_NUMBER;
  106.                          do
  107.                          {
  108.                              buffer.append((char) next_byte);
  109.                              next_byte = System.in.read();
  110.                          } while (next_byte != '\n' && Character.isDigit((char) next_byte));
  111.                     }
  112.  
  113.                     token.name = buffer.toString();
  114.         }
  115.  
  116.             if (token.kind != TK_ERROR && token.kind != TK_NUMBER)
  117.                 next_byte = System.in.read();
  118.     
  119.             lex_stream.tokens.addElement(token);
  120.         }
  121.  
  122.         Token end_token = new Token();
  123.         end_token.kind = TK_EOF;
  124.         end_token.name = "";
  125.         lex_stream.tokens.addElement(end_token);
  126.  
  127.         return;
  128.     }
  129. }
  130.