-
Notifications
You must be signed in to change notification settings - Fork 0
/
p02par.y
402 lines (402 loc) · 9.84 KB
/
p02par.y
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
%{
//---------------------------------------------------------------------
//File p02par.y contains a specification for simple expressions
//defined by Thomas R. Turner.
//--------------------------------------------------------------------
// Author 1: Steven Chambers
// Student ID: *20342421
// E-Mail: [email protected]
// Author 2: Melicent King
// Student ID: *20355157
// E-mail: [email protected]
// Course: CMSC 4023 - Programming Languages
// CRN: 12105
// Project: p02
// Due: November 5, 2014
// Account: tt060
//--------------------------------------------------------------------
//---------------------------------------------------------------------
//C++ include files
//---------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
//---------------------------------------------------------------------
//Application include files
//---------------------------------------------------------------------
#include "p02lex.h"
#include "p02par.h"
//---------------------------------------------------------------------
//Externals
//---------------------------------------------------------------------
extern int lineNo;
extern int colNo;
#define YYDEBUG 1
//---------------------------------------------------------------------
//Function prototypes
//---------------------------------------------------------------------
void yyerror(const char* m);
//---------------------------------------------------------------------
%}
%token NOTOKEN
%token INTLIT
%token PLUS
%token MINUS
%token STAR
%token SLASH
%token LPAREN
%token RPAREN
%token RBRACKET
%token LBRACKET
%token COLON
%token SEMICOLON
%token COMMA
%token PERIOD
%token RANGE
%token ASSIGNOP
%token EQU
%token NEQ
%token LES
%token GRT
%token GEQ
%token CHRLIT
%token REALIT
%token ID
%token AND
%token ARRAY
%token BEGAN
%token DIV
%token DO
%token ELSE
%token END
%token FOR
%token FUNCTION
%token IF
%token INTEGER
%token MOD
%token NOT
%token OF
%token OR
%token PROCEDURE
%token PROGRAM
%token REAL
%token REPEAT
%token THEN
%token TO
%token UNTIL
%token VAR
%token WHILE
%token LEQ
%token ERROR
%debug
%error-verbose
%%
program:
PROGRAM ID LPAREN identifier_list RPAREN SEMICOLON declarations subprogram_declarations compound_statement PERIOD
{ cout << endl << "program -> PROGRAM ID ( identifier-list ) ; declarations subprogram-declarations compound-statement .";
}
identifier_list:
ID
{ cout << endl << "identifier-list -> ID";
}
identifier_list:
identifier_list COMMA ID
{ cout << endl << "identifier-list -> identifier-list, ID";
}
declarations:
{ /* empty */
cout << endl << "declarations -> \"\"";
}
declarations:
declarations VAR identifier_list COLON type SEMICOLON
{ cout << endl << "declarations -> declarations VAR identifier-list : type ;";
}
type:
standard_type
{ cout << endl << "type -> standard-type";
}
type:
ARRAY LBRACKET INTLIT RANGE INTLIT RBRACKET OF standard_type
{ cout << endl << "type -> ARRAY [ INTLIT .. INTLIT ] OF standard-type";
}
standard_type:
INTEGER
{ cout << endl << "standard-type -> INTEGER";
}
standard_type:
REAL
{ cout << endl << "standard-type -> REAL";
}
subprogram_declarations:
{ /* empty */
cout << endl << "subprogram-declarations -> \"\"";
}
subprogram_declarations:
subprogram_declarations subprogram_declaration SEMICOLON
{ cout << endl << "subprogram-declarations -> subprogram-declarations subprogram-declaration ;";
}
subprogram_declaration:
subprogram_head declarations compound_statement
{ cout << endl << "subprogram-declaration -> subprogram-head declarations compound-statement";
}
subprogram_head:
FUNCTION ID arguments COLON standard_type SEMICOLON
{ cout << endl << "subprogram-head -> FUNCTION ID arguments : standard-type ;";
}
subprogram_head:
PROCEDURE ID arguments SEMICOLON
{ cout << endl << "subprogram-head -> PROCEDURE ID arguments;";
}
arguments:
{ /* empty */
cout << endl << "arguments -> \"\"";
}
arguments:
LPAREN parameter_list RPAREN
{ cout << endl << "arguments -> ( parameter list )";
}
parameter_list:
identifier_list COLON type
{ cout << endl << "parameter-list -> identifier-list : type";
}
parameter_list:
parameter_list SEMICOLON identifier_list COLON type
{ cout << endl << "parameter-list -> parameter-list ; identifier-list : type";
}
compound_statement:
BEGAN optional_statements END
{ cout << endl << "compound-statement -> BEGIN optional-statements END";
}
optional_statements:
statement_list
{ cout << endl << "optional-statements -> statement-list";
}
optional_statements:
{ /* empty */
cout << endl << "optional-statements -> \"\"";
}
statement_list:
statement
{ cout << endl << "statement-list -> statement";
}
statement_list:
statement_list SEMICOLON statement
{ cout << endl << "statement-list -> statement-list; statement";
}
statement:
variable ASSIGNOP expression
{ cout << endl << "statement -> variable := expression";
}
statement:
procedure_statement
{ cout << endl << "statement -> procedure-statement";
}
statement:
compound_statement
{ cout << endl << "statement -> compound-statement";
}
statement:
IF expression THEN statement ELSE statement
{ cout << endl << "statement -> IF expression THEN statement ELSE statement";
}
statement:
WHILE expression DO statement
{ cout << endl << "statement -> WHILE expression DO statement";
}
variable:
ID
{ cout << endl << "variable -> ID";
}
variable:
ID LBRACKET expression RBRACKET
{ cout << endl << "variable -> ID [ expression ]";
}
procedure_statement:
ID
{ cout << endl << "procedure-statement -> ID";
}
procedure_statement:
ID LPAREN expression_list RPAREN
{ cout << endl << "procedure-statement -> ID ( expression-list )";
}
expression_list:
expression
{ cout << endl << "expression-list -> expression";
}
expression_list:
expression_list COMMA expression
{ cout << endl << "expression-list -> expression-list, expression";
}
expression:
simple_expression
{ cout << endl << "expression -> simple-expression";
}
expression:
simple_expression relop simple_expression
{
cout << endl << "expression -> simple_expression relop simple_expression";
}
relop:
EQU
{
cout << endl << "relop -> =";
}
relop:
NEQ
{
cout << endl << "relop -> <>";
}
relop:
LES
{
cout << endl << "relop -> <";
}
relop:
LEQ
{
cout << endl << "relop -> <=";
}
relop:
GRT
{
cout << endl << "relop -> >";
}
relop:
GEQ
{
cout << endl << "relop -> >=";
}
simple_expression:
term
{
cout << endl << "simple_expression -> term";
}
simple_expression:
sign term
{
cout << endl << "simple_expression -> sign term";
}
simple_expression:
simple_expression addop term
{
cout << endl << "simple_expression -> simple-expression addop term";
}
addop:
PLUS
{
cout << endl << "addop -> +";
}
addop:
MINUS
{
cout << endl << "addop -> -";
}
addop:
OR
{
cout << endl << "addop -> OR";
}
term:
factor
{
cout << endl << "term -> factor";
}
term:
term mulop factor
{
cout << endl << "term -> term mulop factor";
}
mulop:
STAR
{
cout << endl << "mulop -> *";
}
mulop:
SLASH
{
cout << endl << "mulop -> /";
}
mulop:
DIV
{
cout << endl << "mulop -> DIV";
}
mulop:
MOD
{
cout << endl << "mulop -> MOD";
}
mulop:
AND
{
cout << endl << "mulop -> AND";
}
factor:
ID
{
cout << endl << "factor -> ID";
}
factor:
ID LPAREN expression_list RPAREN
{
cout << endl << "factor -> ID(expression_list)";
}
factor:
INTLIT
{
cout << endl << "factor -> INTLIT";
}
factor:
REALIT
{
cout << endl << "factor -> REALIT";
}
factor:
LPAREN expression RPAREN
{
cout << endl << "factor -> (expression)";
}
factor:
NOT factor
{
cout << endl << "factor -> NOT factor";
}
sign:
PLUS
{
cout << endl << "sign -> +";
}
sign:
MINUS
{
cout << endl << "sign -> -";
}
%%
//---------------------------------------------------------------------
//User function section
//---------------------------------------------------------------------
struct Error {
Error(const char* m) {
cout << endl << "line(" << lineNo << ") col(" << colNo << ")" << m;
cout << endl;
}
};
//---------------------------------------------------------------------
//Function yyerror is necessary
//---------------------------------------------------------------------
void yyerror(const char* m)
{
throw Error(m);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//Parser::Parser(FILE* i):Lexer(i){};
int Parser::Parse(){return yyparse();}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
Parser::~Parser(){}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//End of exppar.y
//---------------------------------------------------------------------