CocoSourcesCS 3
1 /*---------------------------------------------------------------------- 2 Compiler Generator Coco/R, 3 Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz 4 extended by M. Loeberbauer & A. Woess, Univ. of Linz 5 with improvements by Pat Terry, Rhodes University 6 7 This program is free software; you can redistribute it and/or modify it 8 under the terms of the GNU General Public License as published by the 9 Free Software Foundation; either version 2, or (at your option) any 10 later version. 11 12 This program is distributed in the hope that it will be useful, but 13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program; if not, write to the Free Software Foundation, Inc., 19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 21 As an exception, it is allowed to write an extension of Coco/R that is 22 used as a plugin in non-free software. 23 24 If not otherwise stated, any source code generated by Coco/R (other than 25 Coco/R itself) does not fall under the GNU General Public License. 26 -----------------------------------------------------------------------*/ 27 using System.IO; 28 29 30 31 using System; 32 33 namespace at.jku.ssw.Coco { 34 35 36 37 public class Parser { 38 public const int _EOF = 0; 39 public const int _ident = 1; 40 public const int _number = 2; 41 public const int _string = 3; 42 public const int _badString = 4; 43 public const int _char = 5; 44 public const int maxT = 41; 45 public const int _ddtSym = 42; 46 public const int _optionSym = 43; 47 48 const bool _T = true; 49 const bool _x = false; 50 const int minErrDist = 2; 51 52 public Scanner scanner; 53 public Errors errors; 54 55 public Token t; // last recognized token 56 public Token la; // lookahead token 57 int errDist = minErrDist; 58 59 const int id = 0; 60 const int str = 1; 61 62 public TextWriter trace; // other Coco objects referenced in this ATG 63 public Tab tab; 64 public DFA dfa; 65 public ParserGen pgen; 66 67 bool genScanner; 68 string tokenString; // used in declarations of literal tokens 69 string noString = "-none-"; // used in declarations of literal tokens 70 71 /*-------------------------------------------------------------------------*/ 72 73 74 75 public Parser(Scanner scanner) { 76 this.scanner = scanner; 77 errors = new Errors(); 78 } 79 80 void SynErr (int n) { 81 if (errDist >= minErrDist) errors.SynErr(la.line, la.col, n); 82 errDist = 0; 83 } 84 85 public void SemErr (string msg) { 86 if (errDist >= minErrDist) errors.SemErr(t.line, t.col, msg); 87 errDist = 0; 88 } 89 90 void Get () { 91 for (;;) { 92 t = la; 93 la = scanner.Scan(); 94 if (la.kind <= maxT) { ++errDist; break; } 95 if (la.kind == 42) { 96 tab.SetDDT(la.val); 97 } 98 if (la.kind == 43) { 99 tab.SetOption(la.val); 100 } 101 102 la = t; 103 } 104 } 105 106 void Expect (int n) { 107 if (la.kind==n) Get(); else { SynErr(n); } 108 } 109 110 bool StartOf (int s) { 111 return set[s, la.kind]; 112 } 113 114 void ExpectWeak (int n, int follow) { 115 if (la.kind == n) Get(); 116 else { 117 SynErr(n); 118 while (!StartOf(follow)) Get(); 119 } 120 } 121 122 123 bool WeakSeparator(int n, int syFol, int repFol) { 124 int kind = la.kind; 125 if (kind == n) {Get(); return true;} 126 else if (StartOf(repFol)) {return false;} 127 else { 128 SynErr(n); 129 while (!(set[syFol, kind] || set[repFol, kind] || set[0, kind])) { 130 Get(); 131 kind = la.kind; 132 } 133 return StartOf(syFol); 134 } 135 } 136 137 138 void Coco() { 139 Symbol sym; Graph g, g1, g2; string gramName; CharSet s; int beg, line; 140 if (StartOf(1)) { 141 Get(); 142 beg = t.pos; line = t.line; 143 while (StartOf(1)) { 144 Get(); 145 } 146 pgen.usingPos = new Position(beg, la.pos, 0, line); 147 } 148 Expect(6); 149 genScanner = true; 150 tab.ignored = new CharSet(); 151 Expect(1); 152 gramName = t.val; 153 beg = la.pos; line = la.line; 154 155 while (StartOf(2)) { 156 Get(); 157 } 158 tab.semDeclPos = new Position(beg, la.pos, 0, line); 159 if (la.kind == 7) { 160 Get(); 161 dfa.ignoreCase = true; 162 } 163 if (la.kind == 8) { 164 Get(); 165 while (la.kind == 1) { 166 SetDecl(); 167 } 168 } 169 if (la.kind == 9) { 170 Get(); 171 while (la.kind == 1 || la.kind == 3 || la.kind == 5) { 172 TokenDecl(Node.t); 173 } 174 } 175 if (la.kind == 10) { 176 Get(); 177 while (la.kind == 1 || la.kind == 3 || la.kind == 5) { 178 TokenDecl(Node.pr); 179 } 180 } 181 while (la.kind == 11) { 182 Get(); 183 bool nested = false; 184 Expect(12); 185 TokenExpr(out g1); 186 Expect(13); 187 TokenExpr(out g2); 188 if (la.kind == 14) { 189 Get(); 190 nested = true; 191 } 192 dfa.NewComment(g1.l, g2.l, nested); 193 } 194 while (la.kind == 15) { 195 Get(); 196 Set(out s); 197 tab.ignored.Or(s); 198 } 199 while (!(la.kind == 0 || la.kind == 16)) {SynErr(42); Get();} 200 Expect(16); 201 if (genScanner) dfa.MakeDeterministic(); 202 tab.DeleteNodes(); 203 204 while (la.kind == 1) { 205 Get(); 206 sym = tab.FindSym(t.val); 207 bool undef = sym == null; 208 if (undef) sym = tab.NewSym(Node.nt, t.val, t.line); 209 else { 210 if (sym.typ == Node.nt) { 211 if (sym.graph != null) SemErr("name declared twice"); 212 } else SemErr("this symbol kind not allowed on left side of production"); 213 sym.line = t.line; 214 } 215 bool noAttrs = sym.attrPos == null; 216 sym.attrPos = null; 217 218 if (la.kind == 24 || la.kind == 26) { 219 AttrDecl(sym); 220 } 221 if (!undef) 222 if (noAttrs != (sym.attrPos == null)) 223 SemErr("attribute mismatch between declaration and use of this symbol"); 224 225 if (la.kind == 39) { 226 SemText(out sym.semPos); 227 } 228 ExpectWeak(17, 3); 229 Expression(out g); 230 sym.graph = g.l; 231 tab.Finish(g); 232 233 ExpectWeak(18, 4); 234 } 235 Expect(19); 236 Expect(1); 237 if (gramName != t.val) 238 SemErr("name does not match grammar name"); 239 tab.gramSy = tab.FindSym(gramName); 240 if (tab.gramSy == null) 241 SemErr("missing production for grammar name"); 242 else { 243 sym = tab.gramSy; 244 if (sym.attrPos != null) 245 SemErr("grammar symbol must not have attributes"); 246 } 247 tab.noSym = tab.NewSym(Node.t, "???", 0); // noSym gets highest number 248 tab.SetupAnys(); 249 tab.RenumberPragmas(); 250 if (tab.ddt[2]) tab.PrintNodes(); 251 if (errors.count == 0) { 252 Console.WriteLine("checking"); 253 tab.CompSymbolSets(); 254 if (tab.ddt[7]) tab.XRef(); 255 if (tab.GrammarOk()) { 256 Console.Write("parser"); 257 pgen.WriteParser(); 258 if (genScanner) { 259 Console.Write(" + scanner"); 260 dfa.WriteScanner(); 261 if (tab.ddt[0]) dfa.PrintStates(); 262 } 263 Console.WriteLine(" generated"); 264 if (tab.ddt[8]) pgen.WriteStatistics(); 265 } 266 } 267 if (tab.ddt[6]) tab.PrintSymbolTable(); 268 269 Expect(18); 270 } 271 272 void SetDecl() { 273 CharSet s; 274 Expect(1); 275 string name = t.val; 276 CharClass c = tab.FindCharClass(name); 277 if (c != null) SemErr("name declared twice"); 278 279 Expect(17); 280 Set(out s); 281 if (s.Elements() == 0) SemErr("character set must not be empty"); 282 tab.NewCharClass(name, s); 283 284 Expect(18); 285 } 286 287 void TokenDecl(int typ) { 288 string name; int kind; Symbol sym; Graph g; 289 Sym(out name, out kind); 290 sym = tab.FindSym(name); 291 if (sym != null) SemErr("name declared twice"); 292 else { 293 sym = tab.NewSym(typ, name, t.line); 294 sym.tokenKind = Symbol.fixedToken; 295 } 296 tokenString = null; 297 298 while (!(StartOf(5))) {SynErr(43); Get();} 299 if (la.kind == 17) { 300 Get(); 301 TokenExpr(out g); 302 Expect(18); 303 if (kind == str) SemErr("a literal must not be declared with a structure"); 304 tab.Finish(g); 305 if (tokenString == null || tokenString.Equals(noString)) 306 dfa.ConvertToStates(g.l, sym); 307 else { // TokenExpr is a single string 308 if (tab.literals[tokenString] != null) 309 SemErr("token string declared twice"); 310 tab.literals[tokenString] = sym; 311 dfa.MatchLiteral(tokenString, sym); 312 } 313 314 } else if (StartOf(6)) { 315 if (kind == id) genScanner = false; 316 else dfa.MatchLiteral(sym.name, sym); 317 318 } else SynErr(44); 319 if (la.kind == 39) { 320 SemText(out sym.semPos); 321 if (typ != Node.pr) SemErr("semantic action not allowed here"); 322 } 323 } 324 325 void TokenExpr(out Graph g) { 326 Graph g2; 327 TokenTerm(out g); 328 bool first = true; 329 while (WeakSeparator(28,7,8) ) { 330 TokenTerm(out g2); 331 if (first) { tab.MakeFirstAlt(g); first = false; } 332 tab.MakeAlternative(g, g2); 333 334 } 335 } 336 337 void Set(out CharSet s) { 338 CharSet s2; 339 SimSet(out s); 340 while (la.kind == 20 || la.kind == 21) { 341 if (la.kind == 20) { 342 Get(); 343 SimSet(out s2); 344 s.Or(s2); 345 } else { 346 Get(); 347 SimSet(out s2); 348 s.Subtract(s2); 349 } 350 } 351 } 352 353 void AttrDecl(Symbol sym) { 354 if (la.kind == 24) { 355 Get(); 356 int beg = la.pos; int col = la.col; int line = la.line; 357 while (StartOf(9)) { 358 if (StartOf(10)) { 359 Get(); 360 } else { 361 Get(); 362 SemErr("bad string in attributes"); 363 } 364 } 365 Expect(25); 366 if (t.pos > beg) 367 sym.attrPos = new Position(beg, t.pos, col, line); 368 } else if (la.kind == 26) { 369 Get(); 370 int beg = la.pos; int col = la.col; int line = la.line; 371 while (StartOf(11)) { 372 if (StartOf(12)) { 373 Get(); 374 } else { 375 Get(); 376 SemErr("bad string in attributes"); 377 } 378 } 379 Expect(27); 380 if (t.pos > beg) 381 sym.attrPos = new Position(beg, t.pos, col, line); 382 } else SynErr(45); 383 } 384 385 void SemText(out Position pos) { 386 Expect(39); 387 int beg = la.pos; int col = la.col; int line = la.line; 388 while (StartOf(13)) { 389 if (StartOf(14)) { 390 Get(); 391 } else if (la.kind == 4) { 392 Get(); 393 SemErr("bad string in semantic action"); 394 } else { 395 Get(); 396 SemErr("missing end of previous semantic action"); 397 } 398 } 399 Expect(40); 400 pos = new Position(beg, t.pos, col, line); 401 } 402 403 void Expression(out Graph g) { 404 Graph g2; 405 Term(out g); 406 bool first = true; 407 while (WeakSeparator(28,15,16) ) { 408 Term(out g2); 409 if (first) { tab.MakeFirstAlt(g); first = false; } 410 tab.MakeAlternative(g, g2); 411 412 } 413 } 414 415 void SimSet(out CharSet s) { 416 int n1, n2; 417 s = new CharSet(); 418 if (la.kind == 1) { 419 Get(); 420 CharClass c = tab.FindCharClass(t.val); 421 if (c == null) SemErr("undefined name"); else s.Or(c.set); 422 423 } else if (la.kind == 3) { 424 Get(); 425 string name = t.val; 426 name = tab.Unescape(name.Substring(1, name.Length-2)); 427 foreach (char ch in name) 428 if (dfa.ignoreCase) s.Set(char.ToLower(ch)); 429 else s.Set(ch); 430 } else if (la.kind == 5) { 431 Char(out n1); 432 s.Set(n1); 433 if (la.kind == 22) { 434 Get(); 435 Char(out n2); 436 for (int i = n1; i <= n2; i++) s.Set(i); 437 } 438 } else if (la.kind == 23) { 439 Get(); 440 s = new CharSet(); s.Fill(); 441 } else SynErr(46); 442 } 443 444 void Char(out int n) { 445 Expect(5); 446 string name = t.val; n = 0; 447 name = tab.Unescape(name.Substring(1, name.Length-2)); 448 if (name.Length == 1) n = name[0]; 449 else SemErr("unacceptable character value"); 450 if (dfa.ignoreCase && (char)n >= 'A' && (char)n <= 'Z') n += 32; 451 452 } 453 454 void Sym(out string name, out int kind) { 455 name = "???"; kind = id; 456 if (la.kind == 1) { 457 Get(); 458 kind = id; name = t.val; 459 } else if (la.kind == 3 || la.kind == 5) { 460 if (la.kind == 3) { 461 Get(); 462 name = t.val; 463 } else { 464 Get(); 465 name = """ + t.val.Substring(1, t.val.Length-2) + """; 466 } 467 kind = str; 468 if (dfa.ignoreCase) name = name.ToLower(); 469 if (name.IndexOf(' ') >= 0) 470 SemErr("literal tokens must not contain blanks"); 471 } else SynErr(47); 472 } 473 474 void Term(out Graph g) { 475 Graph g2; Node rslv = null; g = null; 476 if (StartOf(17)) { 477 if (la.kind == 37) { 478 rslv = tab.NewNode(Node.rslv, null, la.line); 479 Resolver(out rslv.pos); 480 g = new Graph(rslv); 481 } 482 Factor(out g2); 483 if (rslv != null) tab.MakeSequence(g, g2); 484 else g = g2; 485 486 while (StartOf(18)) { 487 Factor(out g2); 488 tab.MakeSequence(g, g2); 489 } 490 } else if (StartOf(19)) { 491 g = new Graph(tab.NewNode(Node.eps, null, 0)); 492 } else SynErr(48); 493 if (g == null) // invalid start of Term 494 g = new Graph(tab.NewNode(Node.eps, null, 0)); 495 496 } 497 498 void Resolver(out Position pos) { 499 Expect(37); 500 Expect(30); 501 int beg = la.pos; int col = la.col; int line = la.line; 502 Condition(); 503 pos = new Position(beg, t.pos, col, line); 504 } 505 506 void Factor(out Graph g) { 507 string name; int kind; Position pos; bool weak = false; 508 g = null; 509 510 switch (la.kind) { 511 case 1: case 3: case 5: case 29: { 512 if (la.kind == 29) { 513 Get(); 514 weak = true; 515 } 516 Sym(out name, out kind); 517 Symbol sym = tab.FindSym(name); 518 if (sym == null && kind == str) 519 sym = tab.literals[name] as Symbol; 520 bool undef = sym == null; 521 if (undef) { 522 if (kind == id) 523 sym = tab.NewSym(Node.nt, name, 0); // forward nt 524 else if (genScanner) { 525 sym = tab.NewSym(Node.t, name, t.line); 526 dfa.MatchLiteral(sym.name, sym); 527 } else { // undefined string in production 528 SemErr("undefined string in production"); 529 sym = tab.eofSy; // dummy 530 } 531 } 532 int typ = sym.typ; 533 if (typ != Node.t && typ != Node.nt) 534 SemErr("this symbol kind is not allowed in a production"); 535 if (weak) 536 if (typ == Node.t) typ = Node.wt; 537 else SemErr("only terminals may be weak"); 538 Node p = tab.NewNode(typ, sym, t.line); 539 g = new Graph(p); 540 541 if (la.kind == 24 || la.kind == 26) { 542 Attribs(p); 543 if (kind != id) SemErr("a literal must not have attributes"); 544 } 545 if (undef) 546 sym.attrPos = p.pos; // dummy 547 else if ((p.pos == null) != (sym.attrPos == null)) 548 SemErr("attribute mismatch between declaration and use of this symbol"); 549 550 break; 551 } 552 case 30: { 553 Get(); 554 Expression(out g); 555 Expect(31); 556 break; 557 } 558 case 32: { 559 Get(); 560 Expression(out g); 561 Expect(33); 562 tab.MakeOption(g); 563 break; 564 } 565 case 34: { 566 Get(); 567 Expression(out g); 568 Expect(35); 569 tab.MakeIteration(g); 570 break; 571 } 572 case 39: { 573 SemText(out pos); 574 Node p = tab.NewNode(Node.sem, null, 0); 575 p.pos = pos; 576 g = new Graph(p); 577 578 break; 579 } 580 case 23: { 581 Get(); 582 Node p = tab.NewNode(Node.any, null, 0); // p.set is set in tab.SetupAnys 583 g = new Graph(p); 584 585 break; 586 } 587 case 36: { 588 Get(); 589 Node p = tab.NewNode(Node.sync, null, 0); 590 g = new Graph(p); 591 592 break; 593 } 594 default: SynErr(49); break; 595 } 596 if (g == null) // invalid start of Factor 597 g = new Graph(tab.NewNode(Node.eps, null, 0)); 598 599 } 600 601 void Attribs(Node p) { 602 if (la.kind == 24) { 603 Get(); 604 int beg = la.pos; int col = la.col; int line = la.line; 605 while (StartOf(9)) { 606 if (StartOf(10)) { 607 Get(); 608 } else { 609 Get(); 610 SemErr("bad string in attributes"); 611 } 612 } 613 Expect(25); 614 if (t.pos > beg) p.pos = new Position(beg, t.pos, col, line); 615 } else if (la.kind == 26) { 616 Get(); 617 int beg = la.pos; int col = la.col; int line = la.line; 618 while (StartOf(11)) { 619 if (StartOf(12)) { 620 Get(); 621 } else { 622 Get(); 623 SemErr("bad string in attributes"); 624 } 625 } 626 Expect(27); 627 if (t.pos > beg) p.pos = new Position(beg, t.pos, col, line); 628 } else SynErr(50); 629 } 630 631 void Condition() { 632 while (StartOf(20)) { 633 if (la.kind == 30) { 634 Get(); 635 Condition(); 636 } else { 637 Get(); 638 } 639 } 640 Expect(31); 641 } 642 643 void TokenTerm(out Graph g) { 644 Graph g2; 645 TokenFactor(out g); 646 while (StartOf(7)) { 647 TokenFactor(out g2); 648 tab.MakeSequence(g, g2); 649 } 650 if (la.kind == 38) { 651 Get(); 652 Expect(30); 653 TokenExpr(out g2); 654 tab.SetContextTrans(g2.l); dfa.hasCtxMoves = true; 655 tab.MakeSequence(g, g2); 656 Expect(31); 657 } 658 } 659 660 void TokenFactor(out Graph g) { 661 string name; int kind; 662 g = null; 663 if (la.kind == 1 || la.kind == 3 || la.kind == 5) { 664 Sym(out name, out kind); 665 if (kind == id) { 666 CharClass c = tab.FindCharClass(name); 667 if (c == null) { 668 SemErr("undefined name"); 669 c = tab.NewCharClass(name, new CharSet()); 670 } 671 Node p = tab.NewNode(Node.clas, null, 0); p.val = c.n; 672 g = new Graph(p); 673 tokenString = noString; 674 } else { // str 675 g = tab.StrToGraph(name); 676 if (tokenString == null) tokenString = name; 677 else tokenString = noString; 678 } 679 680 } else if (la.kind == 30) { 681 Get(); 682 TokenExpr(out g); 683 Expect(31); 684 } else if (la.kind == 32) { 685 Get(); 686 TokenExpr(out g); 687 Expect(33); 688 tab.MakeOption(g); tokenString = noString; 689 } else if (la.kind == 34) { 690 Get(); 691 TokenExpr(out g); 692 Expect(35); 693 tab.MakeIteration(g); tokenString = noString; 694 } else SynErr(51); 695 if (g == null) // invalid start of TokenFactor 696 g = new Graph(tab.NewNode(Node.eps, null, 0)); 697 } 698 699 700 701 public void Parse() { 702 la = new Token(); 703 la.val = ""; 704 Get(); 705 Coco(); 706 Expect(0); 707 708 } 709 710 static readonly bool[,] set = { 711 {_T,_T,_x,_T, _x,_T,_x,_x, _x,_x,_T,_T, _x,_x,_x,_T, _T,_T,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_T, _x,_x,_x}, 712 {_x,_T,_T,_T, _T,_T,_x,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_x}, 713 {_x,_T,_T,_T, _T,_T,_T,_x, _x,_x,_x,_x, _T,_T,_T,_x, _x,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_x}, 714 {_T,_T,_x,_T, _x,_T,_x,_x, _x,_x,_T,_T, _x,_x,_x,_T, _T,_T,_T,_x, _x,_x,_x,_T, _x,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,_x,_T, _x,_x,_x}, 730 {_x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_T,_x, _x,_x,_x,_x, _x,_x,_x,_x, _T,_x,_x,_T, _x,_T,_x,_T, _x,_x,_x,_x, _x,_x,_x}, 731 {_x,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_T,_x, _T,_T,_T,_T, _T,_T,_T,_T, _T,_T,_x} 732 733 }; 734 } // end Parser 735 736 737 public class Errors { 738 public int count = 0; // number of errors detected 739 public System.IO.TextWriter errorStream = Console.Out; // error messages go to this stream 740 public string errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text 741 742 public virtual void SynErr (int line, int col, int n) { 743 string s; 744 switch (n) { 745 case 0: s = "EOF expected"; break; 746 case 1: s = "ident expected"; break; 747 case 2: s = "number expected"; break; 748 case 3: s = "string expected"; break; 749 case 4: s = "badString expected"; break; 750 case 5: s = "char expected"; break; 751 case 6: s = ""COMPILER" expected"; break; 752 case 7: s = ""IGNORECASE" expected"; break; 753 case 8: s = ""CHARACTERS" expected"; break; 754 case 9: s = ""TOKENS" expected"; break; 755 case 10: s = ""PRAGMAS" expected"; break; 756 case 11: s = ""COMMENTS" expected"; break; 757 case 12: s = ""FROM" expected"; break; 758 case 13: s = ""TO" expected"; break; 759 case 14: s = ""NESTED" expected"; break; 760 case 15: s = ""IGNORE" expected"; break; 761 case 16: s = ""PRODUCTIONS" expected"; break; 762 case 17: s = ""=" expected"; break; 763 case 18: s = ""." expected"; break; 764 case 19: s = ""END" expected"; break; 765 case 20: s = ""+" expected"; break; 766 case 21: s = ""-" expected"; break; 767 case 22: s = "".." expected"; break; 768 case 23: s = ""ANY" expected"; break; 769 case 24: s = ""<" expected"; break; 770 case 25: s = "">" expected"; break; 771 case 26: s = ""<." expected"; break; 772 case 27: s = "".>" expected"; break; 773 case 28: s = ""|" expected"; break; 774 case 29: s = ""WEAK" expected"; break; 775 case 30: s = ""(" expected"; break; 776 case 31: s = "")" expected"; break; 777 case 32: s = ""[" expected"; break; 778 case 33: s = ""]" expected"; break; 779 case 34: s = ""{" expected"; break; 780 case 35: s = ""}" expected"; break; 781 case 36: s = ""SYNC" expected"; break; 782 case 37: s = ""IF" expected"; break; 783 case 38: s = ""CONTEXT" expected"; break; 784 case 39: s = ""(." expected"; break; 785 case 40: s = "".)" expected"; break; 786 case 41: s = "??? expected"; break; 787 case 42: s = "this symbol not expected in Coco"; break; 788 case 43: s = "this symbol not expected in TokenDecl"; break; 789 case 44: s = "invalid TokenDecl"; break; 790 case 45: s = "invalid AttrDecl"; break; 791 case 46: s = "invalid SimSet"; break; 792 case 47: s = "invalid Sym"; break; 793 case 48: s = "invalid Term"; break; 794 case 49: s = "invalid Factor"; break; 795 case 50: s = "invalid Attribs"; break; 796 case 51: s = "invalid TokenFactor"; break; 797 798 default: s = "error " + n; break; 799 } 800 errorStream.WriteLine(errMsgFormat, line, col, s); 801 count++; 802 } 803 804 public virtual void SemErr (int line, int col, string s) { 805 errorStream.WriteLine(errMsgFormat, line, col, s); 806 count++; 807 } 808 809 public virtual void SemErr (string s) { 810 errorStream.WriteLine(s); 811 count++; 812 } 813 814 public virtual void Warning (int line, int col, string s) { 815 errorStream.WriteLine(errMsgFormat, line, col, s); 816 } 817 818 public virtual void Warning(string s) { 819 errorStream.WriteLine(s); 820 } 821 } // Errors 822 823 824 public class FatalError: Exception { 825 public FatalError(string m): base(m) {} 826 } 827 }