-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalyzer.cpp
More file actions
268 lines (223 loc) · 9.18 KB
/
Copy pathSemanticAnalyzer.cpp
File metadata and controls
268 lines (223 loc) · 9.18 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
// ============================================================
//SYMENTIC ANALYZER
// ============================================================
// Purpose: SEMANTIC ANALYSIS implementation.
// Walks the AST and performs all meaning-checks.
// Builds and maintains the Symbol Table.
// ============================================================
#include "SemanticAnalyzer.h"
#include <iostream>
#include <iomanip> // std::setw for pretty table
SemanticAnalyzer::SemanticAnalyzer() {}
// ANALYZE() – entry point for semantic analysis, takes the AST and returns the symbol table
std::unordered_map<std::string, Symbol>
SemanticAnalyzer::analyze(const std::vector<ASTNodePtr>& stmts) {
symbolTable_.clear();
scopes_.clear();
analysisLog_.clear();
enterScope();
log("=== SEMANTIC ANALYSIS STARTED ===");
for (const auto& stmt : stmts) {
analyzeNode(stmt.get()); // Visit each top-level statement
}
symbolTable_ = scopes_.front();
log("=== SEMANTIC ANALYSIS COMPLETE - No errors found ===");
return symbolTable_; // Return the fully populated symbol table
}
// ANALYZE_NODE() – dispatches to the appropriate analysis function based on node type
void SemanticAnalyzer::analyzeNode(const ASTNode* node) {
if (!node) return; // Null check (safety)
// Dynamic cast to each possible concrete node type
if (auto* n = dynamic_cast<const VarDeclNode*>(node)) {
analyzeVarDecl(n);
} else if (auto* n = dynamic_cast<const AssignNode*>(node)) {
analyzeAssign(n);
} else if (auto* n = dynamic_cast<const IfNode*>(node)) {
analyzeIf(n);
} else if (auto* n = dynamic_cast<const WhileNode*>(node)) {
analyzeWhile(n);
} else if (auto* n = dynamic_cast<const PrintNode*>(node)) {
analyzePrint(n);
} else if (auto* n = dynamic_cast<const ReturnNode*>(node)) {
analyzeReturn(n);
} else if (auto* n = dynamic_cast<const BlockNode*>(node)) {
analyzeBlock(n);
}
// Leaf expression nodes (Number, String, Identifier) need no
// top-level analysis – they are checked via inferType() below
}
// ANALYZE_VAR_DECL() – check variable declaration rules and update symbol table
void SemanticAnalyzer::analyzeVarDecl(const VarDeclNode* node) {
log("CHECK: Variable declaration '" + node->name + "' of type '" + node->type + "'");
// ── Rule 1: No duplicate declarations in same scope ──
if (scopes_.back().count(node->name)) {
error("Variable '" + node->name + "' already declared in this scope");
}
// ── Rule 2: Type-check the initialiser ───────────────
std::string initType = "none";
if (node->init) {
initType = inferType(node->init.get());
log(" Initialiser inferred type: " + initType);
// float can hold int values (widening) but not vice-versa
if (node->type == "int" && initType == "float") {
error("Cannot assign float to int variable '" + node->name + "'");
}
}
// ── Add to symbol table ───────────────────────────────
scopes_.back()[node->name] = Symbol{ node->type, node->init != nullptr, 0 };
log(" Added '" + node->name + "' to symbol table");
}
// ANALYZE_ASSIGN() – check assignment rules and update symbol table
void SemanticAnalyzer::analyzeAssign(const AssignNode* node) {
log("CHECK: Assignment to '" + node->name + "'");
// ── Rule: Variable must be declared before assignment ─
Symbol* symbol = findSymbol(node->name);
if (!symbol) {
error("Variable '" + node->name + "' used before declaration");
}
std::string exprType = inferType(node->expr.get());
std::string varType = symbol->type;
log(" Assigning type '" + exprType + "' to '" + varType + "' variable");
if (varType == "int" && exprType == "float") {
error("Type mismatch: cannot assign float expression to int '" + node->name + "'");
}
symbol->initialised = true; // Mark as assigned
}
// ANALYZE_IF() – check if-statement rules
void SemanticAnalyzer::analyzeIf(const IfNode* node) {
log("CHECK: if-statement condition");
inferType(node->condition.get()); // Verify condition is well-typed
log("CHECK: if-statement then-branch");
enterScope();
for (const auto& s : node->thenBranch)
analyzeNode(s.get());
exitScope();
if (!node->elseBranch.empty()) {
log("CHECK: if-statement else-branch");
enterScope();
for (const auto& s : node->elseBranch)
analyzeNode(s.get());
exitScope();
}
}
// ANALYZE_WHILE() – check while-loop rules
void SemanticAnalyzer::analyzeWhile(const WhileNode* node) {
log("CHECK: while-loop condition");
inferType(node->condition.get());
log("CHECK: while-loop body");
enterScope();
for (const auto& s : node->body)
analyzeNode(s.get());
exitScope();
}
// ANALYZE_PRINT() – check print statement rules
void SemanticAnalyzer::analyzePrint(const PrintNode* node) {
log("CHECK: print statement");
inferType(node->expr.get()); // Ensure the expression is valid
}
// ANALYZE_RETURN() – check return statement rules
void SemanticAnalyzer::analyzeReturn(const ReturnNode* node) {
log("CHECK: return statement");
if (node->expr) inferType(node->expr.get());
}
// ANALYZE_BLOCK() – check block statement rules
void SemanticAnalyzer::analyzeBlock(const BlockNode* node) {
enterScope();
for (const auto& s : node->statements)
analyzeNode(s.get());
exitScope();
}
// INFER_TYPE() – recursively determine the type of an expression node
std::string SemanticAnalyzer::inferType(const ASTNode* node) {
if (!node) return "void";
if (dynamic_cast<const NumberNode*>(node)) {
// Numeric literal: check if it has a decimal point
auto* n = dynamic_cast<const NumberNode*>(node);
// If value has a fractional part it is float, otherwise int
return (n->value != (int)n->value) ? "float" : "int";
}
if (dynamic_cast<const StringNode*>(node)) {
return "string";
}
if (auto* n = dynamic_cast<const IdentifierNode*>(node)) {
// Look up in symbol table
const Symbol* symbol = findSymbol(n->name);
if (!symbol)
error("Undeclared variable '" + n->name + "'");
return symbol->type;
}
if (auto* n = dynamic_cast<const BinaryOpNode*>(node)) {
std::string lt = inferType(n->left.get());
std::string rt = inferType(n->right.get());
// Relational operators always produce int (0 or 1)
if (n->op == "==" || n->op == "!=" ||
n->op == "<" || n->op == ">" ||
n->op == "<=" || n->op == ">=") {
return "int";
}
// Arithmetic: if either operand is float, result is float
if (lt == "float" || rt == "float") return "float";
return "int";
}
return "unknown";
}
// INFER_TYPE() – recursively determine the type of an expression node
void SemanticAnalyzer::log(const std::string& msg) {
analysisLog_.push_back(msg);
std::cout << " [SEMANTIC] " << msg << "\n";
}
// ERROR() – throw a semantic error
void SemanticAnalyzer::error(const std::string& msg) {
throw std::runtime_error("Semantic error: " + msg);
}
void SemanticAnalyzer::enterScope() {
scopes_.push_back({});
}
void SemanticAnalyzer::exitScope() {
if (!scopes_.empty()) {
scopes_.pop_back();
}
}
Symbol* SemanticAnalyzer::findSymbol(const std::string& name) {
for (auto it = scopes_.rbegin(); it != scopes_.rend(); ++it) {
auto found = it->find(name);
if (found != it->end()) {
return &found->second;
}
}
return nullptr;
}
const Symbol* SemanticAnalyzer::findSymbol(const std::string& name) const {
for (auto it = scopes_.rbegin(); it != scopes_.rend(); ++it) {
auto found = it->find(name);
if (found != it->end()) {
return &found->second;
}
}
return nullptr;
}
// PRINT_SYMBOL_TABLE() – utility to display the current symbol table (for debugging)
void SemanticAnalyzer::printSymbolTable() const {
std::cout << "\n +---------------+-------+-------------+\n";
std::cout << " | Name | Type | Initialised |\n";
std::cout << " +---------------+-------+-------------+\n";
for (const auto& entry : symbolTable_) {
const std::string& name = entry.first;
const Symbol& sym = entry.second;
std::cout << " | " << std::setw(13) << std::left << name
<< " | " << std::setw(5) << std::left << sym.type
<< " | " << std::setw(11) << std::left << (sym.initialised ? "YES" : "NO")
<< " |\n";
}
std::cout << " +---------------+-------+-------------+\n\n";
}
//example of semantic analysis for the statement:
// int x = 3+5
// AST (parser) =
// =
// x +
// 3 5
// semmentic analyze (ASY) + symbol table
// <assignment,=>
// <identifier,x> <arithmaticoperator,+>
// <number,3> <number,5>