Guest User

Untitled

a guest
Jan 13th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import java.util.Vector;
  2. import java.util.Arrays;
  3. import java.util.regex.Pattern;
  4. import java.io.*;
  5.  
  6. public class Interpreter {
  7.    
  8.     String filename;
  9.  
  10.     public Interpreter(String filename) {
  11.         this.filename = filename;
  12.     }
  13.     /*Global vars*/
  14.     private static int errors = 0;
  15.     /*Reserved words list*/
  16.     final String[] RESERVED_WORDS = {"add","sub","mul","div","exp","mod","eq","ne","ge","gt","le","lt","AND","OR","NOT"};
  17.     final String[] DATA_TYPES = {"SHORT", "FLOAT", "BOOL", "STRING"};
  18.     /*EBNF Rules*/                 
  19.     /*MAIN*/
  20.     public static void main(String[] args) throws Exception { //args[0] is our file
  21.         if(args.length < 1) {
  22.             System.out.println("No file specified");
  23.             System.out.println("USAGE: Interpreter <filename.ext>");
  24.         }
  25.         else {
  26.             Interpreter i = new Interpreter(args[0]);
  27.             if(i.checkSyntax()) System.out.println("Starting program...");
  28.             else System.out.println("Interpretation Failed... found "+errors+" error(s)");
  29.         }
  30.     }
  31.    
  32.     public boolean checkSyntax() throws Exception{
  33.         FileInputStream fis = new FileInputStream(filename);
  34.         DataInputStream dis = new DataInputStream(fis);
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(dis));
  36.         String strLine;
  37.         String[] split;
  38.         int lineNumber = 1;
  39.         while((strLine = br.readLine()) != null) {
  40.             if(strLine.charAt(0) != '#') { //if it is not a comment
  41.                 split = strLine.split(" ");
  42.                 //System.out.println(Arrays.toString(split));
  43.                 for(String s : RESERVED_WORDS) {
  44.                     if(s.equalsIgnoreCase(split[0])) {
  45.                         System.out.println("[LINE "+lineNumber+"]: Illegal start of expression");
  46.                         System.out.println(">>> "+strLine);
  47.                         errors++;
  48.                     }
  49.                 }
  50.                 for(String s : DATA_TYPES) {
  51.                     if(s.equalsIgnoreCase(split[0])) {
  52.                         System.out.println("[LINE "+lineNumber+"]: Variable declatarion without a name");
  53.                         System.out.println(">>> "+strLine);
  54.                         errors++;
  55.                     }
  56.                 }
  57.             }
  58.             lineNumber++;
  59.         }
  60.         if(errors > 0) return false;
  61.         else return true;
  62.     }
  63.  
  64. }
Add Comment
Please, Sign In to add comment