Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. // javac scan.java // compile
  2. // java scan example1.x // run
  3.  
  4. import java.io.*; // nice way to import everything
  5.  
  6.  
  7. public class scan // needs to match the file name as in scan.java
  8. {
  9.  
  10.   public static void main(String[] args) throws IOException
  11.   {
  12.     token[] identified = new token[999]; // array of identified tokens
  13.     int line = 1; // line number
  14.     int pos = 0; // position on line
  15.     char sym = 0; // symbol as a part of token
  16.     String[] tokens = new String[999]; // array of all tokens
  17.     tokens[0] = "";
  18.     int tokenNum = 0; // number of tokens
  19.  
  20.    
  21.     FileReader scanner = new FileReader(args[0]); // Java thing used for input
  22.    
  23.    
  24.     while(sym != (char) -1) // scanner.read returns -1 when reaches eof, thus this will read till eof
  25.     {
  26.  
  27.       token curr = new token();
  28.  
  29.       pos++;
  30.       sym = (char) scanner.read(); // reads a char, returns it as an int. Thus has to be casted as char
  31.  
  32.      
  33.       if(tokenNum > 1 && tokens[tokenNum - 2] == "") // these two blocks are used to delete "empty" tokens caused by spaces, tabs and \n
  34.       {
  35.         tokens[tokenNum - 2] = tokens[tokenNum - 1];
  36.         tokens[tokenNum - 1] = "";
  37.         tokenNum--;
  38.       }
  39.       if(tokenNum > 0 && tokens[tokenNum - 1] == "")
  40.       {
  41.         tokens[tokenNum - 1] = "";
  42.         tokenNum--;
  43.       }
  44.      
  45.    
  46.       if(Character.isLetterOrDigit(sym) || sym == '_') { // if token is an Identifier
  47.         tokens[tokenNum] += sym;
  48.         pos++;
  49.       }
  50.        
  51.       else if (sym == '(' || // if token is an operator
  52.                sym == ')' ||
  53.                sym == '{' ||
  54.                sym == '}' ||
  55.                sym == '+' ||
  56.                sym == '-' ||
  57.                sym == '*' ||
  58.                sym == '/' ||
  59.                sym == '<' ||
  60.                sym == '>' ||
  61.                sym == ';')
  62.       {
  63.  
  64.         curr.value = tokens[tokenNum]; // takes the current string and store it in curr.value
  65.         curr.pos = pos - tokens[tokenNum].length(); // gets the position of where the current token started
  66.         curr.line = line; // line number
  67.  
  68.         identified[tokenNum] = curr;
  69.         pos++;
  70.  
  71.         tokenNum++; // has to be a new token
  72.         tokens[tokenNum] = "";
  73.         tokens[tokenNum] += sym; // stores operator as a token
  74.  
  75.         curr.value = tokens[tokenNum];
  76.         curr.pos = pos;
  77.         curr.line = line;
  78.  
  79.         identified[tokenNum] = curr;
  80.  
  81.         tokenNum++; // all operators are 1 symbol, thus next symbol will be in a different token
  82.         tokens[tokenNum] = "";
  83.       }
  84.      
  85.       else if (sym == '\n')
  86.       {
  87.        
  88.         curr.value = tokens[tokenNum];
  89.         curr.pos = pos - tokens[tokenNum].length();
  90.         curr.line = line;
  91.  
  92.         identified[tokenNum] = curr;
  93.        
  94.         pos = 0; // reset column position because its a new line
  95.         line++; // increment line count
  96.         tokenNum++;
  97.         tokens[tokenNum] = "";
  98.       } else if (sym == ' ') {
  99.        
  100.         curr.value = tokens[tokenNum];
  101.         curr.pos = pos - tokens[tokenNum].length();
  102.         curr.line = line;
  103.        
  104.         identified[tokenNum] = curr;
  105.  
  106.         pos++; //inc position but not line count
  107.         tokenNum++;
  108.         tokens[tokenNum] = "";
  109.       } else if (sym == '\t') {
  110.        
  111.         curr.value = tokens[tokenNum];
  112.         curr.pos = pos - tokens[tokenNum].length();
  113.         curr.line = line;
  114.  
  115.         identified[tokenNum] = curr;
  116.  
  117.         pos += 2; //increment position count by 2
  118.         tokenNum++;
  119.         tokens[tokenNum] = "";
  120.       }
  121.      
  122.     } // while
  123.    
  124.     for(int i = 0; i <= tokenNum; i++) // prints all tokens
  125.     {
  126.       System.out.println(tokens[i]);
  127.       System.out.println(identified[i].value);
  128.     }
  129.    
  130.   } // main
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement