-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
198 lines (185 loc) · 4.4 KB
/
print.c
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
/* Group 3
Ashrya Agrawal 2018A7PS0210P
Kalit Naresh Inani 2018A7PS0207P
Prajwal Gupta 2018A7PS0231P */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "naryTree.h"
#include "token.h"
#include "typeExpression.h"
#include "parsetree.h"
void printTypeExpression(int type, TypeExpression typeExpr)
{
printf("< type = ");
switch (type)
{
case PRIMITIVE:
{
int primType = typeExpr.primitiveType;
switch (primType)
{
case PRIM_INTEGER:
{
printf("integer ");
break;
}
case PRIM_REAL:
{
printf("real ");
break;
}
case PRIM_BOOLEAN:
{
printf("boolean ");
break;
}
}
break;
}
case RECTANGULAR_ARRAY:
{
RectangularArray raExp = typeExpr.rectType;
printf("rectangularArray, dimensions=%d,", raExp.currDimensions);
for (int i = 0; i < raExp.currDimensions; i++)
{
printf("range_R%d=(", (i + 1));
if (raExp.dimenArray[i][0] != -1)
{
printf("%d,", raExp.dimenArray[i][0]);
}
else
{
printf("%s,", raExp.dynamicDimenArray[i][0]);
}
if (raExp.dimenArray[i][1] != -1)
{
printf("%d),", raExp.dimenArray[i][1]);
}
else
{
printf("%s),", raExp.dynamicDimenArray[i][1]);
}
}
printf("basicElementType = integer");
break;
}
case JAGGED_ARRAY:
{
JaggedArray jaggArr = typeExpr.jaggedType;
if (jaggArr.isValid == 0)
{
printf("***");
break;
}
printf("jaggedArray, dimensions=%d, ", jaggArr.is2D ? 2 : 3);
printf("range_R1 = (%d,%d), range_R2 = (", jaggArr.r1Low, jaggArr.r1High);
int r1Len = jaggArr.r1High - jaggArr.r1Low + 1;
for (int i = 0; i < r1Len; i++)
{
if (jaggArr.is2D)
{
printf(" %d,", jaggArr.type.twod_array.size[i]);
}
else
{
printf("%d[", jaggArr.type.threed_array.sizeR2[i]);
for (int j = 0; j < jaggArr.type.threed_array.sizeR2[i]; j++)
{
printf("%d,", jaggArr.type.threed_array.size[i][j]);
}
printf("], ");
}
}
printf("), basicElementType = integer");
break;
}
}
printf(">");
}
void printTerminal(TreeNode *node)
{
//printing leaf node in pretty columns
if (node->terminal != EPS && node->tok != NULL)
printf("%-20s\tTERMINAL\t\t%-10s\t%d\t\t--\t\t%d\t\t--\n", TOKENS[node->terminal], node->tok->lexeme, node->tok->lineNo, node->depth);
}
void printNonTerminal(TreeNode *node)
{
//printing non-leaf node
printf("%-20s\tNON TERMINAL\t\t--\t\t--\t\t%d\t\t%d\t\t", NONTERMINALS[node->nonterminal], node->ruleNo, node->depth);
if (node->typeInformationStored == 1)
{
printTypeExpression(node->type, node->expression);
}
else
{
printf("--");
}
printf("\n");
}
void printTraverse(TreeNode *root)
{
// preorder traversal
if (root == NULL)
{
return;
}
if (root->isLeaf)
{
// is a leaf node
printTerminal(root);
}
else
{
printNonTerminal(root);
}
TreeNode *iter = root->child;
while (iter != NULL)
{
printTraverse(iter);
iter = iter->next;
}
}
void printParseTree(TreeNode *root)
{
printf("SymbolName\t\tTerminal/NonTerminal\tNameOfLexeme\tLineNumber\tGrammarRule\tDepthOfNode\tTypeExpression\n\n");
printTraverse(root);
printf("\n----------------------------------------------------PRINTING PARSE TREE COMPLETED---------------------------------------------\n\n");
return;
}
char *printArrayType(int n)
{
switch (n)
{
case 0:
{
return "STATIC";
}
case 1:
{
return "DYNAMIC";
}
default:
{
return "NOT APPLICABLE";
}
}
}
char *printType(int type)
{
switch (type)
{
case PRIMITIVE:
{
return "Primitive";
}
case RECTANGULAR_ARRAY:
{
return "Rectangular Array";
}
case JAGGED_ARRAY:
{
return "Jagged Array";
}
}
}