home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / src / yycosts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  6.7 KB  |  268 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[] = "@(#)yycosts.c    5.3 (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. /*
  44.  * Symbol costs for Pascal.
  45.  *
  46.  * Cost strategy of August 14, 1977.
  47.  *
  48.  * The costs determined by the routines in this file are used by
  49.  * the recovery in choosing appropriate corrections.
  50.  * The cost vectors and the error productions in the grammar
  51.  * work together to define the corrective capacity of the grammar.
  52.  *
  53.  * The costs here largely derive from those given in Steve Rhode's
  54.  * thesis for the Pascal-I error correcting parser which he implemented.
  55.  * Some minor changes have been made to adjust for the fact that
  56.  * the current error recovery is not as "smart", both because of the
  57.  * limited forward move and because of the lack of any type information
  58.  * about identifiers.
  59.  *
  60.  * These adjustments largely take the form of increased costs for certain
  61.  * tokens, noticeably keywords which are major brackets such as "begin"
  62.  * "label", "procedure", etc.
  63.  *
  64.  * The overall weighting strategy is still similar to Rhodes' strategy.
  65.  * The costs can be considered:
  66.  *
  67.  *    LOW    <= 3
  68.  *    MEDIUM    4 or 5
  69.  *    HIGH    >= 6
  70.  */
  71.  
  72. /*
  73.  * Insertion costs
  74.  *
  75.  * In addition to the normal symbol insertion costs,
  76.  * there are zero cost insertions here.
  77.  * The current error recovery system treats symbols
  78.  * which have zero insertion cost in a special way,
  79.  * inserting them but suppressing diagnostics.
  80.  * This allows the system to hold of on bracketing
  81.  * error diagnostics about missing end's until the
  82.  * reduction occurs which knows the line number of the
  83.  * corresponding "begin", "repeat", etc.
  84.  * A more intelligent and useful diagnostic can then
  85.  * be printed.
  86.  *
  87.  * Although this routine never allows the insertion
  88.  * of the keyword begin, it can be inserted after a
  89.  * procedure or function body starts, if it was omitted
  90.  * by a special case in the panic routine, which notices
  91.  * the keywords in the statement body of the procedure
  92.  * and inserts the begin to recover.
  93.  *
  94.  * Similarly, we do not insert end-of-file, but
  95.  * the fact that end-of-file is the unique input
  96.  * is noticed by the recovery routines as a special
  97.  * case and handled there.
  98.  */
  99. inscost(sy, before)
  100.     register int sy, before;
  101. {
  102.  
  103.     switch (before) {
  104.         case YEND:
  105.             if (sy == YEND)
  106.                 break;
  107.         case YPROCEDURE:
  108.         case YFUNCTION:
  109.             if (sy == YUNTIL || sy == YEND)
  110.                 return (0);
  111.     }
  112.     switch (sy) {
  113.         case ';':
  114.             return (1);
  115.         case ',':
  116.         case ':':
  117.         case YOF:
  118.         case YDO:
  119.             return (2);
  120.         case YARRAY:
  121.         case '+':
  122.         case '*':
  123.             return (3);
  124.         default:
  125.             return (4);
  126.         case '^':
  127.         case YNOT:
  128.         case YLABEL:
  129.         case YCONST:
  130.         case YTYPE:
  131.         case YVAR:
  132.         case YUNTIL:
  133.         case '(':
  134.         case '[':
  135.         case YWHILE:
  136.         case YWITH:
  137.             return (5);
  138.         case YPROCEDURE:
  139.         case YFUNCTION:
  140.         case YCASE:
  141.             return (6);
  142.         case YEND:
  143.             return (8);
  144.         case YBEGIN:
  145.         case YEOF:
  146.         case YREPEAT:
  147.         case YRECORD:
  148.             return (INFINITY);
  149.     }
  150. }
  151.  
  152. /*
  153.  * Replacement costs
  154.  *
  155.  * Most replacement costs are the same as an insertion
  156.  * plus a deletion cost.  One special case is the replacement
  157.  * of a large number of keywords by an identifier.
  158.  * These are given lower costs, especially the keyword "to".
  159.  */
  160. repcost(what, with)
  161.     register int what, with;
  162. {
  163.     register int c;
  164.  
  165.     if (with == what)
  166.         return (INFINITY);
  167.     if (with == YID && what > ERROR)
  168.         switch (what) {
  169.             case YID:
  170.             case YDOTDOT:
  171.             case YINT:
  172.             case YBINT:
  173.             case YSTRING:
  174.             case YNUMB:
  175.                 break;
  176.             case YTO:
  177.                 return (3);
  178.             default:
  179.                 return (5);
  180.             case YRECORD:
  181.             case YTHEN:
  182.                 return (6);
  183.             case YBEGIN:
  184.                 break;
  185.         }
  186.     if (what == YID && with == YFORWARD)
  187.         return(5);
  188.     if (what == ';' && (with == ',' || with == '.'))
  189.         return (CLIMIT - 1);
  190.     c = delcost(what) + inscost(with, what);
  191.     /*
  192.      * It costs extra to replace something which has
  193.      * semantics by something which doesn't.
  194.      */
  195.     if (nullsem(what) == NIL && nullsem(with) != NIL)
  196.         c += 4;
  197.     return (c);
  198. }
  199.  
  200. /*
  201.  * Deletion costs
  202.  */
  203. delcost(what)
  204.     int what;
  205. {
  206.  
  207.     switch (what) {
  208.         case '.':
  209.         case ':':
  210.         case ',':
  211.         case '=':
  212.         case '(':
  213.             return (3);
  214.         case YELSE:
  215.         case YTHEN:
  216.             return (4);
  217.         default:
  218.             return (5);
  219.         case YLABEL:
  220.         case YCONST:
  221.         case YTYPE:
  222.         case YVAR:
  223.             return (10);
  224.         case YPROCEDURE:
  225.         case YFUNCTION:
  226.         case YBEGIN:
  227.         case YEND:
  228.             return ((CLIMIT * 3) / 4);
  229.         case ';':
  230.         case YEOF:
  231.             return (INFINITY);
  232.     }
  233. }
  234. #ifdef DEBUG
  235.  
  236. /*
  237.  * Routine to print out costs with "-K" option.
  238.  */
  239. char    yysyms[] = ";,:=*+/-|&()[]<>~^";
  240.  
  241.  
  242. yycosts()
  243. {
  244.     register int c;
  245.     register char *cp;
  246.  
  247.     printf("Insert\tDelete\tRep(ID)\tSymbol\n");
  248.     for (cp = yysyms; *cp; cp++)
  249.         yydocost(*cp);
  250.     for (c = ERROR + 1; c < YLAST; c++)
  251.         yydocost(c);
  252. #ifdef PXP
  253.     flush();
  254. #endif
  255. }
  256.  
  257. yydocost(c)
  258.     int c;
  259. {
  260.  
  261.     printf("%4d\t", inscost(c, -1));
  262.     printf("%4d\t", delcost(c));
  263.     if (repcost(c, YID) != inscost(YID, c) + delcost(c))
  264.         printf("%4d", repcost(c, YID));
  265.     printf("\t%s\n", charname(c,1));
  266. }
  267. #endif
  268.