Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
1
Indexable
Never
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;

class symTab {

	class symtabEntry {
		int address;
		String symbol;

		symtabEntry(int ad, String s) {
			address = ad;
			symbol = s;
		}
	}

	symtabEntry table[];
	int curr;

	public symTab() {

		table = new symtabEntry[50];
		curr = 0;
	}

	public void insert(String s, int x) {
		symtabEntry obj = new symtabEntry(x, s);
		table[curr++] = obj;
	}
}

class litTab {

	class littabEntry {
		int address;
		String literal;

		littabEntry(int ad, String s) {
			address = ad;
			literal = s;
		}
	}

	littabEntry table[];
	int curr;

	public litTab() {
		table = new littabEntry[50];
		curr = 0;
	}

	public void insert(String s, int x) {
		littabEntry obj = new littabEntry(x, s);
		table[curr++] = obj;
	}
}

class PassOne {
	HashMap<String, String> opt = new HashMap<>();
	litTab literalTable = new litTab();
	symTab symbolTable = new symTab();
	String[] input;
	String inputFile = "src/input.txt";
	String ICFile = "src/intermediateCode.txt";

	public PassOne() {
		initialiseOpt();
		readData();
		createIntermediateCode();
	}

	void initialiseOpt() {
		opt.put("STOP", "IS00");
		opt.put("ADD", "IS01");
		opt.put("SUB", "IS02");
		opt.put("MULT", "IS03");
		opt.put("MOVER", "IS04");
		opt.put("MOVEM", "IS05");
		opt.put("COMP", "IS06");
		opt.put("BC", "IS07");
		opt.put("DIV", "IS08");
		opt.put("READ", "IS09");
		opt.put("PRINT", "IS10");

		opt.put("START", "AD01");
		opt.put("END", "AD02");
		opt.put("ORIGIN", "AD03");
		opt.put("EQU", "AD04");
		opt.put("LTORG", "AD05");

		opt.put("DC", "DL01");
		opt.put("DS", "DL01");
	}

	void readData() {
		try {
			int ind = 0;
			BufferedReader reader = new BufferedReader(new FileReader(inputFile));
			input = new String[100];
//			for (int i = 0; i < 100; i++)
//				input[i] = new String();
			String line = reader.readLine();
			while (line != null) {
				input[ind++] = line;
				line = reader.readLine();
			}
		} catch (Exception e) {

		}
	}

	void createIntermediateCode() {
		try {

			BufferedWriter writer = new BufferedWriter(new FileWriter(ICFile));
			for (String line : input) {
				if (line == null)
					break;
				String[] tokens = line.split(" ");
				String res = "(";
				int i = 0;
				for (String token : tokens) {
					if (token == "$")
						continue;
					String temp = opt.get(token);
					res += temp.substring(0, 2);
					res += ",";
					res += temp.substring(2);
					System.out.println(res);
				}
			}
		} catch (Exception e) {
		}
	}
}

public class main {
	public static void main(String[] args) {
		PassOne obj = new PassOne();
	}
}