-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticAnalyzer.cs
208 lines (190 loc) · 7.14 KB
/
SemanticAnalyzer.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Compiler_Construction
{
class SemanticAnalyzer
{
string outputPath = @"C:\Users\Admin\Desktop\CompilerFinal\maintable.txt";
static List<MainTable> mainTable = new List<MainTable>();
public ArrayList functionTable = new ArrayList();
public List<ArrayList> classLinks = new List<ArrayList>();
Stack scopeStack = new Stack();
int scope = 0;
int link = -1;
public void print_MT()
{
File.WriteAllText(outputPath, String.Empty);
File.AppendAllText(outputPath, $"{"Name"} {"Class Type"} {"Access Modifier"} {"Category"} {"Parent"} {"Link"} \n");
}
public void print_CT()
{
File.WriteAllText(outputPath, String.Empty);
File.AppendAllText(outputPath, $"{"Name"} {"Type"} {"Access Modifier"} {"Type Modifier"} \n");
}
public bool insert_MT(string name, string ctype, string accessModifier, string category, string parent)
{
bool isParent = false;
foreach (MainTable row in mainTable)
{
if (row.name == name)
{
File.AppendAllText(outputPath, "Redeclaration of class \n");
classLinks.RemoveAt(this.link);
this.link--;
return false;
}
}
if (parent != "none")
{
foreach (MainTable row in mainTable)
{
if (parent == row.name)
{
isParent = true;
if (row.category == "final")
{
classLinks.RemoveAt(this.link);
this.link--;
File.AppendAllText(outputPath, "Final class cant be inherited \n");
//Console.WriteLine("Final class cant be inherited");
return false;
}
}
}
if (!isParent)
{
File.AppendAllText(outputPath, "Parent class is not declared \n");
//Console.WriteLine("PARENT CLASS IS NOT DECLARED^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
classLinks.RemoveAt(this.link);
this.link--;
return false;
}
}
mainTable.Add(new MainTable(name, ctype, accessModifier, category, parent, this.link));
File.AppendAllText(outputPath, $"{name} {ctype} {accessModifier} {category} {parent} {link} \n");
return true;
}
public bool insert_FT(string name, string type, string scope)
{
foreach (FunctionTable row in functionTable)
{
if (row.name == name)
{
if (row.scope == scope)
{
File.AppendAllText(outputPath, "Redeclaration of Identifier \n");
return false;
}
}
}
functionTable.Add(new FunctionTable(name, type, scope));
File.AppendAllText(outputPath, $"Function Table \n");
File.AppendAllText(outputPath, $"{"Name"} {"Type"} {"Scope"} \n");
File.AppendAllText(outputPath, $"{name} {type} {scope} \n");
return true;
}
public bool insert_CT(string name, string type, string acessModifier, string typeModifier, ArrayList a)
{
foreach (ClassTable row in a)
{
if (row.name == name)
{
if (row.type == type)
{
File.AppendAllText(outputPath, "Redeclaration of Function \n");
return false;
}
}
}
classLinks[this.link].Add(new ClassTable(name, type, acessModifier, typeModifier));
File.AppendAllText(outputPath, $"Class Table \n");
File.AppendAllText(outputPath, $"{"Name"} {"Type"} {"AccessModifier"} {"TypeModifier"} \n");
File.AppendAllText(outputPath, $"{name} {type} {acessModifier} {typeModifier} \n");
return true;
}
public void create_CT()
{
link++;
ArrayList c = new ArrayList();
classLinks.Add(c);
}
string lookup_MT(string name, out string category, out string parents)
{
foreach (MainTable main in mainTable)
{
if (main.name == name)
{
category = main.category;
parents = main.parent;
return "class";
}
}
category = "";
parents = "";
return "class not declared";
}
string lookupAttr_CT(int link, string name, out string AM, out string TM)
{
foreach (ClassTable classT in classLinks[link])
{
if (classT.name == name)
{
AM = classT.acessModifier;
TM = classT.typeModifier;
return classT.type;
}
}
AM = "";
TM = "";
return "variable not declared";
}
public void createScope()
{
scope++;
scopeStack.Push(scope);
}
public void destroyScope()
{
scopeStack.Pop();
}
}
class MainTable
{
public string name, ctype, accessModifier, category, parent;
int link;
public MainTable(string name, string ctype, string accessModifier, string category, string parent, int link)
{
this.name = name;
this.ctype = ctype;
this.accessModifier = accessModifier;
this.category = category;
this.parent = parent;
this.link = link;
}
}
class FunctionTable
{
public string name, type, scope;
public FunctionTable(string name, string type, string scope)
{
this.name = name;
this.type = type;
this.scope = scope;
}
}
class ClassTable
{
public string name, type, acessModifier, typeModifier;
public ClassTable(string name, string type, string acessModifier, string typeModifier)
{
this.name = name;
this.type = type;
this.acessModifier = acessModifier;
this.typeModifier = typeModifier;
}
}
}