-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathASTCreator.cpp
More file actions
278 lines (231 loc) · 9.08 KB
/
Copy pathASTCreator.cpp
File metadata and controls
278 lines (231 loc) · 9.08 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
/***************************************************************************
ASTCreator.cpp
This class works as a framework for the
creation of an abstract syntax tree specific
for a grammar
-------------------
begin : Sun Jun 2 2002
copyright : (C) 2002 by Manuel Astudillo
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/
#include "ASTCreator.h"
// Include the header with YOUR AST node classes
ASTNode *ASTCreator::createTree (const Symbol &reductionTree) {
// If the tree is an empty terminal node return null
return getASTNode (&reductionTree, NULL);
}
ASTNode *ASTCreator::getASTNode (const Symbol *reduction, ASTNode *parent) {
/*!
This method has to be customized for YOUR Abstract Grammar.
Base your Abstract Grammar in the Context Free Grammar.
Check for nodes that are equivalent in both grammars and convert
the nodes that are not equivalent.
*/
//vector <ASTNode*> *children= NULL;
wstring sym = reduction->symbol;
deque <Symbol*> rdcChildren;
if (reduction->type == NON_TERMINAL) {
rdcChildren = ((NonTerminal*) reduction)->children;
}
/*
Next is a colection of if statements that checks for the
equivalency between nodes in the reduction tree and in the
desired Abstract Syntax Tree.
Note as we always pass the latest node as the parent for the children.
Having the parent is often usefull when doing the type analysis or
interpreting the AST.
*/
/*
// (Example of a simple rule) Check for Start node
///////////////////////////////////////////////////
// Original Rule:
<Start> ::= begin <ConstList> defs <DefsList> prog <StatementList> end
// Translated to:
<Start> ::= <ConstList> <DefsList> <StatementList>
if ( sym == L"Start" ) {
Start *start = new Start ();
start->init (reduction, parent);
start->addChild (getASTNode(rdcChildren[1], start));
start->addChild (getASTNode(rdcChildren[3], start));
start->addChild (getASTNode(rdcChildren[5], start));
return start;
}
// (Example of a List)
////////////////////////
// Original Rule:
<StatementList> ::= <Statement> ';' <StatementList>
|
// Translated to:
<StatementList> ::= <Statement>*
if (sym == L"StatementList") {
StatementList *statementList = new StatementList ();
statementList->init (reduction, parent);
if (rdcChildren.size() == 3) {
statementList->addChild (getASTNode (rdcChildren[0], stmtList));
children = stmtList->getChildren();
getASTNodeList (children, rdcChildren[2]->reduction, 1, 3, stmtList);
}
return stmtList;
}
*/
/*
If the symbol constants are included it is possible to do it in the possible way:
// <If Statement> ::= if <Expression> then <StatementList> end
if (sym == RULE_IF_THEN_END_STATEMENT) {
IfStatement *ifStatement = new IfStatement ();
ifStatement->init (reduction, parent);
ifStmt->addChild (getASTNode(rdcChildren[1], ifStmt));
ifStmt->addChild (getASTNode(rdcChildren[3], ifStmt));
return ifStmt;
}
// <If Statement> ::= if <Expression> then <StatementList> else <StatementList> end
if ( sym == RULE_IF_THEN_END_ELSE_STATEMENT) {
IfStatement *ifStatement = new IfStatement ();
ifStatement->init (reduction, parent);
ifStmt->addChild (getASTNode(rdcChildren[1], ifStmt));
ifStmt->addChild (getASTNode(rdcChildren[3], ifStmt));
ifStmt->addChild (getASTNode(rdcChildren[5], ifStmt));
return ifStmt;
}
*/
return searchEquivNode( rdcChildren, parent);
}
/*!
If the node has not an equivalent in the Abstract Tree we just search
for more equivalent nodes in their children. If none found return NULL.
*/
ASTNode *ASTCreator::searchEquivNode (const deque <Symbol*> &rdcChildren,
ASTNode *parent) {
ASTNode *result = NULL;
for (unsigned short i=0; i < rdcChildren.size(); i ++) {
if (rdcChildren[i]->type == NON_TERMINAL) {
result = getASTNode(rdcChildren[i], parent);
if (result != NULL) {
break;
}
}
}
return result;
}
/*!
This method is usefull to create nodes in the tree for rules that
have the following structure:
NonTerminal1 ::= NonTerminalA NonTerminalB ... NonTerminal1
(Note that left NonTerminal and the last non-terminal in the rule are the same)
/param children vector where the children will be added
/param reduction reduction tree where the list resides
/param nbrInsert specifies the number of children to insert in the vector.
Begining from the left of the rule.
/param nbrElements specifies the number of element in the "iterative" production.
/param parent Parent node, where the list will be hanging.
*/
void ASTCreator::getASTNodeList (vector <ASTNode*> *children,
NonTerminal *reduction, int nbrInsert, int nbrElements, ASTNode *parent) {
integer i;
ASTNode *ast;
deque <Symbol*> rdcChildren = reduction->children;
int size = rdcChildren.size();
if (size > 0) {
if (size < nbrElements) {
for (i=0; i < rdcChildren.size(); i++) {
if (i < nbrInsert) {
// if we have a rule that produces a
// terminal symbol, we only get the node for this reduction
if (rdcChildren[i]->type == TERMINAL) {
ast = getASTNode (reduction, parent);
} else {
ast = getASTNode (rdcChildren[i], parent);
}
// Do not insert NULL childs
if (ast!=NULL) {
children->push_back(ast);
}
}
}
} else {
for (i=0; i < rdcChildren.size()-1; i++) {
if (i < nbrInsert) {
ast = getASTNode (rdcChildren[i], parent);
if (ast!=NULL) {
children->push_back(ast);
}
}
}
if (rdcChildren[i]->type == NON_TERMINAL) {
getASTNodeList (children, (NonTerminal*) rdcChildren[i],
nbrInsert, nbrElements, parent);
}
}
}
}
/*!
This method is usefull to create nodes in the tree for rules that
have the following structure:
NonTerminal1 ::= NonTerminalA NonTerminalB ... NonTerminal1
(Note that left NonTerminal and the last non-terminal in the rule are the same)
/param children vector where the children will be added
/param iterNode name of the Iterative node
/param reduction reduction tree where the list resides
/param nbrInsert specifies the number of children to insert in the vector.
Begining from the left of the rule.
/param nbrElements specifies the number of element in the "iterative" production.
/param parent Parent node, where the list will be hanging.
*/
void ASTCreator::getASTNodeList (vector <ASTNode*> *children, wstring iterNode,
NonTerminal *reduction, int nbrInsert, int nbrElements, ASTNode *parent) {
integer i;
ASTNode *ast;
wstring sym = reduction->symbol;
deque <Symbol*> rdcChildren = reduction->children;
if (sym == iterNode) {
int size = rdcChildren.size();
if (size > 0) {
if (size < nbrElements) {
for (i=0; i < rdcChildren.size(); i++) {
if (i < nbrInsert) {
// if we have a rule that produces a
// terminal symbol, we only get the node for this reduction
if (rdcChildren[i]->type == TERMINAL) {
ast = getASTNode (reduction,parent);
} else {
ast = getASTNode (rdcChildren[i],parent);
}
// Do not insert NULL childs
if (ast!=NULL) {
children->push_back(ast);
}
}
}
} else {
for (i=0; i < rdcChildren.size()-1; i++) {
if (i < nbrInsert) {
// Do not insert NULL childs
if (rdcChildren[i]->type == NON_TERMINAL) {
ast = getASTNode (rdcChildren[i], parent);
} else {
ast = NULL;
}
if (ast!=NULL) {
children->push_back(ast);
}
}
}
if (rdcChildren[i]->type == NON_TERMINAL) {
getASTNodeList (children, iterNode, (NonTerminal*) rdcChildren[i],
nbrInsert, nbrElements, parent);
}
}
}
} else {
// If this is not the iterNode then we just push this node as a child.
ast = getASTNode (reduction, parent);
children->push_back(ast);
}
}