-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyser.c
127 lines (84 loc) · 3.03 KB
/
Analyser.c
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
#include<stdio.h>
#include<ctype.h>
#include<string.h>
//*************************** CREATED BY JINTU KUMAR DAS *********************************************
//---------------------- DATE : 3/11/2017 ----------------------------------
// KNOWN ISSUE : Duplicate values not handled
main(void)
{
FILE *open;
open = fopen("assembly.txt", "r"); //Replace assembly.txt with your assembly language text file
FILE *write;
write = fopen("tokens.txt", "w"); //Tokens will stored in this file (Autogenerated)
FILE *a;
//Declaring string arrays
char *mn[50] = {"ADD","DIV","MULT","SUB","READ","MOV","START","STOP"}; //Mnemonic codes
char *reg [50]= {"AREG","BREG","CREG","DREG"}; //Registors
char *symbols[50] = {"!","#","%","$",",","'"}; //Symbols
char *aDir[50] = {"DS","DC","LTORG","ORIGIN","EQU"}; //Assembly directives
int opCode[50] = {0001,0002,0003,0004,0005,0006,0007,0008}; //Opcodes
char ch;
char str[127]; //String buffer
int i;
//Reading content of assembly file
while ((ch = fgetc(open)) != EOF)
{
if (ch == 32 || ch == ',') //If space found start new line or if , found write it on the file
if(ch == ',')
fprintf(write, "\n,\n");
else
fprintf(write,"\n");
else
fprintf(write, "%c", ch); //Write individual characters till space or , found
}
//Closing file stream
fclose(write);
fclose(open);
a = fopen("tokens.txt", "r"); //Pointing to token file
while (!feof(a)) //Reading content of tokens.txt
{
int m = 0,r = 0,sym=0,dir=0;
if (fgets(str, 126, a)){ //Getting first line from token file
//checking mnemonic code
for(i=0;i<8;i++){
int res = strncmp(str,mn[i],3); //Comparing the string upto 3 digits
if(res == 0){
printf("%s : Mnemonic code and Corresponding Opcode is : %d\n",str,opCode[i]);
m = 1;
}
}
//checking registors
for(i=0;i<4;i++){
int res = strncmp(str,reg[i],4);
if(res == 0){
printf("%s : Register\n",str);
r = 1;
}
}
//checking symbols
for(i=0;i<6;i++){
int res = strncmp(str,symbols[i],1);
if(res == 0){
printf("%s : Symbol\n",str);
sym = 1;
}
}
//checking assembly directive
for(i=0;i<5;i++){
int res = strncmp(str,aDir[i],2);
if(res == 0){
printf("%s : Assembly directive \n",str);
dir = 1;
}
}
//Checking digits and constant
if(m == 0 && r == 0 && dir == 0 && sym == 0){ //If none of the string matches continue with this if
if(isdigit(str[0])) //Checking for digit
printf("%s : Digit \n",str);
else //Operand
printf("%s : Symbol \n",str);
}
}
} //End of token file
fclose(a); //Closing token file stream
} //Main end