Question: implement the symbol table that will be used by the assembler during pass 1 and pass 2. It should be constructed as an efficient hashing
implement the symbol table that will be used by the assembler during pass 1 and pass 2. It should be constructed as an efficient hashing table. You should construct a "main" routine that will invoke the symbol table operations. The main routine should read a file name off the command line. The file will consist of a character string and an optional number one per line. For example the file might look like: moss 25 eno fred gorge 18 The actions should follow the following rules. 1) upon seeing moss 25 hash moss creating a location in an array. if moss already exists, report an error, for example: (ERROR moss already exists at location 8) if moss does not exist, store the name and its number when moss is stored print a lines such as: stored moss 25 at location 8 (where 8 is the array index where moss is stored reporting collisions, if necessary) 2) upon seeing eno hash eno to find the location in the array where eno may or may not exist. if eno does not exist, report an error (ERROR eno not found) if eno does exist, report the location in the array and its number. (eno found at location 12 with value 433)
have at least one printed line for each input line in the file. (Maybe more when printing the occurrence of collisions) You must be able to handle collisions. You must write a hashing function, you may not use one built in the language.
import java.io.*;
public class Sys { //hash function public static String[] hash(String[] input) { int sz = input.length; //size of input array String[] hashedArray=new String[sz]; for(int i=0; i { hashedArray[i]=null; } for (String x : input) { String[] strParts = x.split(" "); int hashLoc = Integer.parseInt(strParts[1])%sz; int i=0; while(hashedArray[hashLoc]!=null) { hashLoc = (Integer.parseInt(strParts[1])+i)%sz; i=i+1; } hashedArray[hashLoc] = strParts[0]; } return hashedArray; }
public static void main(String[] args)throws Exception{ String inpfile=args[0]; FileReader fr= new FileReader(inpfile); BufferedReader br = new BufferedReader(fr); String st, inputStrings=""; //read strings from file System.out.println("Reading strings from file..."); while ((st= br.readLine()) != null) { System.out.println(st); inputStrings+=st+";"; } System.out.println("Reading strings from file complete!"); String Inputs[]=inputStrings.split(";"); //call hash() function String[] hashedArray = hash(Inputs); //print hashed array System.out.println("Printing hashed array..."); for(int i=0;i { System.out.println("Location:"+i+",Key:"+hashedArray[i]); } //close readers br.close(); fr.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
