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
|
/* This is the parser for the dlg
* This is a part of the Purdue Compiler Construction Tool Set
*
* SOFTWARE RIGHTS
*
* We reserve no LEGAL rights to the Purdue Compiler Construction Tool
* Set (PCCTS) -- PCCTS is in the public domain. An individual or
* company may do whatever they wish with source code distributed with
* PCCTS or the code generated by PCCTS, including the incorporation of
* PCCTS, or its output, into commerical software.
*
* We encourage users to develop software with PCCTS. However, we do ask
* that credit is given to us for developing PCCTS. By "credit",
* we mean that if you incorporate our source code into one of your
* programs (commercial product, research project, or otherwise) that you
* acknowledge this fact somewhere in the documentation, research report,
* etc... If you like PCCTS and have developed a nice tool with the
* output, please mention that you developed it using PCCTS. In
* addition, we ask that this header remain intact in our source code.
* As long as these guidelines are kept, we expect to continue enhancing
* this system and expect to make other tools available as they are
* completed.
*
* DLG 1.33
* Will Cohen
* With mods by Terence Parr; AHPCRC, University of Minnesota
* 1989-1995
*/
#header <<
#include <ctype.h>
#include "dlg.h"
>>
<<
/* MR20 G. Hobbelt
Fix for Borland C++ 4.x & 5.x compiling with ALL warnings enabled
*/
#ifdef __TURBOC__
#pragma warn -aus /* unused assignment of 'xxx' */
#endif
#pragma clang diagnostic ignored "-Wparentheses-equality"
int action_no = 0; /* keep track of actions outputted */
int nfa_allocated = 0; /* keeps track of number of nfa nodes */
nfa_node **nfa_array = NULL;/* root of binary tree that stores nfa array */
nfa_node nfa_model_node; /* model to initialize new nodes */
set used_chars; /* used to label trans. arcs */
set used_classes; /* classes or chars used to label trans. arcs */
set normal_chars; /* mask to get rid elements that aren't used
in set */
int flag_paren = FALSE;
int flag_brace = FALSE;
int mode_counter = 0; /* keep track of number of %%names */
>>
#lexaction <<
int func_action; /* should actions be turned into functions?*/
int lex_mode_counter = 0; /* keeps track of the number of %%names */
/* MR1 */
/* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
/* MR1 via <<%%lexmember...>> */
/* MR1 */
int lexMember = 0; /* <<%%lexmemeber ...>> MR1 */
int lexAction = 0; /* <<%%lexaction ...>> MR1 */
int parserClass = 0; /* <<%%parserclass ...>> MR1 */
int lexPrefix = 0; /* <<%%lexprefix ...>> MR1 */
char theClassName[100]; /* MR11 */
char *pClassName=theClassName; /* MR11 */
int firstLexMember=1; /* MR1 */
#ifdef __USE_PROTOS
void xxputc(int c) { /* MR1 */
#else
void xxputc(c) /* MR1 */
int c; /* MR1 */
{ /* MR1 */
#endif
if (parserClass) { /* MR1 */
*pClassName++=c; /* MR1 */
*pClassName=0; /* MR1 */
} else if (lexMember || lexPrefix) { /* MR1 */
if (class_stream != NULL) fputc(c,class_stream); /* MR1 */
} else { /* MR1 */
fputc(c,OUT); /* MR1 */
}; /* MR1 */
} /* MR1 */
#ifdef __USE_PROTOS
void xxprintf(char *format,char *string) { /* MR1 */
#else
void xxprintf(format,string) /* MR1 */
char *format; /* MR1 */
char *string; /* MR1 */
{ /* MR1 */
#endif
if (lexMember || lexPrefix || parserClass) { /* MR1 */
if (class_stream != NULL) /* MR1 */
fprintf(class_stream,format,string); /* MR1 */
} else { /* MR1 */
fprintf(OUT,format,string); /* MR1 */
}; /* MR1 */
} /* MR1 */
>>
#token "[\r\t\ ]+" << zzskip(); >> /* Ignore white */
#token "\n" << zzline++; zzskip(); DAWDLE; >> /* Track Line # */
#token L_EOF "\@"
#token PER_PER "\%\%"
#token NAME_PER_PER "\%\%[a-zA-Z_][a-zA-Z0-9_]*"
<< p_mode_def(&zzlextext[2],lex_mode_counter++); >>
#token LEXMEMBER "\<\<\%\%lexmember" /* MR1 */
<<lexMember=1; /* MR1 */
if (firstLexMember != 0) { /* MR1 */
firstLexMember=0; /* MR1 */
p_class_def1(); /* MR1 */
}; /* MR1 */
zzmode(ACT); /* MR1 */
>> /* MR1 */
#token LEXACTION "\<\<\%\%lexaction" /* MR1 */
<<lexAction=1;zzmode(ACT);>> /* MR1 */
#token PARSERCLASS "\<\<\%\%parserclass" /* MR1 */
<<parserClass=1; /* MR1 */
zzmode(ACT); /* MR1 */
>> /* MR1 */
#token LEXPREFIX "\<\<\%\%lexprefix" /* MR1 */
<<lexPrefix=1;zzmode(ACT);>> /* MR1 */
#token ACTION "\<\<"
<< if (func_action)
fprintf(OUT,"\n%s %sact%d()\n{ ",
gen_cpp?"ANTLRTokenType":"static void",
gen_cpp?ClassName("::"):"", ++action_no);
zzmode(ACT); zzskip();
>>
#token GREAT_GREAT "\>\>"
#token L_BRACE "\{"
#token R_BRACE "\}"
#token L_PAR "\("
#token R_PAR "\)"
#token L_BRACK "\["
#token R_BRACK "\]"
#token ZERO_MORE "\*"
#token ONE_MORE "\+"
#token OR "\|"
#token RANGE "\-"
#token NOT "\~"
#token OCTAL_VALUE "\\0[0-7]*"
<< {int t; sscanf(&zzlextext[1],"%o",&t); zzlextext[0] = t;}>>
#token HEX_VALUE "\\0[Xx][0-9a-fA-F]+"
<< {int t; sscanf(&zzlextext[3],"%x",&t); zzlextext[0] = t;}>>
#token DEC_VALUE "\\[1-9][0-9]*"
<< {int t; sscanf(&zzlextext[1],"%d",&t); zzlextext[0] = t;}>>
#token TAB "\\t" << zzlextext[0] = '\t';>>
#token NL "\\n" << zzlextext[0] = '\n';>>
#token CR "\\r" << zzlextext[0] = '\r';>>
#token BS "\\b" << zzlextext[0] = '\b';>>
/* MR1 */
/* MR1 10-Apr-97 MR1 Allow #token regular expressions to cross lines */
/* MR1 */
#token CONTINUATION "\\ \n" << zzline++; zzskip();>> /* MR1 */
/* NOTE: this takes ANYTHING after the \ */
#token LIT "\\~[tnrb]" << zzlextext[0] = zzlextext[1];>>
/* NOTE: this takes ANYTHING that doesn't match the other tokens */
#token REGCHAR "~[\\]"
grammar : << p_head(); p_class_hdr(); func_action = FALSE;>>
( {LEXACTION | LEXMEMBER | LEXPREFIX | PARSERCLASS } ACTION)* /* MR1 */
<<if ( gen_cpp ) p_includes();>>
start_states
<< func_action = FALSE; p_tables(); p_tail(); >>
(ACTION)* "@"
<< if (firstLexMember != 0) p_class_def1(); >> /* MR1 */
;
start_states : ( PER_PER do_conversion
| NAME_PER_PER do_conversion (NAME_PER_PER do_conversion)*)
PER_PER
;
do_conversion : <<new_automaton_mode(); func_action = TRUE;>>
rule_list
<<
dfa_class_nop[mode_counter] =
relabel($1.l,comp_level);
if (comp_level)
p_shift_table(mode_counter);
dfa_basep[mode_counter] = dfa_allocated+1;
make_dfa_model_node(dfa_class_nop[mode_counter]);
nfa_to_dfa($1.l);
++mode_counter;
func_action = FALSE;
#ifdef HASH_STAT
fprint_hash_stats(stderr);
#endif
>>
;
rule_list : rule <<$$.l=$1.l; $$.r=$1.r;>>
(rule
<<{nfa_node *t1;
t1 = new_nfa_node();
(t1)->trans[0]=$$.l;
(t1)->trans[1]=$1.l;
/* all accept nodes "dead ends" */
$$.l=t1; $$.r=NULL;
}
>>
)*
| /* empty */
<<$$.l = new_nfa_node(); $$.r = NULL;
warning("no regular expressions", zzline);
>>
;
rule : reg_expr ACTION
/* MR23 */ << if ($1.r != NULL) {
$$.l=$1.l; $$.r=$1.r; ($1.r)->accept=action_no;
}
>>
| ACTION
<<$$.l = NULL; $$.r = NULL;
error("no expression for action ", zzline);
>>
;
reg_expr : and_expr <<$$.l=$1.l; $$.r=$1.r;>>
(OR and_expr
<<{nfa_node *t1, *t2;
t1 = new_nfa_node(); t2 = new_nfa_node();
(t1)->trans[0]=$$.l;
(t1)->trans[1]=$2.l;
/* MR23 */ if ($$.r != NULL) ($$.r)->trans[1]=t2;
if ($2.r) {
($2.r)->trans[1]=t2; /* MR20 */
}
$$.l=t1; $$.r=t2;
}
>>
)*
;
and_expr : repeat_expr
<<
$$.l=$1.l; $$.r=$1.r;
>>
(repeat_expr
/* MR23 */ << if ($$.r != NULL) {
($$.r)->trans[1]=$1.l;
$$.r=$1.r;
}
>>
)*
;
repeat_expr : expr <<$$.l=$1.l; $$.r=$1.r;>>
{ ZERO_MORE
<<{ nfa_node *t1,*t2;
/* MR23 */ if ($$.r != NULL) ($$.r)->trans[0] = $$.l;
t1 = new_nfa_node(); t2 = new_nfa_node();
t1->trans[0]=$$.l;
t1->trans[1]=t2;
/* MR23 */ if ($$.r != NULL) ($$.r)->trans[1]=t2;
$$.l=t1;$$.r=t2;
}
>>
| ONE_MORE
/* MR23 */ <<if ($$.r != NULL) ($$.r)->trans[0] = $$.l;>>
}
| ZERO_MORE
<< error("no expression for *", zzline);>>
| ONE_MORE
<< error("no expression for +", zzline);>>
;
expr : << $$.l = new_nfa_node();
$$.r = new_nfa_node();
>>
L_BRACK atom_list R_BRACK
<<
/* MR23 */ if ($$.l != NULL) {
($$.l)->trans[0] = $$.r;
($$.l)->label = set_dup($2.label);
set_orin(&used_chars,($$.l)->label);
}
>>
| NOT L_BRACK atom_list R_BRACK
<<
/* MR23 */ if ($$.l != NULL) {
($$.l)->trans[0] = $$.r;
($$.l)->label = set_dif(normal_chars,$3.label);
set_orin(&used_chars,($$.l)->label);
}
>>
| L_PAR reg_expr R_PAR
<<
/* MR23 */ if ($$.l != NULL) {
($$.l)->trans[0] = $2.l;
if ($2.r) {
($2.r)->trans[1] = $$.r; /* MR20 */
}
}
>>
| L_BRACE reg_expr R_BRACE
<<
/* MR23 */ if ($$.l != NULL) {
($$.l)->trans[0] = $2.l;
($$.l)->trans[1] = $$.r;
if ($2.r) {
($2.r)->trans[1] = $$.r; /* MR20 */
}
}
>>
| atom
<<
/* MR23 */ if ($$.l != NULL) {
($$.l)->trans[0] = $$.r;
($$.l)->label = set_dup($1.label);
set_orin(&used_chars,($$.l)->label);
}
>>
;
atom_list : << set_free($$.label); >>
(near_atom <<set_orin(&($$.label),$1.label);>>)*
;
near_atom : << register int i;
register int i_prime;
>>
anychar
<<$$.letter=$1.letter; $$.label=set_of($1.letter);
i_prime = $1.letter + MIN_CHAR;
if (case_insensitive && islower(i_prime))
set_orel(toupper(i_prime)-MIN_CHAR,
&($$.label));
if (case_insensitive && isupper(i_prime))
set_orel(tolower(i_prime)-MIN_CHAR,
&($$.label));
>>
{ RANGE anychar
<< if (case_insensitive){
i_prime = $$.letter+MIN_CHAR;
$$.letter = (islower(i_prime) ?
toupper(i_prime) : i_prime)-MIN_CHAR;
i_prime = $2.letter+MIN_CHAR;
$2.letter = (islower(i_prime) ?
toupper(i_prime) : i_prime)-MIN_CHAR;
}
/* check to see if range okay */
{
int debugLetter1 = $$.letter;
int debugLetter2 = $2.letter;
}
if ($$.letter > $2.letter
&& $2.letter != 0xff){ /* MR16 */
error("invalid range ", zzline);
}
for (i=$$.letter; i<= (int)$2.letter; ++i){
set_orel(i,&($$.label));
i_prime = i+MIN_CHAR;
if (case_insensitive && islower(i_prime))
set_orel(toupper(i_prime)-MIN_CHAR,
&($$.label));
if (case_insensitive && isupper(i_prime))
set_orel(tolower(i_prime)-MIN_CHAR,
&($$.label));
}
>>
}
;
atom : << register int i_prime;>>
anychar
<<$$.label = set_of($1.letter);
i_prime = $1.letter + MIN_CHAR;
if (case_insensitive && islower(i_prime))
set_orel(toupper(i_prime)-MIN_CHAR,
&($$.label));
if (case_insensitive && isupper(i_prime))
set_orel(tolower(i_prime)-MIN_CHAR,
&($$.label));
>>
;
anychar : REGCHAR <<$$.letter = $1.letter - MIN_CHAR;>>
| OCTAL_VALUE <<$$.letter = $1.letter - MIN_CHAR;>>
| HEX_VALUE <<$$.letter = $1.letter - MIN_CHAR;>>
| DEC_VALUE <<$$.letter = $1.letter - MIN_CHAR;>>
| TAB <<$$.letter = $1.letter - MIN_CHAR;>>
| NL <<$$.letter = $1.letter - MIN_CHAR;>>
| CR <<$$.letter = $1.letter - MIN_CHAR;>>
| BS <<$$.letter = $1.letter - MIN_CHAR;>>
| LIT <<$$.letter = $1.letter - MIN_CHAR;>>
/* NOTE: LEX_EOF is ALWAYS shifted to 0 = MIN_CHAR - MIN_CHAR*/
| L_EOF <<$$.letter = 0;>>
;
<</* empty action */>>
#lexclass ACT
#token "@" << error("unterminated action", zzline); zzmode(START); >>
#token ACTION "\>\>"
<< if (func_action) fprintf(OUT,"}\n\n");
zzmode(START);
/* MR1 */
/* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
/* MR1 via <<%%lexmember ...>> */
/* MR1 This is a consequence of not saving actions */
/* MR1 */
/* MR1 */ parserClass=0;
/* MR1 */ lexPrefix=0;
/* MR1 */ lexAction=0;
/* MR1 */ lexMember=0;
>>
#token "\>" << xxputc(zzlextext[0]); zzskip(); >> /* MR1 */
#token "\\\>" << xxputc('>'); zzskip(); >> /* MR1 */
#token "\\" << xxputc('\\'); zzskip(); >> /* MR1 */
#token "\n" << xxputc(zzlextext[0]); ++zzline; zzskip(); >> /* MR1 */
#token "/\*" << zzmode(ACTION_COMMENTS); /* MR1 */
xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
>> /* MR1 */
#token "//" << zzmode(ACTION_CPP_COMMENTS); /* MR1 */
xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
>> /* MR1 */
#token "~[]" << xxputc(zzlextext[0]); zzskip(); >> /* MR1 */
/* MR1 */
#lexclass ACTION_COMMENTS /* MR1 */
#token "\*/" << zzmode(ACT); /* MR1 */
xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
>> /* MR1 */
#token "[\n\r]" << zzline++; xxputc(zzlextext[0]); zzskip();>> /* MR1 */
#token "~[]" << xxputc(zzlextext[0]); zzskip();>> /* MR1 */
/* MR1 */
#lexclass ACTION_CPP_COMMENTS /* MR1 */
#token "[\n\r]" << zzmode(ACT); zzline++; /* MR1 */
xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
>> /* MR1 */
#token "~[]" << xxputc(zzlextext[0]); zzskip();>> /* MR1 */
<<
/* adds a new nfa to the binary tree and returns a pointer to it */
nfa_node *
#ifdef __USE_PROTOS
new_nfa_node(void)
#else
new_nfa_node()
#endif
{
register nfa_node *t;
static int nfa_size=0; /* elements nfa_array[] can hold */
++nfa_allocated;
if (nfa_size<=nfa_allocated){
/* need to redo array */
if (!nfa_array){
/* need some to do initial allocation */
nfa_size=nfa_allocated+NFA_MIN;
nfa_array=(nfa_node **) malloc(sizeof(nfa_node*)*
nfa_size);
}else{
/* need more space */
nfa_size=2*(nfa_allocated+1);
nfa_array=(nfa_node **) realloc(nfa_array,
sizeof(nfa_node*)*nfa_size);
}
}
/* fill out entry in array */
t = (nfa_node*) malloc(sizeof(nfa_node));
nfa_array[nfa_allocated] = t;
*t = nfa_model_node;
t->node_no = nfa_allocated;
return t;
}
/* initialize the model node used to fill in newly made nfa_nodes */
void
#ifdef __USE_PROTOS
make_nfa_model_node(void)
#else
make_nfa_model_node()
#endif
{
nfa_model_node.node_no = -1; /* impossible value for real nfa node */
nfa_model_node.nfa_set = 0;
nfa_model_node.accept = 0; /* error state default*/
nfa_model_node.trans[0] = NULL;
nfa_model_node.trans[1] = NULL;
nfa_model_node.label = empty;
}
>>
<<
#if defined(DEBUG) || defined(_DEBUG)
/* print out the pointer value and the node_number */
void
#ifdef __USE_PROTOS
fprint_dfa_pair(FILE *f, nfa_node *p)
#else
fprint_dfa_pair(f, p)
FILE *f;
nfa_node *p;
#endif
{
if (p){
fprintf(f, "%x (%d)", p, p->node_no);
}else{
fprintf(f, "(nil)");
}
}
/* print out interest information on a set */
void
#ifdef __USE_PROTOS
fprint_set(FILE *f, set s)
#else
fprint_set(f,s)
FILE *f;
set s;
#endif
{
unsigned int *x;
fprintf(f, "n = %d,", s.n);
if (s.setword){
fprintf(f, "setword = %x, ", s.setword);
/* print out all the elements in the set */
x = set_pdq(s);
while (*x!=nil){
fprintf(f, "%d ", *x);
++x;
}
}else{
fprintf(f, "setword = (nil)");
}
}
/* code to be able to dump out the nfas
return 0 if okay dump
return 1 if screwed up
*/
int
#ifdef __USE_PROTOS
dump_nfas(int first_node, int last_node)
#else
dump_nfas(first_node, last_node)
int first_node;
int last_node;
#endif
{
register int i;
nfa_node *t;
for (i=first_node; i<=last_node; ++i){
t = NFA(i);
if (!t) break;
fprintf(stderr, "nfa_node %d {\n", t->node_no);
fprintf(stderr, "\n\tnfa_set = %d\n", t->nfa_set);
fprintf(stderr, "\taccept\t=\t%d\n", t->accept);
fprintf(stderr, "\ttrans\t=\t(");
fprint_dfa_pair(stderr, t->trans[0]);
fprintf(stderr, ",");
fprint_dfa_pair(stderr, t->trans[1]);
fprintf(stderr, ")\n");
fprintf(stderr, "\tlabel\t=\t{ ");
fprint_set(stderr, t->label);
fprintf(stderr, "\t}\n");
fprintf(stderr, "}\n\n");
}
return 0;
}
#endif
>>
<<
/* DLG-specific syntax error message generator
* (define USER_ZZSYN when compiling so don't get 2 definitions)
*/
void
#ifdef __USE_PROTOS
zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok, int k, char *bad_text)
#else
zzsyn(text, tok, egroup, eset, etok, k, bad_text)
char *text, *egroup, *bad_text;
int tok;
int etok;
int k;
SetWordType *eset;
#endif
{
fprintf(stderr, ErrHdr, file_str[0]!=NULL?file_str[0]:"stdin", zzline);
fprintf(stderr, " syntax error at \"%s\"", (tok==zzEOF_TOKEN)?"EOF":text);
if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
if ( k==1 ) fprintf(stderr, " missing");
else
{
fprintf(stderr, "; \"%s\" not", bad_text);
if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
}
if ( zzset_deg(eset)>0 ) zzedecode(eset);
else fprintf(stderr, " %s", zztokens[etok]);
if ( strlen(egroup) > (size_t)0 ) fprintf(stderr, " in %s", egroup);
fprintf(stderr, "\n");
}
>>
|