Java Compiler Creation Help..Please

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-04-06T17:36:16Z Indexed on 2010/04/06 17:53 UTC
Read the original article Hit count: 272

Filed under:

I need some help with my code here...What we are trying to do is make a compiler that will read a file containing Machine Code and converting it to 100 lines of 4 bits example: this code is the machine code being converting to opcode and operands. I need some help please.. thanks

799
798
198
499
1008
1108
899
909
898
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Everything compiles but when I go and run my Test.java I get the following OutPut:

Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Scanner.java:1516)
        at Compiler.FirstPass(Compiler.java:22)
        at Compiler.compile(Compiler.java:11)
        at Test.main(Test.java:5)

Here is my class Compiler:

import java.io.*;
import java.io.DataOutputStream;
import java.util.NoSuchElementException;
import java.util.Scanner;
class Compiler{
    private int lc = 0;
    private int dc = 99;
    public void compile(String filename)
    {
        SymbolList symbolTable = FirstPass(filename);
        SecondPass(symbolTable, filename);
    }
    public SymbolList FirstPass(String filename)
    {
        File file = new File(filename);
        SymbolList temp = new SymbolList();
        int dc = 99;
        int lc = 0;
        try{
            Scanner scan = new Scanner(file);
            String line = scan.nextLine();
            String[] linearray = line.split(" ");
            while(line!=null){
                if(!linearray[0].equals("REM")){
                    if(!this.isInstruction(linearray[0])){
                        linearray[0]=removeColon(linearray[0]);
                        if(this.isInstruction(linearray[1])){
                            temp.add(new Symbol(linearray[0], lc, null));
                            lc++;
                        } else {
                            temp.add(new Symbol(linearray[0], dc, Integer.valueOf((linearr\
ay[2]))));
                            dc--;
                        }
                    } else {
                        if(!linearray[0].equals("REM"))
                            lc++;
                    }
                }
                try{
                    line = scan.nextLine();
                } catch(NoSuchElementException e){
                    line=null;
                    break;
 }
                linearray = line.split(" ");
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return temp;
    }
 public String makeFilename(String filename)
    {
        return filename + ".ex";
    }

    public String removeColon(String str)
    {
        if(str.charAt(str.length()-1) == ':'){
            return str.substring(0, str.length()-1);
        } else {
            return str;
        }
    }
 public void SecondPass(SymbolList symbolTable, String filename){
        try {
            int dc = 99;
            //Open file for reading
            File file = new File(filename);
            Scanner scan = new Scanner(file);

            //Make filename of new executable file

            String newfile = makeFilename(filename);

            //Open Output Stream for writing new file.

            FileOutputStream os = new FileOutputStream(filename);
            DataOutputStream dos = new DataOutputStream(os);

            //Read First line. Split line by Spaces into linearray.
            String line = scan.nextLine();
            String[] linearray = line.split(" ");

            while(scan.hasNextLine()){
                if(!linearray[0].equals("REM")){
                    int inst=0, opcode, loc;

                    if(isInstruction(linearray[0])){
                        opcode = getOpcode(linearray[0]);
                        loc = symbolTable.searchName(linearray[1]).getMemloc();
                        inst = (opcode*100)+loc;

                    } else if(!isInstruction(linearray[0])){
                        if(isInstruction(linearray[1])){
                            opcode = getOpcode(linearray[1]);
                            if(linearray[1].equals("STOP"))
                                inst=0000;
                            else {
                                loc = symbolTable.searchName(linearray[2]).getMemloc();
                                inst = (opcode*100)+loc;
                            }
                        }
if(linearray[1].equals("DC"))
                            dc--;
                    }
                    System.out.println(inst);
                    dos.writeInt(inst);
                    linearray = line.split(" ");
                }

                if(scan.hasNextLine())
                    {
                        line = scan.nextLine();
                    }


            }
            scan.close();
            for(int i = lc; i <= dc; i++)
                {
                    dos.writeInt(0);

                }
 for(int i = dc+1; i<100; i++){
                dos.writeInt(symbolTable.searchLocation(i).getValue());
                if(i!=99)
                    dos.writeInt(0);
            }

            dos.close();
            os.close();
        }
        catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
public int getOpcode(String inst){
        int toreturn = -1;
        if(isInstruction(inst)){

            if(inst.equals("STOP"))
                toreturn=0;
            if(inst.equals("LD"))
                toreturn=1;
            if(inst.equals("STO"))
                toreturn=2;
            if(inst.equals("ADD"))
                toreturn=3;
            if(inst.equals("SUB"))
                toreturn=4;
            if(inst.equals("MPY"))
                toreturn=5;
            if(inst.equals("DIV"))
                toreturn=6;
            if(inst.equals("IN"))
                toreturn=7;
            if(inst.equals("OUT"))
                toreturn=8;
            if(inst.equals("B"))
                toreturn=9;
            if(inst.equals("BGTR"))
                toreturn=10;
            if(inst.equals("BZ"))
                toreturn=11;
 return toreturn;
        } else {
            return -1;
        }
    }

    public boolean isInstruction(String totest){
        boolean toreturn = false;
        String[] labels = {"IN", "LD", "SUB", "BGTR", "BZ", "OUT", "B", "STO", "STOP", "AD\
D", "MTY", "DIV"};
        for(int i = 0; i < 12; i++){
            if(totest.equals(labels[i]))
                toreturn = true;
        }
        return toreturn;
    }
}

And here is my class Computer:

import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
class Computer{
    private Cpu cpu;
    private Input in;
    private OutPut out;
    private Memory mem;
    public Computer() throws IOException
    {
        Memory mem = new Memory(100);
        Input in = new Input();
        OutPut out = new OutPut();
        Cpu cpu = new Cpu();
        System.out.println(in.getInt());
    }
    public void run() throws IOException
    {
        cpu.reset();
        cpu.setMDR(mem.read(cpu.getMAR()));
        cpu.fetch2();
        while (!cpu.stop())
            {
                cpu.decode();
                if (cpu.OutFlag())
                    OutPut.display(mem.read(cpu.getMAR()));
                if (cpu.InFlag())
                    mem.write(cpu.getMDR(),in.getInt());
                if (cpu.StoreFlag())
                    {
                        mem.write(cpu.getMAR(),in.getInt());
                        cpu.getMDR();
                    }
                else
                    {
                        cpu.setMDR(mem.read(cpu.getMAR()));
                        cpu.execute();
                        cpu.fetch();
                        cpu.setMDR(mem.read(cpu.getMAR()));
                        cpu.fetch2();
                    }
            }
    }
 public void load()
    {
        mem.loadMemory();
    }
}

Here is my Memory class:

import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
class Memory{
    private MemEl[] memArray;
    private int size;
    private int[] mem;
    public Memory(int s)
    {size = s;
        memArray = new MemEl[s];
        for(int i = 0; i < s; i++)
            memArray[i] = new MemEl();
    }
    public void write (int loc,int val)
    {if (loc >=0 && loc < size)
            memArray[loc].write(val);
        else
            System.out.println("Index Not in Domain");
    }
    public int read (int loc)
    {return memArray[loc].read();
    }
    public void dump()
    {
        for(int i = 0; i < size; i++)
            if(i%1 == 0)
                System.out.println(memArray[i].read());
            else
                System.out.print(memArray[i].read());
    }
    public void writeTo(int location, int value)
    {
        mem[location] = value;
    }

    public int readFrom(int location)
    {
        return mem[location];
    }

    public int size()
    {
        return mem.length;
    }
public void loadMemory() {
        this.write(0, 799);
        this.write(1, 798);
        this.write(2, 198);
        this.write(3, 499);
        this.write(4, 1008);
        this.write(5, 1108);
        this.write(6, 899);
        this.write(7, 909);
        this.write(8, 898);
        this.write(9, 0000);
    }
 public void loadFromFile(String filename){
        try {
            FileReader fr = new FileReader(filename);
            BufferedReader br = new BufferedReader(fr);

            String read=null;
            int towrite=0;
            int l=0;
            do{
                try{
                    read=br.readLine();
                    towrite = Integer.parseInt(read);
                }catch(Exception e){

                }
                this.write(l, towrite);
                l++;
            }while(l<100);
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Here is my Test class:

public class Test{

    public static void main(String[] args) throws java.io.IOException {
        Compiler compiler = new Compiler();
        compiler.compile("program.txt");


    }

}

© Stack Overflow or respective owner

Related posts about java