Skip to content

Commit 52fb4e4

Browse files
committed
Use 'Marker' nonterminal for sub-blocks
1 parent a4c70fb commit 52fb4e4

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

grammar.y

100644100755
Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
%token <IntT> INTEGER;
7070
%token <BoolT> BOOLEAN;
7171

72-
%nterm <Tree*> Block Stmnts Stmnt If Else While Repeat For Sub
72+
%nterm <Tree*> Block Stmnts Stmnt Marker
7373
%nterm <Expression*> Exp BoolExp
7474
%nterm <std::string> SubId SubCall ForId ForInId
7575
%nterm <std::pair<std::string,ExpI>> Field Param
@@ -237,19 +237,19 @@ Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
237237
prev->link($$);
238238
prev = $$;
239239
}
240-
| If BoolExp THEN EOL Block ElseIfs Else EOL Block ENDIF EOL {
241-
$$ = new If($2, $5, $9, $6);
242-
$1->link($$);
240+
| IF BoolExp THEN EOL Marker Block ElseIfs ELSE EOL Marker Block ENDIF EOL {
241+
$$ = new If($2, $6, $11, $7);
242+
$5->link($$);
243243
prev = $$;
244244
}
245-
| If BoolExp THEN EOL Block Else EOL Block ENDIF EOL {
246-
$$ = new If($2, $5, $8);
247-
$1->link($$);
245+
| IF BoolExp THEN EOL Marker Block ELSE EOL Marker Block ENDIF EOL {
246+
$$ = new If($2, $6, $10);
247+
$5->link($$);
248248
prev = $$;
249249
}
250-
| If BoolExp THEN EOL Block ENDIF EOL {
251-
$$ = new If($2, $5);
252-
$1->link($$);
250+
| IF BoolExp THEN EOL Marker Block ENDIF EOL {
251+
$$ = new If($2, $6);
252+
$5->link($$);
253253
prev = $$;
254254
}
255255
| OUTPUT Exp EOL {
@@ -261,25 +261,25 @@ Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
261261
prev->link($$);
262262
prev = $$;
263263
}
264-
| While BoolExp EOL Block ENDWHILE EOL {
265-
$$ = new While($2, $4);
266-
$1->link($$);
264+
| WHILE BoolExp EOL Marker Block ENDWHILE EOL {
265+
$$ = new While($2, $5);
266+
$4->link($$);
267267
prev = $$;
268268
}
269-
| Repeat EOL Block UNTIL BoolExp EOL {
270-
$$ = new RepeatUntil($5, $3);
271-
$1->link($$);
269+
| REPEAT EOL Marker Block UNTIL BoolExp EOL {
270+
$$ = new RepeatUntil($6, $4);
271+
$3->link($$);
272272
prev = $$;
273273
}
274-
| For ForId ASSIGN Exp TO Exp EOL Block ENDFOR EOL {
274+
| FOR ForId ASSIGN Exp TO Exp EOL Marker Block ENDFOR EOL {
275275
if (($4->type() != ExpI::IntT) || ($6->type() != ExpI::IntT)) {
276276
error(location(@4.begin, @6.end), "FOR expressions must be IntExp");
277277
}
278-
$$ = new For($2, $4, $6, new Value(1), $8);
279-
$1->link($$);
278+
$$ = new For($2, $4, $6, new Value(1), $9);
279+
$8->link($$);
280280
prev = $$;
281281
}
282-
| For ForId ASSIGN Exp TO Exp STEP Exp EOL Block ENDFOR EOL {
282+
| FOR ForId ASSIGN Exp TO Exp STEP Exp EOL Marker Block ENDFOR EOL {
283283
if (($4->type() != ExpI::IntT) || ($6->type() != ExpI::IntT) || ($8->type() != ExpI::IntT)) {
284284
error(location(@4.begin, @8.end), "FOR expressions must be IntExp");
285285
}
@@ -289,16 +289,16 @@ Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
289289
else if (!std::get<IntT>($8->apply())) {
290290
error(@8, "STEP must be non-zero");
291291
}
292-
$$ = new For($2, $4, $6, $8, $10);
293-
$1->link($$);
292+
$$ = new For($2, $4, $6, $8, $11);
293+
$10->link($$);
294294
prev = $$;
295295
}
296-
| For ForInId IN Exp EOL Block ENDFOR EOL {
296+
| FOR ForInId IN Exp EOL Marker Block ENDFOR EOL {
297297
if ($4->type() != ExpI::StringT) {
298298
error(@4, "FOR-IN expressions must be StringExp");
299299
}
300-
$$ = new ForIn($2, $4, $6);
301-
$1->link($$);
300+
$$ = new ForIn($2, $4, $7);
301+
$6->link($$);
302302
prev = $$;
303303
}
304304
| RECORD ID EOL Fields ENDRECORD EOL {
@@ -329,20 +329,20 @@ Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
329329
prev->link($$);
330330
prev = $$;
331331
}
332-
| Sub SubId Params RPAREN EOL Block ENDSUBROUTINE EOL {
332+
| SUBROUTINE SubId Params RPAREN EOL Marker Block ENDSUBROUTINE EOL {
333333
table->endsub($3);
334-
$$ = new Subroutine(new Decls(table, $2), $2, std::pair { $3, ExpI::None }, $6);
335-
$1->link($$);
334+
$$ = new Subroutine(new Decls(table, $2), $2, std::pair { $3, ExpI::None }, $7);
335+
$6->link($$);
336336
prev = $$;
337337
}
338-
| Sub SubId Params RPAREN EOL Block RETURN Exp EOL ENDSUBROUTINE EOL {
339-
table->endsub($3, $8->type());
340-
if ($8->type() > ExpI::StringT) {
338+
| SUBROUTINE SubId Params RPAREN EOL Marker Block RETURN Exp EOL ENDSUBROUTINE EOL {
339+
table->endsub($3, $9->type());
340+
if ($9->type() > ExpI::StringT) {
341341
error(@8, "unsupported return type for subroutine");
342342
YYERROR;
343343
}
344-
$$ = new Subroutine(new Decls(table, $2), $2, std::pair { $3, $8->type() }, $6, $8);
345-
$1->link($$);
344+
$$ = new Subroutine(new Decls(table, $2), $2, std::pair { $3, $9->type() }, $7, $9);
345+
$6->link($$);
346346
prev = $$;
347347
}
348348
| SubCall Args RPAREN EOL {
@@ -364,29 +364,14 @@ Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
364364
}
365365
;
366366

367-
If : IF { $$ = prev; prev = new Empty; }
368-
;
369-
370-
Else : ELSE { $$ = prev; prev = new Empty; }
371-
;
372-
373-
While : WHILE { $$ = prev; prev = new Empty; }
374-
;
375-
376-
Repeat : REPEAT { $$ = prev; prev = new Empty; }
377-
;
378-
379-
For : FOR { $$ = prev; prev = new Empty; }
380-
;
381-
382-
Sub : SUBROUTINE { $$ = prev; prev = new Empty; }
367+
Marker : %empty { $$ = prev; prev = new Empty; } /* [Compilers] p350 */
383368
;
384369

385370
ElseIfs : ElseIf { $$.push_back($1); }
386371
| ElseIfs ElseIf { $$ = $1; $$.push_back($2); }
387372
;
388373

389-
ElseIf : Else IF Exp THEN EOL Block { $$ = std::pair { $3, $6 }; }
374+
ElseIf : ELSE IF Exp THEN EOL Marker Block { $$ = std::pair { $3, $7 }; }
390375
;
391376

392377
ForId : ID {

0 commit comments

Comments
 (0)