00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "def.h"
00028
00029 extern char token_prval[];
00030 static int token_pos;
00031
00032
00033 extern char *trans;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 static char *
00059 token_get()
00060 {
00061 register char *pos = token_prval + token_pos;
00062 char *tpos = NULL;
00063
00064 while (C_WS == trans[*pos]) pos++;
00065
00066 if (C_PER == trans[*pos] && C_PER == trans[pos[1]]) return NULL;
00067 if (C_EOF == trans[*pos] || C_NL == trans[*pos]) return NULL;
00068 else if (C_DQ == trans[*pos]) {
00069 tpos = ++pos;
00070 while (C_DQ != trans[*pos] && C_NL != trans[*pos]) pos++;
00071 if (C_NL == trans[*pos]) error("unterminated string");
00072 *pos++ = '\0';
00073 }
00074 else {
00075 tpos = pos++;
00076 while (C_WS != trans[*pos] && C_NL != trans[*pos]) pos++;
00077 *pos++ = '\0';
00078 }
00079 token_pos = pos - token_prval;
00080 return tpos;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 #define DEFAULT_PREC 0
00128
00129
00130 extern OP *single_op, *double_op, *name_op, *type_op;
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 static OP *
00149 op_create(opn, arity)
00150 char *opn;
00151 short arity;
00152 {
00153 register int l;
00154 int class;
00155 OP *op;
00156 OP *op_new();
00157 void op_put();
00158
00159 class = trans[opn[0]];
00160 if (C_ALPH == class) {
00161 for (l = 1; C_EOF != (class = trans[opn[l]]); l++) {
00162 if (C_ALPH != class && C_NUM != class) {
00163 fprintf(stderr, "operator: %s\n", opn);
00164 error("invalid alphanumeric operator name");
00165 }
00166 }
00167 op = op_new(l);
00168 strcpy(op->pname, opn);
00169 op->arity = arity;
00170 op_put(&name_op, op);
00171 }
00172 else if (C_SPC == class) {
00173 class = trans[opn[1]];
00174 if (C_EOF == class) {
00175 op = op_new(1);
00176 strcpy(op->pname, opn);
00177 op->arity = arity;
00178 op_put(&single_op, op);
00179 }
00180 else if (C_SPC == class) {
00181 class = trans[opn[2]];
00182 if (C_EOF == class) {
00183 op = op_new(2);
00184 strcpy(op->pname, opn);
00185 op->arity = arity;
00186 op_put(&double_op, op);
00187 }
00188 else {
00189 fprintf(stderr, "operator: %s\n", opn);
00190 error("operator name contains more than two symbols");
00191 }
00192 }
00193 else {
00194 fprintf(stderr, "operator: %s\n", opn);
00195 error("invalid symbol in operator name");
00196 }
00197 }
00198 else {
00199 fprintf(stderr, "operator: %s\n", opn);
00200 error("invalid operator name, or misspelled keyword");
00201 }
00202 return op;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 static short
00217 prec_conv(s)
00218 char *s;
00219 {
00220 long prec = 0;
00221 int i = 0;
00222
00223 while (C_NUM == trans[s[i]]) prec = 10 * prec + s[i++] - '0';
00224
00225 if (C_EOF != trans[s[i]]) {
00226 fprintf(stderr,"field: %s\n", s);
00227 error("invalid precedence field");
00228 }
00229
00230 if (prec > BIG_SHORT) {
00231 printf("Precedence = %d; Maximum allowable = %ld\n", prec, BIG_SHORT);
00232 error("Precedence of operator exceeds maximum allowable");
00233 }
00234
00235 return (short) prec;
00236 }
00237
00238
00239
00240
00241
00242
00243 static void
00244 op_define()
00245 {
00246 register char *tok;
00247 char *op_name = NULL;
00248 char *other_op_name = NULL;
00249 char *supertype = NULL;
00250 char *prfun = NULL;
00251 short arity = -1;
00252 short kind = -1;
00253 short precedence = -1;
00254 OP *op;
00255 char *arity_name();
00256
00257 for ( ; NULL != (tok = token_get()); ) {
00258 if (tok[0] == '#') {
00259 if (prfun) error("multiple special reduce functions not allowed");
00260 prfun = tok;
00261 }
00262 else if (C_NUM == trans[tok[0]]) {
00263 precedence = prec_conv(tok);
00264 }
00265 else if (C_SQ == trans[tok[0]]) {
00266 if (supertype) {
00267 fprintf(stderr, "first supertype: %s, second supertype: %s\n",
00268 supertype, tok);
00269 error("multiple supertypes not allowed");
00270 }
00271 supertype = tok;
00272 }
00273 else if (0==strcmp(tok, "left")) {
00274 if (arity == -1 || arity == BINARY) arity = LEFT;
00275 else {
00276 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00277 tok, arity_name(arity));
00278 error("bad operator specification");
00279 }
00280 }
00281 else if (0==strcmp(tok, "right")) {
00282 if (arity == -1 || arity == BINARY) arity = RIGHT;
00283 else {
00284 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00285 tok, arity_name(arity));
00286 error("bad operator specification");
00287 }
00288 }
00289 else if (0==strcmp(tok, "prefix")) {
00290 if (arity == -1 || arity == UNARY) arity = PREFIX;
00291 else {
00292 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00293 tok, arity_name(arity));
00294 error("bad operator specification");
00295 }
00296 }
00297 else if (0==strcmp(tok, "postfix")) {
00298 if (arity == -1 || arity == UNARY) arity = POSTFIX;
00299 else {
00300 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00301 tok, arity_name(arity));
00302 error("bad operator specification");
00303 }
00304 }
00305 else if (0==strcmp(tok, "nullary")) {
00306 if (arity == -1) arity = NULLARY;
00307 else {
00308 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00309 tok, arity_name(arity));
00310 error("bad operator specification");
00311 }
00312 }
00313 else if (0==strcmp(tok, "nonassoc") ||
00314 0==strcmp(tok, "nonassociative") ||
00315 0==strcmp(tok, "non")) {
00316 if (arity == -1 || arity == BINARY) arity = NONASSOC;
00317 else {
00318 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00319 tok, arity_name(arity));
00320 error("bad operator specification");
00321 }
00322 }
00323 else if (0==strcmp(tok, "outfix") || 0==strcmp(tok, "matchfix")) {
00324 if (arity == -1 || arity == UNARY) arity = OUTFIX1;
00325 else {
00326 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00327 tok, arity_name(arity));
00328 error("bad operator specification");
00329 }
00330 }
00331 else if (0==strcmp(tok, "unary")) {
00332 if (arity == -1) arity = UNARY;
00333 else if (!(arity & UNARY)) {
00334 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00335 tok, arity_name(arity));
00336 error("bad operator specification");
00337 }
00338 }
00339 else if (0==strcmp(tok, "infix") || 0==strcmp(tok, "binary")) {
00340 if (arity == -1) arity = BINARY;
00341 else if (!(arity & BINARY)) {
00342 fprintf(stderr, "keyword %s applied to op of arity %s\n",
00343 tok, arity_name(arity));
00344 error("bad operator specification");
00345 }
00346 }
00347 else if (0==strcmp(tok, "associative")) ;
00348 else if (0==strcmp(tok, "precedence")) ;
00349 else if (0==strcmp(tok, "supertype")) ;
00350 else {
00351 if (!op_name) op_name = tok;
00352 else if (!other_op_name) other_op_name = tok;
00353 else {
00354 fprintf(stderr, "operators: %s, %s, %s\n",
00355 op_name, other_op_name, tok);
00356 error("too many operator names, or invalid keyword");
00357 }
00358 }
00359 }
00360
00361 if (arity == -1) {
00362 if (!other_op_name) arity = NULLARY;
00363 else arity = OUTFIX1;
00364 }
00365 else if (arity == UNARY) arity = PREFIX;
00366 else if (arity == BINARY) arity = NONASSOC;
00367
00368 if (precedence != -1 && arity == NULLARY) {
00369 fprintf(stderr, "operator: %s, precedence: %d\n", op_name, precedence);
00370 error("a nullary operator may not have a precedence");
00371 }
00372 if (precedence != -1 && arity == OUTFIX1) {
00373 fprintf(stderr, "operators: %s %s, precedence: %d\n",
00374 op_name, other_op_name, precedence);
00375 error("an outfix operator may not have a precedence");
00376 }
00377
00378 if (op_name) op = op_create(op_name, arity);
00379 else {
00380 error("no operator name specified in operator definition");
00381 }
00382 if (arity == OUTFIX1) {
00383 if (other_op_name) {
00384 op->other = op_create(other_op_name, OUTFIX2);
00385 op->other->other = op;
00386 }
00387 else {
00388 fprintf(stderr, "first operator: %s\n", op_name);
00389 error("outfix operator requires two operator names");
00390 }
00391 }
00392 else if (other_op_name) {
00393 fprintf(stderr, "operator: %s, second operator: %s\n",
00394 op_name, other_op_name);
00395 error("multiple operator names defined, or invalid keyword");
00396 }
00397 if (arity == NULLARY) op->precedence = BIG_SHORT;
00398 else if (arity == OUTFIX1) op->precedence = 0;
00399 else if (precedence == -1) op->precedence = DEFAULT_PREC;
00400 else op->precedence = precedence;
00401 if (supertype) {
00402 OP *sop;
00403 for (sop = type_op; sop; sop = sop->next) {
00404 if (0==strcmp(sop->pname, supertype+1)) break;
00405 }
00406 if (sop) op->super = sop;
00407 else {
00408 fprintf(stderr,"type: %s\n", supertype);
00409 error("supertype is invalid type");
00410 }
00411 }
00412 if (prfun) {
00413 int snum;
00414 snum = atoi(prfun+1);
00415 if (snum == 0) error("invalid parser reduce function");
00416 else op->eval = -snum;
00417 }
00418 #ifdef DEBUG
00419 printf("operator %s defined, arity = %s, precedence = %d\n",
00420 op->pname, arity_name(op->arity), op->precedence);
00421 if (op->arity == OUTFIX1)
00422 printf("other operator %s defined, arity = %s, precedence = %d\n",
00423 op->other->pname, arity_name(op->other->arity), op->other->precedence);
00424 #endif
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 static void
00442 type_define()
00443 {
00444 register OP *ty;
00445 register char *tok;
00446 int len;
00447 OP *sop;
00448 OP *op_new();
00449
00450 tok = token_get();
00451 if (tok[0] != '\'') {
00452 fprintf(stderr,"type: %s\n", tok);
00453 error("type must begin with a single quote");
00454 }
00455 if (tok[1] == '\0') error("cannot define null type");
00456 len = strlen(tok);
00457 ty = op_new(len-1);
00458 strcpy(ty->pname, tok+1);
00459 ty->arity = OP_NAME;
00460 ty->precedence = 0;
00461 ty->other = (OP *) NULL;
00462 ty->super = (OP *) NULL;
00463 op_put(&type_op, ty);
00464
00465 tok = token_get();
00466 if (tok) {
00467 if (0==strcmp(tok, "supertype")) {
00468 tok = token_get();
00469 if (!tok) {
00470 error("no supertype following supertype keyword");
00471 }
00472 }
00473 if (tok[0] != '\'') {
00474 fprintf(stderr,"supertype: %s\n", tok);
00475 error("supertype must begin with a single quote");
00476 }
00477 for (sop = type_op; sop; sop = sop->next) {
00478 if (0==strcmp(sop->pname, tok+1)) break;
00479 }
00480 if (sop) ty->super = sop;
00481 else {
00482 fprintf(stderr,"type: %s\n", tok);
00483 error("supertype is invalid type");
00484 }
00485 }
00486 tok = token_get();
00487 if (tok) error("invalid type definition");
00488 #ifdef DEBUG
00489 printf("type '%s defined, arity = %s", ty->pname, arity_name(ty->arity));
00490 if (ty->super) printf(", supertype = '%s\n", ty->super->pname);
00491 else printf("\n");
00492 #endif
00493 }
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 static void
00513 primitive_define()
00514 {
00515 register OP *prim;
00516 register char *tok;
00517 register OP *sop;
00518
00519 tok = token_get();
00520 if (tok[0] == '\'') {
00521 for (prim = type_op; prim; prim = prim->next) {
00522 if (0==strcmp(prim->pname, tok+1)) break;
00523 }
00524 if (!prim) {
00525 fprintf(stderr, "primitive type: %s\n", tok);
00526 error("primitive not found");
00527 }
00528 }
00529 else if (C_ALPH == trans[tok[0]]) {
00530 for (prim = name_op; prim; prim = prim->next) {
00531 if (0==strcmp(prim->pname, tok)) break;
00532 }
00533 if (!prim) {
00534 fprintf(stderr, "alphanumeric primitive: %s\n", tok);
00535 error("primitive not found");
00536 }
00537 }
00538 else if (tok[1] == '\0') {
00539 for (prim = single_op; prim; prim = prim->next) {
00540 if (0==strcmp(prim->pname, tok)) break;
00541 }
00542 if (!prim) {
00543 fprintf(stderr, "special character primitive: %s\n", tok);
00544 error("primitive not found");
00545 }
00546 }
00547 else if (tok[2] == '\0') {
00548 for (prim = double_op; prim; prim = prim->next) {
00549 if (0==strcmp(prim->pname, tok)) break;
00550 }
00551 if (!prim) {
00552 fprintf(stderr, "double special character primitive: %s\n", tok);
00553 error("primitive not found");
00554 }
00555 }
00556 else {
00557 fprintf(stderr, "unknown primitive: %s\n", tok);
00558 error("primitive not found");
00559 }
00560 if (prim->super) {
00561 fprintf(stderr, "primitive: %s, supertype: %s\n", tok, prim->super->pname);
00562 error("primitive already has a supertype");
00563 }
00564
00565
00566 tok = token_get();
00567 if (tok) {
00568 if (0==strcmp(tok, "supertype")) {
00569 tok = token_get();
00570 if (!tok) {
00571 error("no supertype following supertype keyword");
00572 }
00573 }
00574 if (tok[0] != '\'') {
00575 fprintf(stderr,"supertype: %s\n", tok);
00576 error("supertype must begin with a single quote");
00577 }
00578 for (sop = type_op; sop; sop = sop->next) {
00579 if (0==strcmp(sop->pname, tok+1)) break;
00580 }
00581 if (sop) prim->super = sop;
00582 else {
00583 fprintf(stderr,"type: %s, supertype: %s\n", tok, prim->super->pname);
00584 error("supertype is invalid type");
00585 }
00586 }
00587 else {
00588 fprintf(stderr, "primitive: %s\n", tok);
00589 error("no supertype specified for primitive");
00590 }
00591 tok = token_get();
00592 if (tok) error("invalid primitive definition");
00593 #ifdef DEBUG
00594 printf("primitive %s defined, arity = %s", ty->pname, arity_name(ty->arity));
00595 if (ty->super) printf(", supertype = '%s\n", ty->super->pname);
00596 else printf("\n");
00597 #endif
00598 }
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622 void
00623 file_push()
00624 {
00625 extern int filespushed;
00626 extern FILE *infiles[];
00627 extern FILE *infile;
00628 extern char *infilenames[];
00629 extern char *infilename;
00630 extern int inlinenos[];
00631 extern int verboses[];
00632 extern int lineno;
00633 char *tok;
00634 char *char_copy();
00635 extern char *libdir;
00636
00637 tok = token_get();
00638 if (!tok) error("no include file name specified");
00639 infiles[filespushed] = infile;
00640 infilenames[filespushed] = infilename;
00641 inlinenos[filespushed] = lineno;
00642 verboses[filespushed] = verbose;
00643 infile = fopen(tok, "r");
00644 if (NULL == infile) {
00645 char fbuf[256];
00646 strcpy(fbuf, libdir);
00647 strcat(fbuf, tok);
00648 infile = fopen(fbuf, "r");
00649 if (NULL == infile) {
00650 fprintf(stderr, "include file: %s\n", tok);
00651 error("file not found");
00652 }
00653 }
00654 infilename = char_copy(tok);
00655 verbose = FALSE;
00656 lineno = 1;
00657 filespushed++;
00658 #ifdef DEBUG
00659 printf("now reading from file %s\n", infilename);
00660 #endif
00661 }
00662
00663
00664 void
00665 load_file()
00666 {
00667 error("#load not implemented");
00668 }
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 void
00682 preprocess()
00683 {
00684 char *tok;
00685 extern int lineno;
00686
00687 token_pos = 0;
00688 tok = token_get();
00689 if (!tok) return;
00690 if (0 == strcmp(tok, "op") || 0 == strcmp(tok, "operator")) op_define();
00691 else if (0 == strcmp(tok, "type")) type_define();
00692 else if (0 == strcmp(tok, "primitive")) primitive_define();
00693 else if (0 == strcmp(tok, "include")) file_push();
00694 else if (0 == strcmp(tok, "load")) load_file();
00695 else if (0 == strcmp(tok, "line")) {
00696 tok = token_get();
00697 if (tok) {
00698 if (C_NUM == trans[tok[0]]) lineno = atoi(tok);
00699 }
00700 }
00701 else if (0 == strcmp(tok, "trace")) {
00702 tok = token_get();
00703 if (tok && (C_NUM == trans[tok[0]])) verbose = atoi(tok);
00704 else verbose = 1;
00705 }
00706 else if (0 == strcmp(tok, "quiet")) verbose = 0;
00707 else {
00708 fprintf(stderr, "preprocessor statement keyword: #%s\n", tok);
00709 error("invalid preprocessor statement");
00710 }
00711 }