This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
minic_ast.c
122 lines (104 loc) · 3.27 KB
/
minic_ast.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
#include <stdio.h>
#include <stdlib.h>
#include "minic_ast.h"
void printTree(Node *ptr, int indent, FILE* astFile);
void printNode(Node *pt, int indent, FILE* astFile);
char *nodeName[] = {
"ACTUAL_PARAM", "ADD", "ADD_ASSIGN", "ARRAY_VAR", "ASSIGN_OP",
"CALL", "COMPOUND_ST", "CONST_NODE", "DCL", "DCL_ITEM",
"DCL_LIST", "DCL_SPEC", "DIV", "DIV_ASSIGN", "EQ",
"ERROR_NODE", "EXP_ST", "FORMAL_PARA", "FUNC_DEF", "FUNC_HEAD",
"GE", "GT", "IDENT", "IF_ELSE_ST", "IF_ST",
"INDEX", "INT_NODE", "LE", "LOGICAL_AND", "LOGICAL_NOT",
"LOGICAL_OR", "LT", "MOD", "MOD_ASSIGN", "MUL",
"MUL_ASSIGN", "NE", "NUMBER", "PARAM_DCL", "POST_DEC",
"POST_INC", "PRE_DEC", "PRE_INC", "PROGRAM", "RETURN_ST",
"SIMPLE_VAR", "STAT_LIST", "SUB", "SUB_ASSIGN", "UNARY_MINUS",
"VOID_NODE", "WHILE_ST"
};
int ruleName[] = {
0, PROGRAM, 0, 0, 0,
0, FUNC_DEF, FUNC_HEAD, DCL_SPEC, 0,
0, 0, 0, CONST_NODE, INT_NODE,
VOID_NODE, 0, FORMAL_PARA, 0, 0,
0, 0, PARAM_DCL, COMPOUND_ST, DCL_LIST,
DCL_LIST, 0, 0, DCL, 0,
0, DCL_ITEM, DCL_ITEM, SIMPLE_VAR, ARRAY_VAR,
0, 0, STAT_LIST, 0, 0,
0, 0, 0, 0, 0,
0, EXP_ST, 0, 0, IF_ST,
IF_ELSE_ST, WHILE_ST, RETURN_ST, 0, 0,
ASSIGN_OP, ADD_ASSIGN, SUB_ASSIGN, MUL_ASSIGN, DIV_ASSIGN,
MOD_ASSIGN, 0, LOGICAL_OR, 0, LOGICAL_AND,
0, EQ, NE, 0, GT,
LT, GE, LE, 0, ADD,
SUB, 0, MUL, DIV, MOD,
0, UNARY_MINUS, LOGICAL_NOT, PRE_INC, PRE_DEC,
0, INDEX, CALL, POST_INC, POST_DEC,
0, 0, ACTUAL_PARAM, 0, 0,
0, 0, 0
};
Node* buildNode(int tokenNumber, char* tokenValue)
{
Node *ptr;
ptr = (Node *) malloc(sizeof(Node));
if (!ptr) {
printf("ERROR : memory allocation error in buildNode()\n");
exit(1);
}
ptr->token.tokenNumber = tokenNumber;
ptr->token.tokenValue = tokenValue;
ptr->noderep = TERMINAL;
ptr->son = ptr->next = NULL;
return ptr;
}
Node* buildTree(int tokenNumber, Node* son)
{
Node *ptr;
ptr = (Node*) malloc(sizeof(Node));
if (!ptr) {
printf("ERROR : memory allocation error in buildTree()\n");
exit(1);
}
ptr->token.tokenNumber = tokenNumber;
ptr->token.tokenValue = NULL;
ptr->noderep = NONTERM;
ptr->son = son;
ptr->next = NULL;
return ptr;
}
void appendNext(Node* node, Node* next)
{
Node *ptr = node;
int i = 1;
while (ptr->next != NULL) { // 계속 뒤쪽으로 연결해서 붙도록...
ptr = ptr->next;
i++;
}
ptr->next = next;
}
void printNode(Node *pt, int indent, FILE* astFile)
{
int i;
for (i = 1; i <= indent; i++) {
fprintf(astFile, " ");
}
if (pt->noderep == TERMINAL) {
fprintf(astFile, "Terminal: %s\n", pt->token.tokenValue);
} else {
int i;
i = (int) (pt->token.tokenNumber);
fprintf(astFile, "Nonterminal: %s\n", nodeName[i]);
}
}
void printTree(Node *ptr, int indent, FILE* astFile)
{
Node *p = ptr;
while (p != NULL) {
printNode(p, indent, astFile);
if (p->noderep == NONTERM) {
printTree(p->son, indent + 1, astFile);
}
p = p->next;
}
}