123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- var type = new Array('IDENT', 'INT', 'REAL', 'RESERVED WORD', 'UNRECOGNIZED SYMBOL','EOF');
- var word = new Array('if', 'else', 'while', 'read', 'write', 'int', 'real','return');
- var specWord = new Array('==', '/*', '*/', '<>');
- var tokens;
- var err;
- var js_cmm = new Token('有思', '20130305', 0, 0);
- var tab = ' ';
- var rowNum, colNum;
- var rows, row, ch;
- function Token(t, v, r, c) {
- this.type = t;
- this.value = v;
-
- this.rowNum = r;
- this.colNum = c;
- };
- function init() {
- U.CD.Obj('UD_CD_Source').focus();
- word.sort();
- specWord.sort();
- };
- window.onload = init;
- function relex() {
-
- rowNum = 0;
- colNum = 0;
- rows = U.CD.Obj('UD_CD_Source').value.split('\n');
- row = rows[rowNum] + ' ';
- ch = row[colNum];
- tokens = null;
- tokens = new Array();
- tokens.push(js_cmm);
- err = null;
- err = new Array();
-
- cls();
- };
- function lex() {
- relex();
- getSym();
- };
- function getCh() {
- ch = row[colNum];
- if (colNum < row.length) {
-
- colNum++;
- }
- if (colNum == row.length) {
- if (rowNum != rows.length - 1) {
- rowNum++;
- colNum = 0;
- row = rows[rowNum] + ' ';
- }
- }
- return ch;
- };
- function getSym() {
-
- var token = new Token();
- var tokenType;
-
- ch = getCh();
-
- var As = new Array();
- var A;
- while (1) {
- var r = rowNum + 1;
- var c = colNum;
-
- if (ch == ' ' || ch == '\t') {
- ch = getCh();
- continue;
- }
-
- else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
- As = new Array();
- do {
- As.push(ch);
- ch = getCh();
- } while ( ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= "0" && ch <= '9' || ch == '_');
- A = As.join('');
-
- var i = 0,k = 0;
- var j = word.length - 1;
- do {
- k = Math.floor((i + j) / 2);
- Math.floor(k);
- if (A <= word[k]) j = k - 1;
- if (A >= word[k]) i = k + 1;
- } while ( i <= j );
-
- if (i - 1 > j) {
- tokenType = A;
-
- }
-
- else {
- tokenType = type[0];
- }
- }
-
- else if (ch >= '0' && ch <= '9') {
- As = new Array();
- do {
- As.push(ch);
- ch = getCh();
- } while ( ch >= '0' && ch <= '9' || ch == '.');
- A = As.join('');
- var patrn1 = /^\d+$/;
- var patrn2 = /\d+(\.)?((E|e)((\+|-)?))?\d+/;
- if ( patrn1.test(A)) {
- tokenType = type[1];
- }
- else if ( patrn2.test(A)){
- tokenType = type[2];
- }
- else {
- tokenType = type[4];
- }
- }
-
- else if (ch == '+' || ch == '-' || ch == '(' || ch == ')' || ch == ';' || ch == '{' ||
- ch == '}' || ch == '[' || ch == ']' || ch == '<' || ch == '>' || ch == '^' || ch == ',' || ch == '*' || ch == '/' || ch == '=') {
- tokenType = ch;
- A = ch;
- if (ch == '/' || ch == '=' || ch == '<') {
- As = new Array();
- As.push(ch);
- ch = getCh();
- As.push(ch);
- A = As.join('');
-
- var i = 0,
- k = 0;
- var j = specWord.length - 1;
- do {
- k = Math.floor((i + j) / 2);
- if (A <= specWord[k]) j = k - 1;
- if (A >= specWord[k]) i = k + 1;
- } while ( i <= j );
-
- if (i - 1 > j) {
- tokenType = specWord[k];
- }
-
- else {
- A = As[0];
- colNum--;
- }
- }
-
- if (tokenType == '/*') {
- do {ch = getCh();} while ( typeof(ch)!='undefined'
- && ( ch != '*' || getCh() != '/' ) )
- tokenType = 'comment';
- A = 'ignore';
- }
- ch = getCh();
- }
-
- else if (typeof(ch) == 'undefined') {
- token = new Token(type[5], 'EOF', r, c);
- tokens.push(token);
- token=null;
- break;
- }
-
- else {
- tokenType = type[4];
- A=ch;
- ch = getCh();
- }
- token = new Token(tokenType, A, r, c);
- if(tokenType==type[4]){
- err.push(token);
- }
- else if(tokenType=='comment'){
-
- }
- else{
- tokens.push(token);
- }
-
- token = null;
- }
-
- };
- function printall() {
- };
|