-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
284 lines (266 loc) · 8.79 KB
/
lexer.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Two Pass Assembler:
"helper.h" - Part of a set of programs
Includes:
driver.cpp - maintains the flow of the Assembler while handling serious errors
lexer.h - provides functions which defines the lexical structure of program and do tokenisation alongwith basic syntax checking
parser.h - provides functions which gives meaning to the assembly code and convert it to machine/object code
symbolTable.h - provides functions which builds the symbol tabel for the provided assmebly code with looking for some context specific errors
errorHandler.h - provides functions which handles all the errors encountered along the flow of Assmebler
helper.h - provides non assembler specific functions which helps in smoothing of operations needed to be done at various points in the flow of Assembler
printer.h - provides functions which helps in printing the data recieved and information processed
Functionality:
Converts a given assembly code present in some txt file into its adequate machine/object code and writes it down in another txt file
Created by: Abhimanyu Gupta
*/
#include"errorHandler.h"
#include"helper.h"
#ifndef LEXER_H
#define LEXER_H
class Lexer
{
public:
Lexer();
static std::vector<std::vector<std::string> > syntaxCheckWithTokenization(std::vector<std::string>,std::vector<std::pair<std::string,int> >);
static std::vector<std::string> handleLocationCounter(std::vector<std::vector<std::string> >);
private:
static std::string checkErrors_GiveCodeSnippet(int,std::string);
static bool isAlienCharacter(char);
static std::vector<std::string> getLineTokens_CheckForValidity(int,std::string,std::vector<std::pair<std::string,int>>);
};
Lexer::Lexer()
{
}
std::vector<std::vector<std::string> > Lexer::syntaxCheckWithTokenization(std::vector<std::string> lines,std::vector<std::pair<std::string,int> > opCodes)
{
std::vector<std::vector<std::string> > tokens;
std::string codeLine;
std::vector<std::string> t;
for(int i=0;i<lines.size();i++)
{
codeLine=Lexer::checkErrors_GiveCodeSnippet(i,lines[i]);
t=Lexer::getLineTokens_CheckForValidity(i,codeLine,opCodes);
if(t.size()!=0)
{
if(t.size()==1 && t[0][t[0].size()-1]!=':')
t[0]=Helper::toUpperString(t[0]);
else if(t.size()==2)
{
if(t[0][t[0].size()-1]==':')
t[1]=Helper::toUpperString(t[1]);
else
t[0]=Helper::toUpperString(t[0]);
}
else if(t.size()==3)
t[1]=Helper::toUpperString(t[1]);
}
tokens.push_back(t);
}
for(int i=tokens.size()-1;i>=0;i--)
{
if(tokens[i].size()==0)
continue;
else if(tokens[i].size()>=2)
throw ErrorHandler::endStatementMissing(i+1);
else
{
if(!Helper::getKeywordType(tokens[i][0],opCodes)==0)
throw ErrorHandler::endStatementMissing(i+1);
else
break;
}
}
return tokens;
}
std::string Lexer::checkErrors_GiveCodeSnippet(int l,std::string s)
{
std::string ss=s;
std::replace(ss.begin(),ss.end(),'\t',' ');
int s1,s2;
int s3=-1;
s1=ss.find_first_not_of(" ");
if(s1!=std::string::npos)
{
ss=ss.substr(s1,s.size()-s1);
s2=ss.find_last_not_of(" ");
if(s2!=std::string::npos)
{
ss=ss.substr(0,s2+1);
s2+=s1+1;
s3=ss.find(" ");
if(s3==std::string::npos)
s3=-1;
else
s3+=s1;
}
else
s2=s.size();
}
else
s1=0;
bool foundSharp=false;
bool foundNum=false;
for(int i=s1;i<s2;i++)
{
if(s[i]=='#')//Comment starts
return s.substr(0,i);
else if(s[i]==':') // : exists as part of label only
{
if(foundSharp)
throw ErrorHandler::invalidSymbol(l+1,i+1,s[i]);
foundSharp=true;
if(i==s1)
throw ErrorHandler::invalidSymbol(l+1,i+1,s[i]);
else if(s[i-1]==' ' || s[i-1]=='\t')
throw ErrorHandler::invalidSymbol(l+1,i+1,s[i]);
else if(s3==-1 && i!=s2)
throw ErrorHandler::invalidSymbol(l+1,i+1,s[i]);
else if(s3!=-1 && i>s3)
throw ErrorHandler::invalidSymbol(l+1,i+1,s[i]);
}
else if(std::isdigit(s[i])) //exists as numeric value
{
if(foundNum)
ErrorHandler::invalidNaming(l+1);
if(i<(s2-1) && !std::isdigit(s[i+1]))
foundNum=true;
if(i==s1)
ErrorHandler::invalidNaming(l+1);
else if(i<(s2-1) && !std::isdigit(s[i+1]) && s[i+1]!=' ' && s[i+1]!='\t' && s[i+1]!='#')
ErrorHandler::invalidNaming(l+1);
else if(s3==-1)
ErrorHandler::invalidNaming(l+1);
else if(s3!=-1 && i<s3)
ErrorHandler::invalidNaming(l+1);
else if(!std::isdigit(s[i-1]) && s[i-1]!=' ' && s[i-1]!='\t')
ErrorHandler::invalidNaming(l+1);
}
else if(Lexer::isAlienCharacter(s[i]))
throw ErrorHandler::invalidSymbol(l+1,i+1,s[i]);
}
return s;
}
bool Lexer::isAlienCharacter(char c)
{
if(c=='#')
return false;
else if(c==' ' || c=='\t')
return false;
else if(c==':')
return false;
else if(std::isalpha(c))
return false;
else if(std::isdigit(c))
return false;
else
return true;
}
std::vector<std::string> Lexer::getLineTokens_CheckForValidity(int l,std::string s,std::vector<std::pair<std::string,int>> opCodes)
{
std::vector<std::string> tokens;
tokens=Helper::splitOnSpaces(s);
if(tokens.size()==0)
return tokens;
else if(tokens.size()==1)
{
if(tokens[0][tokens[0].size()-1]==':') //Label
{
if(Helper::isTokenKeyword(Helper::toUpperString(tokens[0].substr(0,tokens[0].size()-1)),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[1],"Reserved Name");
else
return tokens;
}
else if(Helper::isTokenKeyword(Helper::toUpperString(tokens[0]),opCodes))
{
if(Helper::getKeywordType(Helper::toUpperString(tokens[0]),opCodes)<=1)
return tokens;
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0]);
}
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0],"Invalid Operand");
}
else if(tokens.size()==2)
{
if(tokens[0][tokens[0].size()-1]==':') //Label
{
if(Helper::isTokenKeyword(Helper::toUpperString(tokens[0].substr(0,tokens[0].size()-1)),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0].substr(0,tokens[0].size()-1),"Reserved Name");
else if(Helper::isTokenKeyword(Helper::toUpperString(tokens[1]),opCodes))
{
if(Helper::getKeywordType(Helper::toUpperString(tokens[1]),opCodes)<=1)
return tokens;
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[1]);
}
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[1],"Invalid Operand");
}
else if(Helper::isTokenKeyword(Helper::toUpperString(tokens[0]),opCodes))
{
if(Helper::getKeywordType(Helper::toUpperString(tokens[0]),opCodes)<=1)
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0],"Invalid Operand type");
else if(Helper::isTokenKeyword(Helper::toUpperString(tokens[1]),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[1],"Reserved Name");
else
return tokens;
}
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0],"Invalid Operand");
}
else if(tokens.size()==3)
{
if(tokens[0][tokens[0].size()-1]!=':')
{
if(Helper::isTokenKeyword(Helper::toUpperString(tokens[0]),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0],"Invalid Operand type");
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0],"Invalid Operand");
}
else
{
if(Helper::isTokenKeyword(Helper::toUpperString(tokens[0].substr(0,tokens[0].size()-1)),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[0].substr(0,tokens[0].size()-1),"Reserved Name");
else if(!Helper::isTokenKeyword(Helper::toUpperString(tokens[1]),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[1],"Invalid Operand");
else
{
if(Helper::getKeywordType(Helper::toUpperString(tokens[1]),opCodes)<=1)
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[1],"Invalid Operand type");
else
{
if(Helper::isTokenKeyword(Helper::toUpperString(tokens[2]),opCodes))
throw ErrorHandler::invalidInstruction(l+1,tokens.size(),tokens[2],"Reserved Name");
else
return tokens;
}
}
}
}
else
throw ErrorHandler::invalidInstruction(l+1,tokens.size());
}
std::vector<std::string> Lexer::handleLocationCounter(std::vector<std::vector<std::string> > tokens)
{
std::vector<std::string> lc;
int c=1;
for(int i=0;i<tokens.size();i++)
{
if(c>=128)
throw::ErrorHandler::memoryExhausted(i+1);
else
{
if(tokens[i].size()==0)
lc.push_back("");
else if(tokens[i].size()==1 && tokens[i][0][tokens[i][0].size()-1]==':')
lc.push_back("");
else
{
lc.push_back(Helper::integerToBinary(8,c));
c+=12;
}
}
}
return lc;
}
#endif