123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //符号表项类
- function SymItem(name, type, val) {
- this.Name = name || "";
- this.Type = type || "double";
- this.Val = val || 0;
- }
- var SymType_INT='int';
- var SymType_REAL = 'real';
- //符号表类
- function SymTable() {
- this.SymItemList = new Array();
- }
- //定位符号表项
- SymTable.prototype.position = function(str) {
- for ( var i = 0; i < this.SymItemList.length; i++) {
- if (str == this.SymItemList[i].Name) {
- return this.SymItemList[i];
- }
- }
- return null;
- }
- //语法树的节点
- function Node(type) {
- this.type = type; // 节点类型
- this.str = type; // 节点的字符串形式
- this.children = new Array(); // 节点的孩子链表
- }
- //匹配单词
- Node.prototype.match1 = function(type) {
- while (!(Syntax.readall)) {
- Syntax.getWord();
- if (Syntax.word.type == type) {
- Syntax.nextWord();
- break;
- } else {
- if (type == "IDENT") {
- Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少变量。";
-
- } else if (type == "INT") {
- Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少整数。";
- } else if (type == "REAL") {
- Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少实数。";
- } else {
- Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少" + type + "。";
-
- }
- Syntax.nextWord();
- }
- }
- }
- Node.prototype.match2 = function (s) {
- if (!(Syntax.readall)) {
- while (!(Syntax.readall)) {
- Syntax.getWord();
- if (Syntax.word.value == s) {
- Syntax.nextWord();
- break;
- } else {
- Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少 " + s + " 。";
-
- while (s != Syntax.word.value) {
- Syntax.nextWord();
- if (Syntax.index < tokens.length) {
- Syntax.getWord();
- } else
- break;
- }
- Syntax.index--;
- }
- Syntax.nextWord();
- }
- } else {
- Syntax.error += "<br>错误发生在第" + Syntax.word.rowNum + "行:缺少 " + s + " 。";
-
- }
- }
|