-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
428 lines (369 loc) · 17.7 KB
/
Form1.cs
File metadata and controls
428 lines (369 loc) · 17.7 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
namespace Compiler_Construction
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_file_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Input Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"D:\TestingFiles";
if (theDialog.ShowDialog() == DialogResult.OK)
{
textBox_inputfile.Text=theDialog.FileName.ToString();
}
}
private void button_syntax_Click(object sender, EventArgs e)
{
string path = textBox_inputfile.Text;
List<Token> alToken = getAllTokens(path);
alToken.Add(new Token("$", -1));
SyntaxAnalyzer syntaxAnalObj = new SyntaxAnalyzer(alToken);
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
semanticAnalyzer.print_CT();
string allTokens = "";
foreach (var t in alToken)
{
allTokens += $"({t.ClassPart},{t.ValuePart},{t.Line})\n";
}
bool syntaxresult = syntaxAnalObj.checkRule();
string aa = syntaxresult.ToString();
MessageBox.Show($"Check Syntax: {syntaxresult}");
}
private void button_laxical_Click(object sender, EventArgs e)
{
string path = textBox_inputfile.Text;
string[,] token = new string[100, 2];
List<Token> alToken = getAllTokens(path);
string allTokens = "";
foreach (var t in alToken)
{
allTokens += $"({t.ClassPart},{t.ValuePart},{t.Line})\n";
}
string outputPath = @"C:\Users\Admin\Desktop\token.txt";
File.WriteAllText(outputPath, allTokens);
Process.Start(outputPath);
}
static List<Token> getAllTokens(string a)
{
List<Token> alToken = new List<Token>();
string temp = "";
bool multiLineComment = false;
int no = 0;
string[] text = File.ReadAllLines(a);
for (int i = 0; i < text.Length; i++)
{
string str = text[i];
//Console.WriteLine($"line= {str} length={str.Length}");
for (int j = 0; j < str.Length; j++)
{
//FOR STRING
if (str[j] == '\"' && multiLineComment != true)
{
temp += str[j];
while (j + 1 < str.Length && str[j + 1] != '\"')
{
if (str[j + 1] == '\\' && j + 2 < str.Length && (str[j + 2] == '\"'))
{
temp += str[j + 1].ToString() + str[j + 2].ToString();
j += 2;
}
else
{
j++;
temp += str[j];
}
if (j == str.Length - 1)
break;
}
if (j + 1 < str.Length && str[j + 1] == '\"')
temp += '\"';
j++;
//Console.WriteLine($"temp={temp}");
alToken.Add(new Token(temp, i));
no++;
temp = "";
}
//FOR CHAR
else if (str[j].Equals('\'') && multiLineComment != true)
{
//Console.WriteLine($"char start");
temp += str[j];
if (str[j + 1].Equals('\\'))
{
for (int k = 0; k < 3; k++)
{
if (j + 1 < str.Length)
{
j++;
temp += str[j].ToString();
}
}
}
else
{
for (int k = 0; k < 2; k++)
{
if (j + 1 < str.Length)
{
j++;
temp += str[j].ToString();
}
}
}
//Console.WriteLine($"temp={temp}");
alToken.Add(new Token(temp, i));
no++;
temp = "";
}
//FOR IDENTIFIER
else if (Char.IsLetter(str[j]) && multiLineComment != true)
{
//Console.WriteLine($"word HERE3 {str[j]}");
do
{
temp += str[j];
j++;
if (j == str.Length)
{
break;
}
} while (Char.IsLetter(str[j]) || Char.IsDigit(str[j]) || str[j] == '_' && str[j] != ' ');
// Console.WriteLine($"temp={temp}");
alToken.Add(new Token(temp, i));
j--;
no++;
temp = "";
}
// FOR DIGITS
else if (multiLineComment != true && j < str.Length && (((str[j] == '+' || str[j] == '-') && (Char.IsDigit(str[j + 1]) || (str[j + 1] == '.' && Char.IsDigit(str[j + 2])))) || (str[j] == '.' && Char.IsDigit(str[j + 1])) || Char.IsDigit(str[j])))
{
//bool valid = true;
//Console.WriteLine($"digit HERE3 {str[j]}");
do
{
if (str[j] == '.' && temp.Contains('.'))
break;
if (str[j] == '+' && temp.Contains('+'))
break;
if (str[j] == '-' && temp.Contains('-'))
break;
if (str[j] == 'e' && temp.Contains('e'))
break;
temp += str[j];
j++;
if (j == str.Length)
break;
} while (Char.IsDigit(str[j]) || str[j] == '.' || str[j] == 'e' || str[j] == '+' || str[j] == '-' || (!Char.IsWhiteSpace(str[j]) && Char.IsLetter(str[j])));
//Console.WriteLine($"temp={temp}");
alToken.Add(new Token(temp, i));
j--;
temp = "";
no++;
}
//FOR OPERATORS
else if (multiLineComment != true && j < str.Length && (str[j] == '+' || str[j] == '-' || str[j] == '*' || str[j] == '/' || str[j] == '%' || str[j] == '=' || str[j] == '<' || str[j] == '>' || str[j] == '^' || str[j] == '!' || str[j] == '&' || str[j] == '|')) // +-*/=%
{
//Console.WriteLine($"operator here4 {str[j]}");
temp += str[j];
if (j < str.Length - 1 && ((str[j] == '+' && str[j + 1] == '+') || (str[j] == '-' && str[j + 1] == '-') || (str[j] == '|' && str[j + 1] == '|') || (str[j] == '&' && str[j + 1] == '&'))) //++ -- && ||
temp += str[++j];
else if (j < str.Length - 1 && ((str[j] == '|' && str[j + 1] == '=') || (str[j] == '&' && str[j + 1] == '=')))
temp += "";
else if (j < str.Length - 1 && str[j + 1] == '=') // += -= *= /= == %=
temp += str[++j];
//Console.WriteLine($"temp={temp}");
alToken.Add(new Token(temp, i));
no++;
temp = "";
}
//FOR PUNCTUATOR
else if ((Char.IsPunctuation(str[j]) && str[j] != ':' && str[j] != '#' && multiLineComment != true) || (multiLineComment != true && (str[j] == '`' || str[j] == '_' || str[j] == '~' || str[j] == '@' || str[j] == '\\' || str[j] == '$')))
{
//Console.WriteLine($"punctuator Here => {str[j]}");
alToken.Add(new Token(str[j].ToString(), i));
no++;
}
//For inheritance symbol
else if (multiLineComment != true && j < str.Length && str[j] == ':')
{
Console.WriteLine($":Here j={j}");
if (j < str.Length - 1 && str[j + 1] == ':')
{
temp += str[j].ToString() + str[j + 1].ToString();
// Console.WriteLine($"temp={temp}");
alToken.Add(new Token(temp, i));
temp = "";
j++;
}
else
{
//Console.WriteLine($"temp={temp}");
alToken.Add(new Token(str[j].ToString(), i));
}
}
//FOR SINGLE COMMENT
else if (str[j].Equals('#') && str[j + 1] != '#' && multiLineComment != true)
{
//Console.WriteLine("single comment");
do
{
j++;
} while (j != str.Length);
}
//FOR MULTI LINE COMMENT
else if (str[j].Equals('#') && str[j + 1].Equals('#') || multiLineComment)
{
//Console.WriteLine($"comment here, multiline = {multiLineComment}");
if (multiLineComment)
{
if (j != str.Length - 1 && str[j] != '#' && str[j + 1] != '#')
{
do
{
j++;
if (j == str.Length - 1)
{
multiLineComment = true;
break;
}
} while (str[j] != '#' && str[j + 1] != '#' && j != str.Length - 1);
j++;
if (j != str.Length && str[j].Equals('#') && str[j + 1].Equals('#'))
{
//Console.WriteLine($"outside while j= {j}");
j++;
if (multiLineComment)
multiLineComment = false;
}
}
else
{
if (str[j] == '#' && str[j + 1] == '#')
multiLineComment = false;
j++;
}
}
else
{
j++;
do
{
j++;
if (j == str.Length - 1)
{
multiLineComment = true;
break;
}
} while (str[j] != '#' && str[j + 1] != '#' && j != str.Length - 1);
j++;
if (j != str.Length && str[j].Equals('#') && str[j + 1].Equals('#'))
{
j++;
if (multiLineComment)
multiLineComment = false;
}
}
}
}
alToken.Add(new Token("\\r", i));
}
return alToken;
}
private void button_semantic_Click(object sender, EventArgs e)
{
string outputPath = @"C:\Users\Admin\Desktop\CompilerFinal\maintable.txt";
Process.Start(outputPath);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
class Token
{
string valuePart;
string classPart;
int line;
public Token(string ValuePart, int Line)
{
valuePart = ValuePart;
line = Line+1;
classPart = identifyClass(ValuePart);
}
public string ClassPart { get { return classPart; } set { classPart = value; } }
public string ValuePart { get { return valuePart; } set { valuePart = value; } }
public int Line { get { return line; } set { line = value; } }
string identifyClass(string value)
{
Regex idrgx = new Regex(@"^[a-zA-Z][\w]*$");
Regex charrgx = new Regex(@"^'.{1}'$|^'\\.{1}'$");
Regex digrgx = new Regex(@"(^[+-]?\d+)(?:[eE]([-+]?\d+))?$");
Regex floatrgx = new Regex(@"^([+-]?\d*[.]{1}\d+)(?:[eE]([-+]?\d+))?$");
Regex strrgx = new Regex(@"^[""](\W|\w)*[""]$");
Regex boolrgx = new Regex(@"^true$|^false$");
string[,] keywords = new string[,] { {"int","DT"}, { "double", "DT" }, { "char", "DT" }, { "float", "DT" }, { "bool", "DT" },{ "string", "DT" },{"foreach", "foreach"},{"while","while"},{"return","return"},{"label","label"},
{"var","var"},{"goto","goto"},{"switch","switch"},{"case","case"},{"default","default"},{"break","break"},{"continue","continue"},{"new","new"},{"void","void"},{"print","print"},
{"end","end"},{"if","if"},{"else","else"},{"private","private"},{"public","public"},{"protected","protected"},{"class","class"},{"Main","Main"},{"abstract","abstract"},
{"final","final"},{"static","static"},{"null","null"},{"virtual","virtual"},{"override","override"},{"this","this"},{"in","in"},{"const","const"},{"interface","interface"},{"extends","extends"} };
string[,] operators = new string[,] { {"+","PM"}, {"-","PM"}, {"*","MDM"}, {"/","MDM"}, {"%","MDM"}, {"&&","AND"}, {"||","OR"}, {"!","!"}, {"<","RO"}, {">","RO"}, {"<=","RO"}, {">=","RO"}, {"!=","RO"}, {"==","RO"},
{"++","inc_dec"},{"--","inc_dec"},{"+=","CO"},{"-=","CO"},{"*=","CO"},{"/=","CO"},{"=","="}};
string[,] punctuators = new string[,] { { "\"", "\"" }, { "'", "'" }, { "(", "(" }, { ")", ")" }, { "{", "{" }, { "}", "}" }, { "[", "[" }, { "]", "]" }, { ",", "," }, { ".", "." }, { ":", ":" }, { ";", ";" }, { "?", "?" }, { "::", "inherit" }, { "\\r", "\\r" } };
if (Char.IsLetter(value[0]))
{
if (boolrgx.IsMatch(value))
return "bool-const";
else if (idrgx.IsMatch(value))
{
for (int i = 0; i < keywords.Length / 2; i++)
{
if (value == keywords[i, 0])
return keywords[i, 1];
}
return "ID";
}
}
else if (charrgx.IsMatch(value))
return "char-const";
else if (strrgx.IsMatch(value))
return "string-const";
else if (digrgx.IsMatch(value))
return "int-const";
else if (floatrgx.IsMatch(value))
return "float-const";
for (int i = 0; i < operators.Length / 2; i++)
{
if (value == operators[i, 0])
return operators[i, 1];
}
for (int i = 0; i < punctuators.Length / 2; i++)
{
if (value == punctuators[i, 0])
return punctuators[i, 1];
}
return "ERROR";
}
}
}