home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / src / yypanic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  5.2 KB  |  196 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)yypanic.c    5.2 (Berkeley) 4/16/91";
  36. #endif /* not lint */
  37.  
  38. #include "whoami.h"
  39. #include "0.h"
  40. #include "tree_ty.h"    /* must be included for yy.h */
  41. #include "yy.h"
  42.  
  43. struct yytok oldpos;
  44. /*
  45.  * The routine yyPerror coordinates the panic when
  46.  * the correction routines fail. Three types of panics
  47.  * are possible - those in a declaration part, those
  48.  * in a statement part, and those in an expression.
  49.  *
  50.  * Declaration part panics consider insertion of "begin",
  51.  * expression part panics will stop on more symbols.
  52.  * The panics are otherwise the same.
  53.  *
  54.  * ERROR MESSAGE SUPPRESSION STRATEGY: August 11, 1977
  55.  *
  56.  * If the parser has not made at least 2 moves since the last point of
  57.  * error then we want to suppress the supplied error message.
  58.  * Otherwise we print it.
  59.  * We then skip input up to the next solid symbol.
  60.  */
  61. yyPerror(cp, kind)
  62.     char *cp;
  63.     register int kind;
  64. {
  65.     register int ishifts, brlev;
  66.  
  67.     copy((char *) (&oldpos), (char *) (&Y), sizeof oldpos);
  68.     brlev = 0;
  69.     if (yychar < 0)
  70.         yychar = yylex();
  71.     for (ishifts = yyshifts; ; yychar = yylex(), yyshifts++)
  72.         switch (yychar) {
  73.             case YILLCH:
  74.                 yerror("Illegal character");
  75.                 if (ishifts == yyshifts)
  76.                     yyOshifts = 0;
  77.                 continue;
  78.             case YEOF:
  79.                 if (kind == PDECL) {
  80.                     /*
  81.                      * we have paniced to end of file
  82.                      * during declarations. Separately
  83.                      * compiled segments can syntactically
  84.                      * exit without any error message, so
  85.                      * we force one here.
  86.                      */
  87.                     yerror(cp);
  88.                     continuation();
  89.                     yyunexeof();
  90.                 }
  91.                 goto quiet;
  92.             case ';':
  93.                 if (kind == PPROG)
  94.                     continue;
  95.                 if (kind == PDECL)
  96.                     yychar = yylex();
  97.                 goto resume;
  98.             case YEND:
  99.                 if (kind == PPROG)
  100.                     continue;
  101.             case YPROCEDURE:
  102.             case YFUNCTION:
  103.                 goto resume;
  104.             case YLABEL:
  105.             case YTYPE:
  106.             case YCONST:
  107.             case YVAR:
  108.                 if (kind == PSTAT) {
  109.                     yerror("Declaration found when statement expected");
  110.                     goto quiet;
  111.                 }
  112.             case YBEGIN:
  113.                 goto resume;
  114.             case YFOR:
  115.             case YREPEAT:
  116.             case YWHILE:
  117.             case YGOTO:
  118.             case YIF:
  119.                 if (kind != PDECL)
  120.                     goto resume;
  121.                 yerror("Expected keyword begin after declarations, before statements");
  122.                 unyylex(&Y);
  123.                 yychar = YBEGIN;
  124.                 yylval = nullsem(YBEGIN);
  125.                 goto quiet;
  126.             case YTHEN:
  127.             case YELSE:
  128.             case YDO:
  129.                 if (kind == PSTAT) {
  130.                     yychar = yylex();
  131.                     goto resume;
  132.                 }
  133.                 if (kind == PEXPR)
  134.                     goto resume;
  135.                 continue;
  136.             case ')':
  137.             case ']':
  138.                 if (kind != PEXPR)
  139.                     continue;
  140.                 if (brlev == 0)
  141.                     goto resume;
  142.                 if (brlev > 0)
  143.                     brlev--;
  144.                 continue;
  145.             case '(':
  146.             case '[':
  147.                 brlev++;
  148.                 continue;
  149.             case ',':
  150.                 if (brlev != 0)
  151.                     continue;
  152.             case YOF:
  153.             case YTO:
  154.             case YDOWNTO:
  155.                 if (kind == PEXPR)
  156.                     goto resume;
  157.                 continue;
  158. #ifdef PI
  159.             /*
  160.              * A rough approximation for now
  161.              * Should be much more lenient on suppressing
  162.              * warnings.
  163.              */
  164.             case YID:
  165.                 syneflg = TRUE;
  166.                 continue;
  167. #endif
  168.         }
  169. resume:
  170.     if (yyOshifts >= 2) {
  171.         if (yychar != -1)
  172.             unyylex(&Y);
  173.         copy((char *) (&Y), (char *) (&oldpos), sizeof Y);
  174.         yerror(cp);
  175.         yychar = yylex();
  176.     }
  177. quiet:
  178.     if (yyshifts - ishifts > 2 && opt('r')) {
  179.         setpfx('r');
  180.         yerror("Parsing resumes");
  181.     }
  182.     /*
  183.      * If we paniced in the statement part,
  184.      * and didn't stop at a ';', then we insert
  185.      * a ';' to prevent the recovery from immediately
  186.      * inserting one and complaining about it.
  187.      */
  188.     if (kind == PSTAT && yychar != ';') {
  189.         unyylex(&Y);
  190.         yyshifts--;
  191.         yytshifts--;
  192.         yychar = ';';
  193.         yylval = nullsem(';');
  194.     }
  195. }
  196.