1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <string>
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
// If no extra file is provided then exit the program with error message
if (argc <= 1)
{
cout << "Correct Usage: " << argv[0] << " <Filename>" << endl;
exit (1);
}
// Array to hold the registers and initialize them all to zero
int registers [] = {0,0,0,0,0,0,0,0};
string memory [16000];
string Symtablelab[1000];
int Symtablepos[1000];
char line[100];
char label [9];
char opcode[9];
char arg1[256];
char arg2[256];
char* pch;
// Open the file that was input on the command line
ifstream myFile;
myFile.open(argv[1]);
if (!myFile.is_open())
{
cerr << "Cannot open the file." << endl;
}
int counter = 0;
int i = 0;
int j = 0;
while (myFile.good())
{
myFile.getline(line, 100, '\n');
// If the line begins with a #, then just get the next line
if (line[0] == '#')
{
continue;
}
// If there is a label, then this code will run
if ( line[0] != '\t' && line[0]!=' ')
{
if( pch = strtok(line," \t"))
{
strcpy(label,pch);
Symtablelab[i] = (string)label;
}
if (pch = strtok(NULL, " \t"))
{
strcpy(opcode,pch);
if (opcode != "WORDS" && opcode != "INT")
{
counter += 3;
}
else if (opcode == "INT")
counter ++;
}
if (pch = strtok(NULL, " \t,"))
{
strcpy(arg1,pch);
if (opcode == "WORDS")
{
int j = atoi(arg1);
counter += j;
}
}
if (pch = strtok(NULL, ","))
{
strcpy(arg2, pch);
}
Symtablepos[i] = counter;
}
if (line[0] == '\t' || line[0] == ' ')
{
if (pch = strtok(line, " \t"))
{
strcpy(opcode,pch);
if (opcode != "WORDS" && opcode != "INT")
{
counter += 3;
}
else if (opcode == "INT")
counter++;
}
if (pch = strtok(NULL, " \t,"))
{
strcpy(arg1,pch);
if (opcode == "WORDS")
{
j = atoi(arg1);
counter += j;
}
}
if (pch = strtok(NULL, ","))
{
strcpy(arg2,pch);
}
}
i++;
}
for (int x = 0; x <= i; x++)
{
cout << Symtablelab[i] << ' ' << Symtablepos[i] << endl;
}
return 0;
}
| |