Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Vector;
- import java.util.Arrays;
- import java.util.regex.Pattern;
- import java.io.*;
- public class Interpreter {
- String filename;
- public Interpreter(String filename) {
- this.filename = filename;
- }
- /*Global vars*/
- private static int errors = 0;
- /*Reserved words list*/
- final String[] RESERVED_WORDS = {"add","sub","mul","div","exp","mod","eq","ne","ge","gt","le","lt","AND","OR","NOT"};
- final String[] DATA_TYPES = {"SHORT", "FLOAT", "BOOL", "STRING"};
- /*EBNF Rules*/
- /*MAIN*/
- public static void main(String[] args) throws Exception { //args[0] is our file
- if(args.length < 1) {
- System.out.println("No file specified");
- System.out.println("USAGE: Interpreter <filename.ext>");
- }
- else {
- Interpreter i = new Interpreter(args[0]);
- if(i.checkSyntax()) System.out.println("Starting program...");
- else System.out.println("Interpretation Failed... found "+errors+" error(s)");
- }
- }
- public boolean checkSyntax() throws Exception{
- FileInputStream fis = new FileInputStream(filename);
- DataInputStream dis = new DataInputStream(fis);
- BufferedReader br = new BufferedReader(new InputStreamReader(dis));
- String strLine;
- String[] split;
- int lineNumber = 1;
- while((strLine = br.readLine()) != null) {
- if(strLine.charAt(0) != '#') { //if it is not a comment
- split = strLine.split(" ");
- //System.out.println(Arrays.toString(split));
- for(String s : RESERVED_WORDS) {
- if(s.equalsIgnoreCase(split[0])) {
- System.out.println("[LINE "+lineNumber+"]: Illegal start of expression");
- System.out.println(">>> "+strLine);
- errors++;
- }
- }
- for(String s : DATA_TYPES) {
- if(s.equalsIgnoreCase(split[0])) {
- System.out.println("[LINE "+lineNumber+"]: Variable declatarion without a name");
- System.out.println(">>> "+strLine);
- errors++;
- }
- }
- }
- lineNumber++;
- }
- if(errors > 0) return false;
- else return true;
- }
- }
Add Comment
Please, Sign In to add comment