Path: utzoo!attcan!uunet!husc6!purdue!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.misc Subject: Re: Algol-style semicolons Message-ID: <1132@goofy.megatest.UUCP> Date: 28 Dec 88 00:00:49 GMT References: <600@tuck.nott-cs.UUCP> Organization: Megatest Corporation, San Jose, Ca Lines: 48 > In article <4396@tekgvs.GVS.TEK.COM> toma@tekgvs.GVS.TEK.COM (Tom Almy) writes: > >[...] "Why do we need semicolons at all?". > They are "gobble-stoppers" -- symbols which clue the compiler as to how to recover from a syntax error. A typical LR technique is the following: On an error, the compiler discards states from the parse stack until a state which can shift "error" is uncovered. It shifts the error. It then makes reductions any necessary before it discards (gobbles) tokens which can not be shifted. Success is declared when some fixed number of consecutive tokens have been shifted since the last error. The usual number is three. But when a gobble stopper is found, you can say, "That's enough." Here's a short yacc example from a production Pascal compiler currently under construction. The nonterminal "semicolon" is used all over the place. When a semicolon is actually found, yyerrok tells the parser that no more tokens need be shifted in order to consider that an error has been "recovered from". If the semicolon is missing, we could be silent, but for the sake of portability, we issue a warning message. (Note to gurus: The version of yacc being used is a repaired version which does not have the bogus default reduction bug, which would invalidate the error-recovery.) semicolon: ';' { yyerrok; } | '.' { Mpc_warning("Period replaced with semicolon."); } | { Mpc_warning("Semicolon inserted."); } ; label_declaration : LABEL integer_list semicolon { Mpc_add_labels($2); } ; integer_list : INTEGER { $$ = List_new(); List_append($$, $1); } | integer_list ',' INTEGER { List_append($1, $3); | error { $$ = List_new(); } | integer_list ',' error { $$ = $1; }