U.DC.CL.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //符号表项类
  2. function SymItem(name, type, val) {
  3. this.Name = name || "";
  4. this.Type = type || "double";
  5. this.Val = val || 0;
  6. }
  7. var SymType_INT='int';
  8. var SymType_REAL = 'real';
  9. //符号表类
  10. function SymTable() {
  11. this.SymItemList = new Array();
  12. }
  13. //定位符号表项
  14. SymTable.prototype.position = function(str) {
  15. for ( var i = 0; i < this.SymItemList.length; i++) {
  16. if (str == this.SymItemList[i].Name) {
  17. return this.SymItemList[i];
  18. }
  19. }
  20. return null;
  21. }
  22. //语法树的节点
  23. function Node(type) {
  24. this.type = type; // 节点类型
  25. this.str = type; // 节点的字符串形式
  26. this.children = new Array(); // 节点的孩子链表
  27. }
  28. //匹配单词
  29. Node.prototype.match1 = function(type) {
  30. while (!(Syntax.readall)) {
  31. Syntax.getWord();
  32. if (Syntax.word.type == type) {
  33. Syntax.nextWord();
  34. break;
  35. } else {
  36. if (type == "IDENT") {
  37. Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少变量。";
  38. } else if (type == "INT") {
  39. Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少整数。";
  40. } else if (type == "REAL") {
  41. Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少实数。";
  42. } else {
  43. Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少" + type + "。";
  44. }
  45. Syntax.nextWord();
  46. }
  47. }
  48. }
  49. Node.prototype.match2 = function (s) {
  50. if (!(Syntax.readall)) {
  51. while (!(Syntax.readall)) {
  52. Syntax.getWord();
  53. if (Syntax.word.value == s) {
  54. Syntax.nextWord();
  55. break;
  56. } else {
  57. Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少 " + s + " 。";
  58. while (s != Syntax.word.value) {
  59. Syntax.nextWord();
  60. if (Syntax.index < tokens.length) {
  61. Syntax.getWord();
  62. } else
  63. break;
  64. }
  65. Syntax.index--;
  66. }
  67. Syntax.nextWord();
  68. }
  69. } else {
  70. Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少 " + s + " 。";
  71. }
  72. }