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