Skip to content

Commit

Permalink
Assignment 2 First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Oct 3, 2016
1 parent 4bf4f8d commit 5c20201
Show file tree
Hide file tree
Showing 12 changed files with 721 additions and 278 deletions.
45 changes: 45 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import OperandPkg.LinkedList;
import OperandPkg.Operand;
import OperandPkg.OperandUtility;
import SymbolPkg.*;


import java.io.*;


// NAME : Quazi Irfan
// CLASS : CSc 354
// ASSIGNMENT : 1
// DUE DATE : 9/14/16
// INSTRUCTOR : Dr. Hamer
// DESCRIPTION : Assignment 1 : Symbol Table


/**
* Main class of Assignment 1. This class is within SumbolTable package.
*
* Currently everything is contained within static Main method.
* Three stages are labeled as Stage X. This code mostly read the file and
* delegates responsibilities to other classes.
*
*/
public class Main {
public static void main(String[] args) throws IOException{
SymbolTable symbolTable = new SymbolTable();
LinkedList<Operand> literalLnkdLst = new LinkedList<>();

if(args.length < 2){
System.out.println("Please use command \"javac Main labels.txt operands.txt");
return;
}

String symbolFile = args[0];
SymbolUtility.populateSymbolTable(symbolTable, symbolFile);

String operandFile = args[1];
OperandUtility.evaluateOperand(symbolTable, literalLnkdLst, operandFile);

// print the literal linked list
}

}
7 changes: 7 additions & 0 deletions src/OperandPkg/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package OperandPkg;

public class LinkedList<T> {
public T parent;
public Operand operand;
public T child;
}
7 changes: 7 additions & 0 deletions src/OperandPkg/Operand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package OperandPkg;

public class Operand {
public String expression;
public int value;
public boolean relocability, nbit, ibit, xbit;
}
126 changes: 126 additions & 0 deletions src/OperandPkg/OperandUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package OperandPkg;

import SymbolPkg.Node;
import SymbolPkg.SymbolTable;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class OperandUtility{
public static void evaluateOperand(SymbolTable symbolTable, LinkedList<Operand> literalLnkdLst, String operandFile) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(operandFile));

String expression = reader.readLine();
while(expression != null){
Operand operand = new Operand();
operand.expression = expression;

if(expression.charAt(0) == '=') {
expression = reader.readLine();
continue;
}

if(expression.charAt(0) != '=')
expression = expression.toUpperCase();

if(expression.charAt(0) == '#'){
operand.ibit = true;
expression = expression.substring(1);
}
else if(expression.charAt(0) == '@'){
operand.nbit = true;
expression = expression.substring(1);
} else if(Character.isDigit(expression.charAt(0))){
operand.nbit = false;
operand.ibit = true;
}
else {
operand.nbit = true;
operand.ibit = true;
}

if(expression.length() >= 3 && expression.substring(expression.length()-2, expression.length()).equals(",X")){
if(operand.nbit & operand.ibit == false){
System.out.println("@ or # can't be mixed with ,X");
return;
}
operand.xbit = true;
expression = expression.substring(0, expression.length()-2);
}

validateExp(expression, operand, symbolTable);

print(operand);

// go to the next line
expression = reader.readLine();
}
}

private static void validateExp(String expression, Operand operand, SymbolTable symbolTable){
if(expression.indexOf('+') >= 0 || expression.indexOf('-') >= 0){
int token1Value =0, token2Value=0;
boolean token1rflag=false, token2rflag=false;

StringTokenizer tokenizer = new StringTokenizer(expression, "+-");

String temp = tokenizer.nextToken();
token1Value = Token.getTokenValue(temp, symbolTable);
token1rflag = Token.getTokenRflag(temp, symbolTable);

temp = tokenizer.nextToken();
token2Value = Token.getTokenValue(temp, symbolTable);
token2rflag = Token.getTokenRflag(temp, symbolTable);

// set operands value, and flag
if(expression.indexOf('+') >= 0)
operand.value = token1Value + token2Value;
else
operand.value = token1Value - token2Value;

if(evaluateRelocability(token1rflag, token2rflag, expression).equals("true"))
operand.relocability = true;
else
operand.relocability = false;

} else {
operand.value = Token.getTokenValue(expression, symbolTable);
operand.relocability = Token.getTokenRflag(expression, symbolTable);
}
}

public static void print(Operand operand){
System.out.print( "\n" + operand.expression + "\t\t" + operand.value + "\t");
System.out.print( operand.relocability? "Relative" : "Absolute" );
System.out.print( operand.nbit? " 1 ":" 0 ");
System.out.print( operand.ibit? " 1 ":" 0 ");
System.out.print( operand.xbit? " 1 ":" 0 ");
}

private static String evaluateRelocability(boolean token1rflag, boolean token2rflag, String expression){
if(expression.indexOf('+') >= 0){
if(token1rflag == false & token2rflag == false)
return "false";
if(token1rflag == false & token2rflag == true)
return "true";
if(token1rflag == true & token2rflag == false)
return "true";
if(token1rflag == true & token2rflag == true)
return "error";
} else if(expression.indexOf('-') >= 0){
if(token1rflag == false & token2rflag == false)
return "false";
if(token1rflag == false & token2rflag == true)
return "error";
if(token1rflag == true & token2rflag == false)
return "true";
if(token1rflag == true & token2rflag == true)
return "false";
}

return null;
}
}

57 changes: 57 additions & 0 deletions src/OperandPkg/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package OperandPkg;

import SymbolPkg.Node;
import SymbolPkg.SymbolTable;

class Token{
public static boolean getTokenRflag(String token, SymbolTable symbolTable){
boolean operantRflag = false;

Integer tokenValue = null;
try {
tokenValue = Integer.valueOf(token);
} catch (NumberFormatException e){
}

Node node = new Node();

if(tokenValue == null){
// Symbol is not a number
node = symbolTable.search(token);
if(node != null){
// symbol found in the symbol table. Return the rflag
operantRflag = node.getRflag();
}
} else {
// Symbol is a number, return false
operantRflag = false;
}

return operantRflag;
}

public static Integer getTokenValue(String token, SymbolTable symbolTable){
Integer operandValue = null;

Integer tokenValue = null;
try {
tokenValue = Integer.valueOf(token);
} catch (NumberFormatException e){
}

Node node = new Node();

if(tokenValue == null){
// Symbol is not a number
node = symbolTable.search(token);
if(node != null){
operandValue = node.getValue();
}
} else {
// Symbol is a number
operandValue = tokenValue;
}

return operandValue;
}
}
65 changes: 58 additions & 7 deletions src/SymbolTable/BinaryTree.java → src/SymbolPkg/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package SymbolTable;
package SymbolPkg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* BinaryTree class manages the Binary Tree.
* It provides insert, search and view the contents of the Binary Tree.
* It doesn't check for any error in the Node.
* When viewing the content of the BST, after evey 20 lines the user is prompted to press Enter key to continue.
*
* When inserting, searching and viewing the binary tree, it uses a public no-argument method(s) which in turns calls
* a private method. The private method is a recursive method that does all the heavy lifting.
*
*/

public class BinaryTree{
private Node rootNode;
private int count = 0;

private int viewCount = 0;

/**
* Private method to insert the node to the binary tree.
*
* @param parentNode Current node. We start with the rootNode as parent Node,
* and if it's null, the new node is the root node.
* @param newNode New Node we want to insert.
*/
private void insert(Node parentNode, Node newNode){
// If the parentNode is null, this is the first node in the tree
if(parentNode == null){
Expand All @@ -31,18 +49,31 @@ else if(newNode.getKey().compareTo(parentNode.getKey()) > 0){
}
}

// Not necessay, because duplication check is done before calling insert method
// Not necessay, because duplication check is done before calling insert method
// else if(newNode.getKey().compareTo(parentNode.getKey()) == 0){
// // Duplicate key - set mflag to false
// }

}

/**
* Insert a new node in the binary tree.
*
* @param newNode New node we want to insert.
*/
public void insert(Node newNode){
// insert starts with comparing with rootNode
insert(this.rootNode, newNode);
}

/**
* Private search method of binary tree. This method is called by the public one argument search method.
*
* @param parentNode We start search with parent node as the rootNode.
* @param node the node we are looking for.
* @return Returns null if search yeilds no result.
*/

private Node search(Node parentNode, Node node){
if(parentNode == null){
return null;
Expand All @@ -61,11 +92,24 @@ else if(node.getKey().compareTo(parentNode.getKey()) > 0){
return null;
}

/**
* Searches for a Node is the Binary Tree
*
* @param node search for this node
* @return returns the node if found
*/

public Node search(Node node){
// search starts with comparing with rootNode
return search(this.rootNode, node);
}

/**
* This protected view method is the actual recursive method that travels the binary tree.
*
* @param node It starts with the rootNode and recursively other nodes are passed through
* this parameter until the leaves are reached.
*/
private void view(Node node){
if(node.leftNode != null){
view(node.leftNode);
Expand All @@ -74,9 +118,9 @@ private void view(Node node){
// TODO add it to another Data structure, that can be accessed via binaryTree.getAll()
{
System.out.println(node);
count++;
if((count % 20)==0){
System.out.println("\n Press Enter key to see the next 20 symbols.");
viewCount++;
if((viewCount % 20)==0){
System.out.println(" * Press Enter key to see the next 20 symbols.");
try{
BufferedReader tempHalt = new BufferedReader(new InputStreamReader(System.in));
tempHalt.readLine();
Expand All @@ -92,6 +136,13 @@ private void view(Node node){
}
}

/**
* Prints the BinaryTree on the console
*
* This is a convenience method. Internally this method calls
* view(rootNode).
*
*/
public void view(){
if(rootNode != null){
view(rootNode);
Expand Down
Loading

0 comments on commit 5c20201

Please sign in to comment.