-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticAnalyzer.cpp
108 lines (85 loc) · 2.64 KB
/
SemanticAnalyzer.cpp
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
/*
* File: SemanticAnalyzer.cpp
* Author: daniel
*
* Created on October 27, 2013, 11:22 PM
*/
#include "SemanticAnalyzer.h"
#include "ASTFunctionNode.h"
#include "ScopeTable.h"
#include "Admin.h"
SemanticAnalyzer::SemanticAnalyzer() : errorCount(0), ST(NULL), admin(NULL) {
}
SemanticAnalyzer::SemanticAnalyzer(Admin * adminRef) : errorCount(0),
ST(NULL), admin(adminRef) {
ST = new ScopeTable(adminRef);
ASTFunctionNode * fn = new ASTFunctionNode;
fn->declarationType = Scanner::INT;
fn->id = 0;
ASTParamNode * p = new ASTParamNode;
fn->param = p;
ST->insertDeclaration(0, fn);
fn = new ASTFunctionNode;
fn->declarationType = Scanner::VOID;
fn->id = 1;
p = new ASTParamNode;
p->declarationType = Scanner::INT;
p->id = 2;
fn->param = p;
ST->insertDeclaration(1, fn);
ST->insertDeclaration(2, p);
fn = new ASTFunctionNode;
fn->declarationType = Scanner::BOOL;
fn->id = 3;
p = new ASTParamNode;
fn->param = p;
ST->insertDeclaration(3, fn);
fn = new ASTFunctionNode;
fn->declarationType = Scanner::VOID;
fn->id = 4;
p = new ASTParamNode;
p->declarationType = Scanner::BOOL;
p->id = 5;
fn->param = p;
ST->insertDeclaration(4, fn);
ST->insertDeclaration(5, p);
}
SemanticAnalyzer::SemanticAnalyzer(const SemanticAnalyzer& orig) {
}
SemanticAnalyzer::~SemanticAnalyzer() {
}
ScopeTable * SemanticAnalyzer::getST() { return ST; }
void SemanticAnalyzer::semAnalyze(ASTNode* topNode){
ASTNode * current = topNode;
// Loop over all global declarations for forward-declarations
try {
//cout << "Starting pre-analysis" << endl;
ASTNode::init = true;
while(current != NULL) {
current->semAnalyze();
// Meet semantic condition that int main(void) is last
if(current->next == NULL && !isIntMainVoidLast((ASTDeclarationNode *)current)) {
// Semantic error - int main (void) not found as last declaration
admin->semanticError("Last declaration must be int main(void)", current->lineNumber);
}
current = current->next;
}
// Analyze the rest of the source code
//cout << "Starting full analysis" << endl;
ASTNode::init = false;
topNode->semAnalyze();
}
catch(char const * ex) {
cout << "Compiler exception thrown: " << ex;
}
}
void SemanticAnalyzer::semanticError(string desc, int lineNumber) {
admin->semanticError(desc, lineNumber);
}
void SemanticAnalyzer::incError() { errorCount++; }
int SemanticAnalyzer::getErrorCount() { return errorCount; }
bool SemanticAnalyzer::isIntMainVoidLast(ASTDeclarationNode * lastDec) {
return lastDec->declarationType == Scanner::INT &&
lastDec->lookup->getIdentifierName(lastDec->id) == "main" &&
((ASTFunctionNode *)lastDec)->getParamCount() == 0;
}