/* Copyright (c) 1979 Regents of the University of California */
static char sccsid
[] = "@(#)yycosts.c 1.4 8/30/82";
* Symbol costs for Pascal.
* Cost strategy of August 14, 1977.
* The costs determined by the routines in this file are used by
* the recovery in choosing appropriate corrections.
* The cost vectors and the error productions in the grammar
* work together to define the corrective capacity of the grammar.
* The costs here largely derive from those given in Steve Rhode's
* thesis for the Pascal-I error correcting parser which he implemented.
* Some minor changes have been made to adjust for the fact that
* the current error recovery is not as "smart", both because of the
* limited forward move and because of the lack of any type information
* These adjustments largely take the form of increased costs for certain
* tokens, noticeably keywords which are major brackets such as "begin"
* "label", "procedure", etc.
* The overall weighting strategy is still similar to Rhodes' strategy.
* The costs can be considered:
* In addition to the normal symbol insertion costs,
* there are zero cost insertions here.
* The current error recovery system treats symbols
* which have zero insertion cost in a special way,
* inserting them but suppressing diagnostics.
* This allows the system to hold of on bracketing
* error diagnostics about missing end's until the
* reduction occurs which knows the line number of the
* corresponding "begin", "repeat", etc.
* A more intelligent and useful diagnostic can then
* Although this routine never allows the insertion
* of the keyword begin, it can be inserted after a
* procedure or function body starts, if it was omitted
* by a special case in the panic routine, which notices
* the keywords in the statement body of the procedure
* and inserts the begin to recover.
* Similarly, we do not insert end-of-file, but
* the fact that end-of-file is the unique input
* is noticed by the recovery routines as a special
* case and handled there.
if (sy
== YUNTIL
|| sy
== YEND
)
* Most replacement costs are the same as an insertion
* plus a deletion cost. One special case is the replacement
* of a large number of keywords by an identifier.
* These are given lower costs, especially the keyword "to".
if (with
== YID
&& what
> ERROR
)
if (what
== YID
&& with
== YFORWARD
)
if (what
== ';' && (with
== ',' || with
== '.'))
c
= delcost(what
) + inscost(with
);
* It costs extra to replace something which has
* semantics by something which doesn't.
if (nullsem(what
) == NIL
&& nullsem(with
) != NIL
)
return ((CLIMIT
* 3) / 4);
* Routine to print out costs with "-K" option.
char yysyms
[] = ";,:=*+/-|&()[]<>~^";
printf("Insert\tDelete\tRep(ID)\tSymbol\n");
for (cp
= yysyms
; *cp
; cp
++)
for (c
= ERROR
+ 1; c
< YLAST
; c
++)
printf("%4d\t", inscost(c
, -1));
printf("%4d\t", delcost(c
));
if (repcost(c
, YID
) != inscost(YID
) + delcost(c
))
printf("%4d", repcost(c
, YID
));
printf("\t%s%s\n", charname(c
));