-
Notifications
You must be signed in to change notification settings - Fork 0
/
firsts.h
executable file
·166 lines (142 loc) · 4.43 KB
/
firsts.h
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
//
// Created by Marty on 11/15/2017.
//
#ifndef LEXICAL_ANALYZER_C_FIRSTS_H
#define LEXICAL_ANALYZER_C_FIRSTS_H
#endif //LEXICAL_ANALYZER_C_FIRSTS_H
_Bool inFirst(char* firsts[], Token token);
char* fWhile[] = {"1", "while"};
char* fLofVar2[] = {"1", "IDENT"};
char* fAssign[] = {"1", "IDENT"};
char* fPrim[] = {"4", "INT", "CONSTCH", "IDENT", "("};
char* fUop[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fMult[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fAdd[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fComp[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fBop[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fBand[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fBor[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fLogand[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fExp[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fIf[] = {"1", "if"};
char* fLofExp[] = {"7", "!", "-", "~", "INT", "CONSTCH", "IDENT", "("};
char* fCout[] = {"1", "cout"};
char* fLofVar1[] = {"1", "IDENT"};
char* fCin[] = {"1", "cin"};
char* fStatement[] = {"7", "cin", "cout", "if", "IDENT", "while", "{", "return"};
char* fType[] = {"2", "int", "char"};
char* fVariable[] = {"1", "IDENT"};
char* fDec[] = {"2", "int", "char"};
char* fLofStatements[] = {"8", "cin", "cout", "if", "IDENT", "while", "{", "return", ""};
char* fProgram[] = {"10", "int", "char", "cin", "cout", "if", "IDENT", "while", "{", "return", ""};
_Bool inFirst(char* firsts[], Token token) {
int arrSize = atoi(firsts[0]) + 1;
for(int i = 1; i < arrSize; i++) {
if((strcmp(firsts[i], checkType(token.type)) == 0) || (strcmp(firsts[i], token.lexeme) == 0)) {
return 1;
}
}
return 0;
}
struct Node {
//1 is inner, 2 is leaf
int type;
union {
Token* token;
char* rule;
} data;
struct Node* first_child; //Points to the first node on the next level down
struct Node* next_sib; //Points to the next node on the same level
};
typedef struct Node Node;
Node* createTkNode(Token* tk) {
Node* node = malloc(sizeof(Node));
char* lexCopy;
lexCopy = malloc(sizeof(char) * strlen(tk->lexeme));
strcpy(lexCopy, tk->lexeme);
Token* newTk = createToken(tk->type, lexCopy, 0);
node->type = 2;
node->data.token = newTk;
node->first_child = NULL;
node->next_sib = NULL;
return node;
}
Node* createSNode(char* rule) {
Node* node = malloc(sizeof(Node));
node->type = 1;
node->data.rule = rule;
node->first_child = NULL;
node->next_sib = NULL;
return node;
}
void setChild(Node* node, Node* child) {
if(node->first_child == NULL) {
node->first_child = child;
}
else {
Node* nextChild = node->first_child;
while(nextChild->next_sib != NULL) {
nextChild = nextChild->next_sib;
}
nextChild->next_sib = child;
}
}
char* nodeToString(Node* node) {
char* string = (char*) malloc(sizeof(char) * 50);
if(node->type == 1) {
strcpy(string, "Rule: ");
strcat(string, node->data.rule);
strcat(string, "\n");
}
else {
strcpy(string, tokenToString(node->data.token));
}
return string;
}
void printTree(Node* root) {
printf("%s", nodeToString(root));
if(root->first_child != NULL) {
printTree(root->first_child);
}
if(root->next_sib != NULL) {
printTree(root->next_sib);
}
}
int offset = 0;
int counter = 0;
typedef struct Symbol Symbol;
struct Symbol {
TokenType type;
char* name;
int offSet;
};
Symbol* symTable[10000];
void addSymbol(TokenType tType, char* tName) {
Symbol* s = malloc(sizeof(Symbol));
char* nameCopy;
nameCopy = malloc(sizeof(char) * strlen(tName));
strcpy(nameCopy, tName);
s->name = nameCopy;
s->type = tType;
s->offSet = offset++;
symTable[counter++] = s;
}
_Bool tryAdd(TokenType tType, char* tName) {
for(int i = 0; i < counter; i++) {
Symbol* s = symTable[i];
if(s->type == tType && strcmp(s->name, tName) == 0) {
return 0;
}
}
addSymbol(tType, tName);
return 1;
}
int checkIndex(TokenType tType, char* tName) {
for(int i = 0; i < counter; i++) {
Symbol* s = symTable[i];
if(s->type == tType && strcmp(s->name, tName) == 0) {
return s->offSet;
}
}
return -1;
}