1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
|
/***************************************
$Header: /cvs/src/jbofihe/functions.c,v 1.10 2001/03/01 22:40:44 richard Exp $
General functions.
***************************************/
/**********************************************************************
* Copyright (C) Richard P. Curnow 1998-2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*********************************************************************/
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include "functions.h"
#include "nonterm.h"
#include "rpc_tab.h"
/* command line option */
extern int insert_elidables;
/*++++++++++++++++++++++++++++++
Go through children in a node and chain them onto that parent.
Also, set the start line/column of the nonterminal to that of the first child.
++++++++++++++++++++++++++++++*/
static void
mark_children(TreeNode *parent)
{
int i;
struct nonterm *nt;
TreeNode *c;
assert(parent->type == N_NONTERM);
nt = & parent->data.nonterm;
for (i=0; i<nt->nchildren; i++) {
c = nt->children[i];
c->parent = parent;
}
if (nt->nchildren > 0) {
parent->start_line = nt->children[0]->start_line;
parent->start_column = nt->children[0]->start_column;
} else {
parent->start_line = 0;
parent->start_column = 0;
}
}
/*++++++++++++++++++++++++++++++++++++++
Compress child array to remove NULLs. This situation should only arise if
one of the children is the result of matching an error token, and has no
proper contents (e.g. paragraph). Without this, the mark_children function
will blow up.
++++++++++++++++++++++++++++++++++++++*/
static void
compress_children(TreeNode *parent)
{
int i, j;
struct nonterm *nt;
assert(parent->type == N_NONTERM);
nt = & parent->data.nonterm;
j = 0;
for (i=0; i<nt->nchildren; i++) {
if (nt->children[j] = nt->children[i]) j++;
}
nt->nchildren = j;
return;
}
/*++++++++++++++++++++++++++++++++++++++
Return a new dynamically allocated token / node.
TreeNode * new_node Return the new node.
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
new_node(void)
{
TreeNode *result = new(TreeNode);
result->ext = NULL;
result->bahe = NULL;
result->ui_next = result->ui_prev = (TreeNode *) &result->ui_next;
result->eols = 0;
return result;
}
/*++++++++++++++++++++++++++++++
Create new non-terminal nodes
++++++++++++++++++++++++++++++*/
TreeNode *
new_elidable(int code, int selmao)
{
TreeNode *result;
if (insert_elidables) {
result = new_node();
result->type = N_CMAVO;
result->start_line = result->start_column = 0;
result->data.cmavo.code = code;
result->data.cmavo.selmao = selmao;
prop_elidable(result, YES);
} else {
result = NULL;
}
return result;
}
/*++++++++++++++++++++++++++++++
Create new non-terminal nodes
++++++++++++++++++++++++++++++*/
TreeNode *
new_node_0(NonTerm nt)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 0;
result->data.nonterm.children = NULL;
result->data.nonterm.brackets = BR_NONE;
/* only arises for empty TEXT anyway */
result->start_line = result->start_column = 0;
return result;
}
/*++++++++++++++++++++++++++++++++++++++
TreeNode * new_node_1
NonTerm nt
TreeNode *c1
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
new_node_1(NonTerm nt, TreeNode *c1)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 1;
result->data.nonterm.children = new_array(TreeNode *, 1);
result->data.nonterm.children[0] = c1;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
/*++++++++++++++++++++++++++++++++++++++
TreeNode * new_node_2
NonTerm nt
TreeNode *c1
TreeNode *c2
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
new_node_2(NonTerm nt, TreeNode *c1, TreeNode *c2)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 2;
result->data.nonterm.children = new_array(TreeNode *, 2);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
/*++++++++++++++++++++++++++++++++++++++
TreeNode * new_node_3
NonTerm nt
TreeNode *c1
TreeNode *c2
TreeNode *c3
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
new_node_3(NonTerm nt, TreeNode *c1, TreeNode *c2, TreeNode *c3)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 3;
result->data.nonterm.children = new_array(TreeNode *, 3);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
/*++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++*/
TreeNode *
new_node_4(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 4;
result->data.nonterm.children = new_array(TreeNode *, 4);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
/*++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++*/
TreeNode *
new_node_5(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 5;
result->data.nonterm.children = new_array(TreeNode *, 5);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_6(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 6;
result->data.nonterm.children = new_array(TreeNode *, 6);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_7(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6, TreeNode *c7)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 7;
result->data.nonterm.children = new_array(TreeNode *, 7);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
result->data.nonterm.children[6] = c7;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_8(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6, TreeNode *c7, TreeNode *c8)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 8;
result->data.nonterm.children = new_array(TreeNode *, 8);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
result->data.nonterm.children[6] = c7;
result->data.nonterm.children[7] = c8;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_9(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6, TreeNode *c7, TreeNode *c8,
TreeNode *c9)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 9;
result->data.nonterm.children = new_array(TreeNode *, 9);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
result->data.nonterm.children[6] = c7;
result->data.nonterm.children[7] = c8;
result->data.nonterm.children[8] = c9;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_10(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6, TreeNode *c7, TreeNode *c8,
TreeNode *c9, TreeNode *c10)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 10;
result->data.nonterm.children = new_array(TreeNode *, 10);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
result->data.nonterm.children[6] = c7;
result->data.nonterm.children[7] = c8;
result->data.nonterm.children[8] = c9;
result->data.nonterm.children[9] = c10;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_11(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6, TreeNode *c7, TreeNode *c8,
TreeNode *c9, TreeNode *c10, TreeNode *c11)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 11;
result->data.nonterm.children = new_array(TreeNode *, 11);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
result->data.nonterm.children[6] = c7;
result->data.nonterm.children[7] = c8;
result->data.nonterm.children[8] = c9;
result->data.nonterm.children[9] = c10;
result->data.nonterm.children[10] = c11;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
TreeNode *
new_node_12(NonTerm nt,
TreeNode *c1, TreeNode *c2, TreeNode *c3, TreeNode *c4,
TreeNode *c5, TreeNode *c6, TreeNode *c7, TreeNode *c8,
TreeNode *c9, TreeNode *c10, TreeNode *c11, TreeNode *c12)
{
TreeNode *result;
result = new_node();
result->type = N_NONTERM;
result->data.nonterm.type = nt;
result->data.nonterm.nchildren = 12;
result->data.nonterm.children = new_array(TreeNode *, 12);
result->data.nonterm.children[0] = c1;
result->data.nonterm.children[1] = c2;
result->data.nonterm.children[2] = c3;
result->data.nonterm.children[3] = c4;
result->data.nonterm.children[4] = c5;
result->data.nonterm.children[5] = c6;
result->data.nonterm.children[6] = c7;
result->data.nonterm.children[7] = c8;
result->data.nonterm.children[8] = c9;
result->data.nonterm.children[9] = c10;
result->data.nonterm.children[10] = c11;
result->data.nonterm.children[11] = c12;
compress_children(result);
mark_children(result);
result->data.nonterm.brackets = BR_NONE;
return result;
}
/*++++++++++++++++++++++++++++++
Check whether a node is non-terminal of a particular type
++++++++++++++++++++++++++++++*/
void
type_check(TreeNode *x, NonTerm type)
{
assert((x->type == N_NONTERM) &&
(x->data.nonterm.type == type));
}
/*++++++++++++++++++++++++++++++++++++++
Return number of children a nonterminal has
static int nch The number
TreeNode *x The nonterminal.
++++++++++++++++++++++++++++++++++++++*/
int
nch(TreeNode *x)
{
assert(x->type == N_NONTERM);
return x->data.nonterm.nchildren;
}
/*++++++++++++++++++++++++++++++++++++++
Return a reference to a particular child within a non-terminal,
checking that the index is in bounds.
static TreeNode * child_ref Returned reference
TreeNode *x The nonterminal.
int i The index (0..n-1)
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
child_ref(TreeNode *x, int i)
{
struct nonterm *nt;
assert(x->type == N_NONTERM);
assert(i>=0);
nt = & x->data.nonterm;
assert(i < nt->nchildren);
return nt->children[i];
}
/*++++++++++++++++++++++++++++++
Find the nth node (counting from 1) of a particular type amongst the
children of node x. Return null if there aren't any. Trap if x is
terminal.
++++++++++++++++++++++++++++++*/
TreeNode *
find_nth_child(TreeNode *x, int n, NonTerm type)
{
struct nonterm *nt;
TreeNode *c;
int i, nc, togo;
assert(x->type == N_NONTERM);
nt = & x->data.nonterm;
togo = n;
nc = nt->nchildren;
for (i=0; i<nc; i++) {
c = nt->children[i];
if ((c->type == N_NONTERM) &&
(c->data.nonterm.type == type)) {
--togo;
if (togo == 0) {
return c;
}
}
}
return NULL;
}
/*++++++++++++++++++++++++++++++
Find the nth node (counting from 1) of a particular type amongst the
children of node x. Return null if there aren't any. Trap if x is
terminal. If one of the immediate children is a nonterm of type
AUGMENTED, drill down into that. This allows the child we are
looking for to have attitudinals attached to it.
TreeNode * find_nth_cmavo_child The returned child, NULL if not found
TreeNode *x The parent child whose children are to be searched
int n The index of the child to find (starting from 1 for the first)
int selmao The selmao of the child being sought.
++++++++++++++++++++++++++++++*/
TreeNode *
find_nth_cmavo_child(TreeNode *x, int n, int selmao)
{
struct nonterm *nt, *ntc;
TreeNode *c, *cc;
int i, j, nc, ncc, togo;
assert(x->type == N_NONTERM);
nt = & x->data.nonterm;
togo = n;
nc = nt->nchildren;
for (i=0; i<nc; i++) {
c = nt->children[i];
if ((c->type == N_CMAVO) &&
(c->data.cmavo.selmao == selmao)) {
--togo;
if (togo == 0) {
return c;
}
} else if ((c->type == N_NONTERM) &&
(c->data.nonterm.type == AUGMENTED)) {
ntc = & c->data.nonterm;
ncc = ntc->nchildren;
for (j=0; j<ncc; j++) {
cc = ntc->children[j];
if ((cc->type == N_CMAVO) &&
(cc->data.cmavo.selmao == selmao)) {
--togo;
if (togo == 0) {
return cc;
}
}
}
}
}
return NULL;
}
/*++++++++++++++++++++++++++++++++++++++
Given a treenode, either it is a cmavo, or it's an AUGMENTED with a
non-attitudinal cmavo amongst the children. Find the child with the right
selmao, and trap if there isn't one.
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
strip_attitudinal(TreeNode *x)
{
if (x->type == N_CMAVO) {
return x;
} else if (x->type == N_NONTERM) {
TreeNode **child, **child0;
int N;
struct nonterm *nt;
nt = &(x->data.nonterm);
assert(nt->type == AUGMENTED);
N = nt->nchildren;
child = child0 = nt->children;
while (((*child)->type == N_CMAVO) && ((*child)->data.cmavo.selmao == BAhE)) {
child++;
assert ((child - child0) < N);
}
return *child;
}
}
/*++++++++++++++++++++++++++++++++++++++
If node is augmented, get main internal cmavo.
++++++++++++++++++++++++++++++++++++++*/
TreeNode *
maybe_strip_attitudinal(TreeNode *x)
{
if ((x->type == N_NONTERM) && (x->data.nonterm.type == AUGMENTED)) {
return strip_attitudinal(x);
} else {
return x;
}
}
/*++++++++++++++++++++++++++++++++++++++
Return 1 if all children are cmavo
int is_simple_nonterm
TreeNode *x
++++++++++++++++++++++++++++++++++++++*/
int
is_simple_nonterm(TreeNode *x)
{
int ok;
int nc;
int i;
struct nonterm *nt;
TreeNode *c;
assert(x->type == N_NONTERM);
nt = &x->data.nonterm;
nc = nt->nchildren;
ok = 1;
for (i=0; i<nc; i++) {
c = nt->children[i];
if (c->type == N_NONTERM) {
ok = 0;
break;
}
}
return ok;
}
/*++++++++++++++++++++++++++++++++++++++
Turn a string into uppercase. Can only deal with one string at a time.
++++++++++++++++++++++++++++++++++++++*/
char *
make_uppercase(char *s)
{
static char buffer[1024];
char *p;
strcpy(buffer, s);
p = buffer;
while (*p) *p = toupper(*p), p++;
return buffer;
}
|