symbol

 import java.util.*;


class Symbol {

    String name;

    int address;


    public Symbol(String name, int address) {

        this.name = name;

        this.address = address;

    }

}


public class SymbolTableGenerator {


    // Check if symbol exists in the table

    public static boolean symbolExists(List<Symbol> table, String label) {

        for (Symbol sym : table) {

            if (sym.name.equals(label)) {

                return true;

            }

        }

        return false;

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        List<String> code = new ArrayList<>();

        List<Symbol> symbolTable = new ArrayList<>();


        System.out.println("Enter Assembly Code (type 'END' to finish input):");


        // Read input lines

        while (true) {

            String line = scanner.nextLine().trim();

            if (line.equalsIgnoreCase("END")) {

                break;

            }

            if (!line.isEmpty()) {

                code.add(line);

            }

        }

        code.add("END"); // Ensure END is processed


        int locationCounter = 0;

        boolean startFound = false;


        System.out.println("\n--- PASS 1: Generating Symbol Table ---");


        for (String stmt : code) {

            String[] tokens = stmt.trim().split("\\s+");


            if (tokens[0].equalsIgnoreCase("START")) {

                locationCounter = Integer.parseInt(tokens[1]);

                startFound = true;

                continue;

            }


            if (!startFound) {

                System.out.println("Error: START directive not found.");

                return;

            }


            String label = null;

            String opcode = "";

            String operand = "";


            List<String> opcodes = Arrays.asList("READ", "MOVER", "COMP", "BC", "SUB", "STOP", "DS", "END");


            if (!opcodes.contains(tokens[0].toUpperCase())) {

                label = tokens[0];

                if (tokens.length > 1) {

                    opcode = tokens[1].toUpperCase();

                }

                if (tokens.length > 2) {

                    operand = tokens[2];

                }

            } else {

                opcode = tokens[0].toUpperCase();

                if (tokens.length > 1) {

                    operand = tokens[1];

                }

            }


            if (opcode.equals("DS")) {

                if (label != null && !symbolExists(symbolTable, label)) {

                    symbolTable.add(new Symbol(label, locationCounter));

                }

                locationCounter += 1; // assuming DS 1

                continue;

            }


            if (label != null && !symbolExists(symbolTable, label)) {

                symbolTable.add(new Symbol(label, locationCounter));

            }


            locationCounter += 1;

        }


        System.out.println("\n--- SYMBOL TABLE ---");

        System.out.printf("%-10s %-10s\n", "Symbol", "Address");

        for (Symbol sym : symbolTable) {

            System.out.printf("%-10s %-10d\n", sym.name, sym.address);

        }

    }

}

#ip
START 100
LOOP MOVER AREG, X
      ADD B
      SUB C
      BC END
X     DS 1
B     DS 1
C     DS 1
      STOP
END

Comments

Popular posts from this blog

literal

yacc evaluate

built in functions