1
1
#include " tree.h"
2
2
3
3
// Prints the abstract syntax tree to the provided output stream
4
- void Tree::printTree (Token* head){
4
+ void Tree::printTree (Token* head, Token* prevToken){
5
+ bool ignore = true ;
5
6
if (head == nullptr ){
6
7
return ;
7
8
}
8
9
if (head->getValue () == " {" ) {
10
+ ignore = false ;
9
11
std::cout << " BEGIN BLOCK" ;
10
12
} else if (head->getValue () == " }" ) {
13
+ ignore = false ;
11
14
std::cout << " END BLOCK" ;
12
15
} else if (head->getValue () == " procedure" || head->getValue () == " function" ) {
16
+ std::cout << " DECLARATION\n |\n |\n |\n |\n v\n " ;
17
+ while (head->getSibling () != nullptr ) {
18
+ head = head->getSibling ();
19
+ }
20
+ } else if (head->getValue () == " if" ){
21
+ ignore = false ;
22
+ std::cout << " IF ----> " ;
23
+ // This needs to break out to handleAssignment();
24
+ head = head->getSibling ();
25
+ head = handleAssignment (head);
26
+ prevToken = nullptr ;
27
+
28
+ }else if (contains (varTypes, prevToken->getValue ())) {
29
+ ignore = false ;
13
30
std::cout << " DECLARATION" ;
14
- } else if (contains (head->getValue ())) {
15
- std::cout << " DECLARATION" ;
31
+ if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " ," ) {
32
+ while (head->getSibling () != nullptr ) {
33
+ head = head->getSibling ();
34
+ if (head->getType () == " IDENTIFIER" ) {
35
+ std::cout << " \n |\n |\n |\n |\n v\n DECLARATION" ;
36
+ }
37
+ }
38
+ }
16
39
} else if (head->getType () == " IDENTIFIER" ) {
17
40
if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " =" ) {
18
- std::cout << " ASSIGNMENT" << " ----> " << head->getValue ();
41
+ ignore = false ;
42
+ std::cout << " ASSIGNMENT" << " ----> " ;
19
43
// This needs to break out to handleAssignment();
20
- // head = handleAssignment();
21
- }
22
- }
44
+ head = handleAssignment (head);
45
+ prevToken = nullptr ;
46
+ }
47
+ }
23
48
if (head->getSibling () != nullptr ) {
24
- head = head->getSibling ();
25
- std::cout << " ----> " ;
49
+ if (!ignore){
50
+ std::cout << " ----> " ;
51
+ std::cout << head->getValue ();
52
+ }
53
+ return printTree (head->getSibling (), head);
26
54
} else if (head->getChild () != nullptr ) {
27
- head = head->getChild ();
28
- std::cout << " \n |\n |\n |\n |\n v " ;
29
- }
30
- return printTree (head);
55
+ if (!ignore) {
56
+ std::cout << " \n |\n |\n |\n |\n v\n " ;
57
+ }
58
+ return printTree (head->getChild (), head);
59
+ }
60
+ return ;
31
61
}
32
62
33
- bool contains (std::string type){
34
- for (const auto & reserved : varTypes ) {
35
- if (type == reserved ) {
63
+ bool Tree:: contains (const std::vector<std::string> reserved, std::string type){
64
+ for (const auto & word : reserved ) {
65
+ if (type == word ) {
36
66
return true ;
37
67
}
38
68
}
39
69
return false ;
70
+ }
71
+
72
+ Token* Tree::handleAssignment (Token* head) {
73
+ std::string equationAsString;
74
+ equationAsString += head->getValue ();
75
+ head = head->getSibling ();
76
+
77
+ while (contains (equationOperators, head->getValue ()) || head->getType () == " IDENTIFIER" || head->getType () == " INTEGER" || head->getType () == " CHARACTER" ) {
78
+ equationAsString += head->getValue ();
79
+ if (head ->getSibling () != nullptr ) {
80
+ head = head->getSibling ();
81
+ } else {
82
+ break ;
83
+ }
84
+ }
85
+ // head = head->getSibling(); // Remove ?
86
+
87
+ for (char ch : infixToPostfix (equationAsString)) {
88
+ // std::cout << ch << " ----> ";
89
+ }
90
+ std::cout << " Postfix Expression" ;
91
+ return head;
92
+ }
93
+
94
+
95
+ // Function to get precedence of operators
96
+ int Tree::getPrecedence (char op) {
97
+ if (op == ' +' || op == ' -' ) return 1 ;
98
+ if (op == ' *' || op == ' /' ) return 2 ;
99
+ return 0 ;
100
+ }
101
+
102
+ // Function to check if a character is an operator
103
+ bool Tree::isOperator (char c) {
104
+ return c == ' +' || c == ' -' || c == ' *' || c == ' /' ;
105
+ }
106
+
107
+ // Function to convert infix expression to postfix
108
+ std::string Tree::infixToPostfix (const std::string& infix) {
109
+ std::stack<char > operators;
110
+ std::string postfix;
111
+
112
+ for (char c : infix) {
113
+ // If the character is an operand, add it to postfix output
114
+ if (std::isdigit (c) || std::isalpha (c)) {
115
+ postfix += c;
116
+ }
117
+ // If the character is '(', push it onto the stack
118
+ else if (c == ' (' ) {
119
+ operators.push (c);
120
+ }
121
+ // If the character is ')', pop and add operators until '(' is found
122
+ else if (c == ' )' ) {
123
+ while (!operators.empty () && operators.top () != ' (' ) {
124
+ postfix += operators.top ();
125
+ operators.pop ();
126
+ }
127
+ operators.pop (); // Pop '(' from the stack
128
+ }
129
+ // If the character is an operator
130
+ else if (isOperator (c)) {
131
+ while (!operators.empty () && getPrecedence (operators.top ()) >= getPrecedence (c)) {
132
+ postfix += operators.top ();
133
+ operators.pop ();
134
+ }
135
+ operators.push (c);
136
+ }
137
+ }
138
+
139
+ // Pop all remaining operators in the stack
140
+ while (!operators.empty ()) {
141
+ postfix += operators.top ();
142
+ operators.pop ();
143
+ }
144
+
145
+ return postfix;
40
146
}
0 commit comments