-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
321 lines (299 loc) · 6.81 KB
/
parser.c
File metadata and controls
321 lines (299 loc) · 6.81 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Compiler Builder 16
Kenneth Mitchell
Georg Anemogiannis
David Israwi Yordi
Tyler Chauhan
*/
#include "header.h"
/* Symbol table */
symbol symbol_table[MAX_SYMBOL_TABLE_SIZE];
/* Current Token */
token currentTok;
int tokenNumber = 0;
/* Functions */
void program();
void block();
void statement();
void condition();
void expression();
void term();
void factor();
void getToken();
void errorMessage(int x);
/* program ::= block "." */
void program()
{
getToken(); //retrieve the token
block(); //
if(currentTok.type != periodsym)
{
errorMessage(9);
}
}
/* block ::= const-declaration var-declaration statement */
void block()
{
if (currentTok.type == constsym)
{
do {
getToken();
if (currentTok.type != identsym)
errorMessage(25);
getToken();
if (currentTok.type != eqsym)
errorMessage(25);
getToken();
if (currentTok.type != numbersym)
errorMessage(25);
getToken();
} while(currentTok.type == commasym);
if (currentTok.type != semicolonsym)
errorMessage(25);
getToken();
}
if (currentTok.type == varsym)
{
do {
getToken();
if (currentTok.type != identsym)
errorMessage(25);
getToken();
} while(currentTok.type == commasym);
if (currentTok.type != semicolonsym)
errorMessage(25);
getToken();
}
while (currentTok.type == procsym)
{
getToken();
if (currentTok.type != identsym)
errorMessage(25);
getToken();
if (currentTok.type != semicolonsym)
errorMessage(25);
getToken();
block();
if (currentTok.type != semicolonsym)
errorMessage(25);
getToken();
}
statement();
}
/* statement ::= [ ident “:=” expression
| “begin” statement { “;” statement } “end”
| “if” condition “then” statement ]
*/
void statement()
{
if (currentTok.type == identsym)
{
getToken();
if (currentTok.type != becomessym)
errorMessage(25);
getToken();
expression();
}
else if (currentTok.type == callsym)
{
getToken();
if (currentTok.type != identsym)
errorMessage(25);
getToken();
}
else if (currentTok.type == beginsym)
{
getToken();
statement();
while (currentTok.type == semicolonsym)
{
getToken();
statement();
}
if (currentTok.type != endsym)
errorMessage(25);
getToken();
}
else if (currentTok.type == ifsym)
{
getToken();
condition();
if (currentTok.type != thensym)
errorMessage(25);
getToken();
statement();
}
else if (currentTok.type == whilesym)
{
getToken();
condition();
if (currentTok.type != dosym)
errorMessage(25);
getToken();
statement();
}
}
/*condition ::= “odd” expression
| expression rel-op expression */
void condition()
{
if (currentTok.type == oddsym)
{
getToken();
expression();
}
else
{
expression();
if (currentTok.type != leqsym || currentTok.type != neqsym || currentTok.type != lessym || currentTok.type != geqsym || currentTok.type != gtrsym || currentTok.type != eqsym)
errorMessage(25);
getToken();
expression();
}
}
/* expression ::= [ “+” | “-” ] term { ( “+” | “-” ) term} */
void expression()
{
if (currentTok.type == plussym || currentTok.type == minussym)
getToken();
term();
while (currentTok.type == plussym || currentTok.type == minussym)
{
getToken();
term();
}
}
/* term ::= factor { ( “*” | “/” ) factor } */
void term()
{
factor();
while(currentTok.type == multsym || currentTok.type == slashsym) {
getToken();
factor();
/*
// If mult - add mult to code
if(currentTok.type == multsym) {
print(2, 0, 4);
}
// If div - add div to code
else {
print(2, 0, 5);
}
*/
}
}
/* factor ::= ident | number | “(” expression “)”*/
void factor()
{
if (currentTok.type == identsym)
getToken();
else if (currentTok.type == numbersym)
getToken();
else if (currentTok.type == lparentsym)
{
getToken();
expression();
if (currentTok.type != rparentsym)
errorMessage(25);
getToken();
}
else
errorMessage(25);
}
/* function to get the current token */
void getToken()
{
currentTok = tokens[tokenNumber++];
}
/* Error messages for the tiny PL/0 Parser */
void errorMessage(int x)
{
/*if(x != 0)
{
printf("Error number %d: ", x);
}
*/
switch(x)
{
case 0:
printf("No errors, program is syntactically correct.\n");
break;
case 1:
printf("Use = instead of :=.\n");
break;
case 2:
printf("= must be followed by a number.\n");
break;
case 3:
printf("Identifier must be followed by a =.\n");
break;
case 4:
printf("const, var, procedure must be followed by identifier.\n");
break;
case 5:
printf("Semicolon or comma missing.\n");
break;
case 6:
printf("Incorrect symbol after procedure declaration.\n");
break;
case 7:
printf("Statement expected.\n");
break;
case 8:
printf("Incorrect symbol after statement part in block.\n");
break;
case 9:
printf("Period expected.\n");
break;
case 10:
printf("Semicolon between statements missing.\n");
break;
case 11:
printf("Undeclared identifier.\n");
break;
case 12:
printf("Assignment to constant or procedure is not allowed.\n");
break;
case 13:
printf("Assignment operator expected.\n");
break;
case 14:
printf("call must be followed by an identifier.\n");
break;
case 15:
printf("Call of a constant or variable is meaningless\n");
break;
case 16:
printf("then expected.\n");
break;
case 17:
printf("Semicolon or } expected.\n");
break;
case 18:
printf("do expected.\n");
break;
case 19:
printf("Incorrect symbol following statement.\n");
break;
case 20:
printf("Relational operator expected.\n");
break;
case 21:
printf("Expression must not contain a procedure identifier.\n");
break;
case 22:
printf("Right parenthesis missing.\n");
break;
case 23:
printf("The preceding factor cannot begin with this symbol\n");
break;
case 24:
printf("An expression cannot begin with this symbol.\n");
break;
case 25:
printf("This number is too large.\n");
break;
default:
printf("Error. There was a problem with the code.\n");
}
exit(x);
}