-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.y
309 lines (309 loc) · 8.48 KB
/
project.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
%{
#include <stdio.h>
#include <stdlib.h>
int errorCount = 0;
%}
%{
int yylex();
int yyerror(char *s);
void error_msg(char *s);
%}
%token CHAR INT FLOAT BOOL STR VOID TRUE FALSE
%token AND_OP OR_OP DOLLAR_SIGN NOT_OP IDENT
%token IF ELIF ELSE
%token WHILE FOR BREAK CONTINUE
%token FUNC RETURN
%token BLTIN_SHOW_ON_MAP BLTIN_SEARCH_LOCATION BLTIN_GET_ROAD_SPEED BLTIN_GET_LOCATION BLTIN_TARGET BLTIN_GET_ROADS BLTIN_GET_CROSSROADS BLTIN_GET_CROSSROADS_NUM BLTIN_GET_ROADS_NUM
%token LESS_EQ_OP GREATER_EQ_OP NOT_EQ_OP EQUALITY_OP LESS_OP GREATER_OP
%token EQUAL_OP MULTIPLY_OP DIVIDE_OP SUB_OP ADD_OP MOD_OP POW_OP
%token INT_LITERAL FLOAT_LITERAL STR_LITERAL CHAR_LITERAL
%token COMMA SEMICOLON LEFT_PARANT RIGHT_PARANT CURLY_OPEN CURLY_CLOSE SQR_OPEN SQR_CLOSE
%token GPS ROAD CROSSROAD GRAPH USER
%token HOME HOSPITAL SCHOOL BRIDGE MALL BUSSTOP HOTEL POSTOFFICE
%token BLTIN_COLLOBORATE_USERS BLTIN_INSTRUCT_USER BLTIN_INCREASE_SCORE_OF_ROAD BLTIN_DECREASE_SCORE_OF_ROAD BLTIN_GET_SCORE_OF_ROAD BLTIN_SHOW_ROAD_ON_MAP BLTIN_SHOW_CROSSROAD_ON_MAP BLTIN_ADD_CROSSROAD BLTIN_ADD_ROAD BLTIN_PRINT
%%
program : function
| function program
;
function : FUNC return_type IDENT LEFT_PARANT parameter_list RIGHT_PARANT block
| error {error_msg("Missing 'func', not a valid function declaration!");}
;
return_type : data_type
| data_type DOLLAR_SIGN
| geo_types
| geo_types DOLLAR_SIGN
| three_d_objects
| three_d_objects DOLLAR_SIGN
| VOID
;
parameter_list : empty
| VOID
| data_type IDENT
| data_type DOLLAR_SIGN IDENT
| geo_types IDENT
| geo_types DOLLAR_SIGN IDENT
| three_d_objects IDENT
| three_d_objects DOLLAR_SIGN IDENT
| parameter_list COMMA data_type IDENT
| parameter_list COMMA data_type DOLLAR_SIGN IDENT
| parameter_list COMMA geo_types IDENT
| parameter_list COMMA geo_types DOLLAR_SIGN IDENT
| parameter_list COMMA three_d_objects IDENT
| parameter_list COMMA three_d_objects DOLLAR_SIGN IDENT
;
argument_list : empty
| IDENT
| literal
| argument_list COMMA IDENT
| argument_list COMMA literal
;
block : CURLY_OPEN stmt_list CURLY_CLOSE
| CURLY_OPEN empty CURLY_CLOSE
;
stmt_list : stmt
| stmt_list stmt
;
stmt : declaration SEMICOLON
| assignment SEMICOLON
| function_call SEMICOLON
| BREAK SEMICOLON
| CONTINUE SEMICOLON
| RETURN SEMICOLON
| RETURN arithmetic_exp SEMICOLON
| RETURN TRUE SEMICOLON
| RETURN FALSE SEMICOLON
| loop
| if_stmt
;
declaration : data_type IDENT
| data_type IDENT EQUAL_OP RHS
| data_type DOLLAR_SIGN IDENT
| data_type DOLLAR_SIGN IDENT EQUAL_OP CURLY_OPEN literal_list CURLY_CLOSE
| GPS IDENT EQUAL_OP tuple
| GPS DOLLAR_SIGN IDENT EQUAL_OP CURLY_OPEN tuple_list CURLY_CLOSE
| ROAD IDENT EQUAL_OP road
| CROSSROAD IDENT EQUAL_OP cross_road
| ROAD DOLLAR_SIGN IDENT EQUAL_OP CURLY_OPEN roads_list CURLY_CLOSE
| CROSSROAD DOLLAR_SIGN IDENT EQUAL_OP CURLY_OPEN cross_roads_list CURLY_CLOSE
| GRAPH IDENT EQUAL_OP CURLY_OPEN graph_arguments CURLY_CLOSE
| USER IDENT EQUAL_OP LEFT_PARANT STR_LITERAL COMMA cross_road RIGHT_PARANT
| USER IDENT EQUAL_OP LEFT_PARANT IDENT COMMA cross_road RIGHT_PARANT
| three_d_objects IDENT EQUAL_OP LEFT_PARANT STR_LITERAL COMMA cross_road RIGHT_PARANT
| three_d_objects IDENT EQUAL_OP LEFT_PARANT IDENT COMMA cross_road RIGHT_PARANT
| three_d_objects DOLLAR_SIGN IDENT EQUAL_OP CURLY_OPEN three_d_arguments CURLY_CLOSE
;
three_d_arguments : LEFT_PARANT IDENT COMMA cross_road RIGHT_PARANT
| LEFT_PARANT STR_LITERAL COMMA cross_road RIGHT_PARANT
| IDENT
| empty
| three_d_arguments COMMA LEFT_PARANT STR_LITERAL COMMA cross_road RIGHT_PARANT
| three_d_arguments COMMA LEFT_PARANT IDENT COMMA cross_road RIGHT_PARANT
| three_d_arguments COMMA IDENT
;
graph_arguments : empty
| IDENT COMMA IDENT
| IDENT COMMA LEFT_PARANT roads_list RIGHT_PARANT
| LEFT_PARANT cross_roads_list RIGHT_PARANT COMMA IDENT
| LEFT_PARANT cross_roads_list RIGHT_PARANT COMMA LEFT_PARANT roads_list RIGHT_PARANT
;
road : LEFT_PARANT tuple COMMA tuple RIGHT_PARANT
| LEFT_PARANT tuple COMMA IDENT RIGHT_PARANT
| LEFT_PARANT IDENT COMMA tuple RIGHT_PARANT
| LEFT_PARANT IDENT COMMA IDENT RIGHT_PARANT
;
cross_road : tuple
| IDENT
;
roads_list : empty
| road
| IDENT
| roads_list COMMA road
| roads_list COMMA IDENT
;
cross_roads_list : cross_road
| cross_roads_list COMMA cross_road
;
tuple : LEFT_PARANT long_lat_param COMMA long_lat_param RIGHT_PARANT
| empty
;
literal_list : empty
| literal
| literal_list COMMA literal
;
tuple_list : tuple
| tuple_list COMMA tuple
;
arithmetic_exp : term
| arithmetic_exp ADD_OP term
| arithmetic_exp SUB_OP term
;
term : term MULTIPLY_OP literal
| term DIVIDE_OP literal
{ if ($3) $$ = $1 / $3;
else {error_msg("Divide by zero!");}
}
| term POW_OP literal
| term MOD_OP literal
| term MULTIPLY_OP IDENT
| term DIVIDE_OP IDENT
| term POW_OP IDENT
| term MOD_OP IDENT
| factor
;
factor : LEFT_PARANT arithmetic_exp RIGHT_PARANT
| literal
| IDENT
;
RHS : arithmetic_exp
| function_call
| bool_exp
| IDENT SQR_OPEN INT_LITERAL SQR_CLOSE
| IDENT SQR_OPEN IDENT SQR_CLOSE
| error{error_msg("Not a valid expression!");}
;
function_call : IDENT LEFT_PARANT argument_list RIGHT_PARANT
| BLTIN_SHOW_ON_MAP LEFT_PARANT long_lat_param COMMA long_lat_param RIGHT_PARANT
| BLTIN_SEARCH_LOCATION LEFT_PARANT IDENT RIGHT_PARANT
| BLTIN_SEARCH_LOCATION LEFT_PARANT STR_LITERAL RIGHT_PARANT
| BLTIN_GET_ROAD_SPEED LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_GET_LOCATION LEFT_PARANT user_param RIGHT_PARANT
| BLTIN_TARGET LEFT_PARANT STR_LITERAL RIGHT_PARANT
| BLTIN_TARGET LEFT_PARANT IDENT RIGHT_PARANT
| BLTIN_GET_ROADS LEFT_PARANT cross_road RIGHT_PARANT
| BLTIN_GET_CROSSROADS LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_GET_ROADS_NUM LEFT_PARANT cross_road RIGHT_PARANT
| BLTIN_GET_CROSSROADS_NUM LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_COLLOBORATE_USERS LEFT_PARANT user_param RIGHT_PARANT
| BLTIN_INSTRUCT_USER LEFT_PARANT user_param COMMA destination RIGHT_PARANT
| BLTIN_INCREASE_SCORE_OF_ROAD LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_DECREASE_SCORE_OF_ROAD LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_GET_SCORE_OF_ROAD LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_SHOW_ROAD_ON_MAP LEFT_PARANT road_param RIGHT_PARANT
| BLTIN_SHOW_CROSSROAD_ON_MAP LEFT_PARANT cross_road RIGHT_PARANT
| BLTIN_ADD_CROSSROAD LEFT_PARANT IDENT COMMA cross_road RIGHT_PARANT
| BLTIN_ADD_ROAD LEFT_PARANT IDENT COMMA road_param RIGHT_PARANT
| BLTIN_PRINT LEFT_PARANT argument_list RIGHT_PARANT
;
destination : LEFT_PARANT STR_LITERAL COMMA cross_road RIGHT_PARANT
| FLOAT_LITERAL
| IDENT
| tuple
;
user_param : LEFT_PARANT STR_LITERAL COMMA cross_road RIGHT_PARANT
| LEFT_PARANT IDENT COMMA cross_road RIGHT_PARANT
| IDENT
;
road_param : road
| IDENT
;
long_lat_param : FLOAT_LITERAL
| IDENT
;
literal : SUB_OP INT_LITERAL
| ADD_OP INT_LITERAL
| SUB_OP FLOAT_LITERAL
| ADD_OP FLOAT_LITERAL
| INT_LITERAL
| FLOAT_LITERAL
| STR_LITERAL
| CHAR_LITERAL
;
assignment_op : EQUAL_OP
| MULTIPLY_OP
| DIVIDE_OP
| SUB_OP
| ADD_OP
| MOD_OP
| POW_OP
;
assignment : LHS assignment_op RHS
;
LHS : IDENT
| error{error_msg("Invalid identifier declaration!");}
;
loop : while
| for
;
while : WHILE LEFT_PARANT bool_exp RIGHT_PARANT block
| WHILE LEFT_PARANT IDENT RIGHT_PARANT block
;
for : FOR LEFT_PARANT for_stmt RIGHT_PARANT block
;
for_stmt : for_init SEMICOLON bool_exp SEMICOLON assignment
| for_init SEMICOLON IDENT SEMICOLON assignment
;
for_init : declaration
| assignment
;
if_stmt : IF LEFT_PARANT bool_exp RIGHT_PARANT block
| IF LEFT_PARANT IDENT RIGHT_PARANT block
| IF LEFT_PARANT function_call RIGHT_PARANT block
| if_stmt ELIF LEFT_PARANT bool_exp RIGHT_PARANT block
| if_stmt ELIF LEFT_PARANT IDENT RIGHT_PARANT block
| if_stmt ELSE block
;
bool_exp : comparison
| NOT_OP IDENT
| TRUE
| FALSE
;
comparison : IDENT relational_op compared
| bool_exp logic_op compared
| IDENT logic_op compared
| function_call relational_op compared
;
compared : IDENT
| FALSE
| TRUE
| literal
;
relational_op : LESS_EQ_OP
| GREATER_EQ_OP
| NOT_EQ_OP
| EQUALITY_OP
| LESS_OP
| GREATER_OP
;
logic_op : AND_OP
| OR_OP
;
data_type : CHAR
| INT
| FLOAT
| BOOL
| STR
;
three_d_objects : HOME
| HOSPITAL
| SCHOOL
| BRIDGE
| MALL
| BUSSTOP
| HOTEL
| POSTOFFICE
;
geo_types : GPS
| ROAD
| CROSSROAD
| USER
;
empty :
;
%%
#include "lex.yy.c"
int yyerror (char *s) {
errorCount++;
}
int yywrap () {
if (errorCount == 0) {
printf("The program was compiled successfully.\n");
}
return 1;
}
void error_msg (char *s) {
printf("%s Line no: %d found error \n", s, yylineno);
errorCount++;
}
int main (void) {
yyparse();
}