-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.c
221 lines (207 loc) · 4.91 KB
/
grammar.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Group Number: 56
Chirag Singhal 2018A7PS0219P
Harshan Baskar 2018A7PS0166P
Nitya Mangal 2018A7PS0216P
*/
#include "grammar.h"
#define NUM_RULES 68
#define MAX_SIZE 50
char* known_symbols[] = {
"program",
"(",
")",
"{",
"}",
"declare",
":",
"list",
"of",
"variables",
"integer",
"boolean",
"real",
";",
"array",
"jagged",
"[",
"..",
"]",
"R1",
"size",
"values",
"=",
"|||",
"&&&",
"+",
"-",
"*",
"/"
};
char* term_grammar[] = {
"PROGRAM",
"OP",
"CP",
"OCB",
"CCB",
"DECLARE",
"COLON",
"LIST",
"OF",
"VARIABLES",
"INTEGER",
"BOOLEAN",
"REAL",
"SEMI_COLON",
"ARRAY",
"JAGGED",
"OSB",
"DOUBLE_DOT",
"CSB",
"R1",
"SIZE",
"VALUES",
"ASSIGN_OP",
"OR_LOGIC_OP",
"AND_LOGIC_OP",
"ADD_OP",
"SUB_OP",
"MUL_OP",
"DIV_OP",
"NUM",
"VAR_ID",
"EPSILON"
};
char* nonterm_grammar[] = {
"start",
"stmts",
"declarativeStmts",
"declarativeStmtsOptional",
"assignmentStmts",
"assignmentStmtsOptional",
"declarativeStmt",
"varDeclarePhrase",
"varOrListDeclare",
"listVarId",
"listVarIdOptional",
"type",
"specificDeclarativePhrase",
"primitiveDeclarativePhrase",
"rectArrayDeclarativePhrase",
"jaggedArrayDeclarativePhrase",
"listDim",
"listDimOptional",
"idx",
"listEmptyDim",
"listEmptyDimOptional",
"jaggedArrayInitialization",
"jaggedArrayInitializationPhrase",
"jaggedArrayInitializationOptional",
"listIntSemiColon",
"listIntSemiColonOptional",
"listInt",
"listIntOptional",
"assignmentStmt",
"expression",
"element",
"elementDimOptional",
"listIdx",
"listIdxOptional",
"orLogicExpression",
"orLogicExpressionOptional",
"andLogicExpression",
"andLogicExpressionOptional",
"addExpression",
"addExpressionOptional",
"addOps",
"mulExpression",
"mulExpressionOptional",
"mulOps",
"term"
};
int getToken(char* word) {
for (int i = 0; i < sizeof(known_symbols)/sizeof(known_symbols[0]); ++i) {
if (strcmp(word, known_symbols[i]) == 0) {
return i;
}
}
// check for NUM
int len = strlen(word);
int valid_num = 1;
for(int i = 0; i < len; i++) {
if(word[i] < '0' || word[i] > '9') {
valid_num = 0;
break;
}
}
if(valid_num)
return NUM;
// check for VAR_ID
if (len > 20 || !((word[0] >= 'a' && word[0] <= 'z') || word[0] =='_' || (word[0] >= 'A' && word[0] <= 'Z')))
return -1;
for (int i = 1; i < len; ++i) {
if ((word[i] < 'a' || word[i] > 'z') && word[i] != '_' && (word[i] < 'A' || word[i] > 'Z') && (word[i] < '0' || word[i] > '9'))
return -1;
}
return VAR_ID;
}
int getTerm(char* word) {
for (int i = 0; i < sizeof(term_grammar)/sizeof(term_grammar[0]); ++i) {
if (strcmp(word, term_grammar[i]) == 0) {
return i;
}
}
// if no non-terminal matches
return -1;
}
int getNonTerm(char* word) {
for (int i = 0; i < sizeof(nonterm_grammar)/sizeof(nonterm_grammar[0]); ++i) {
if (strcmp(word, nonterm_grammar[i]) == 0) {
return i;
}
}
// if no non-terminal matches
return -1;
}
int isTerminal(const char* word) {
if ('a' <= word[0] && word[0] <= 'z')
return 0;
return 1;
}
void readGrammar(const char* filename, grammar G) {
FILE* grammar_fp;
grammar_fp = fopen(filename, "r");
if(grammar_fp == NULL) {
printf("ERROR : GRAMMAR FILE NOT FOUND\n");
return;
}
for(int i = 0; i < NUM_RULES; ++i) {
char prod_rule[150];
fgets(prod_rule, 150, grammar_fp);
int len = strlen(prod_rule);
if(prod_rule[len - 1] == '\n')
prod_rule[len - 1] = '\0';
char *token;
token = strtok(prod_rule, " ");
G[i].lhs_nonterminal = malloc(sizeof(char) * MAX_SIZE);
strcpy(G[i].lhs_nonterminal, token);
G[i].nontermid = getNonTerm(token);
struct rhs_production_rule* prev_rhs_rule = NULL;
while ((token = strtok(NULL, " \0")) != NULL) {
struct rhs_production_rule* rhs_rule = malloc(sizeof(struct rhs_production_rule));
rhs_rule -> word = malloc(sizeof(char) * MAX_SIZE);
strcpy(rhs_rule -> word, token);
rhs_rule -> terminal = isTerminal(token);
if (rhs_rule -> terminal)
rhs_rule -> tokenId = getTerm(rhs_rule -> word);
else
rhs_rule -> tokenId = getNonTerm(rhs_rule -> word);
rhs_rule -> next = NULL;
if(prev_rhs_rule == NULL)
G[i].head = rhs_rule;
else
prev_rhs_rule -> next = rhs_rule;
prev_rhs_rule = rhs_rule;
}
}
}