[9430] | 1 | // GPDefSyntaxAnalyzerLex.cs HDO, 2006-08-28
|
---|
| 2 | // -------------
|
---|
| 3 | // Lexical analyzer (finite-state machine interpreter).
|
---|
| 4 | // Generated by Coco-2 (SG).
|
---|
| 5 | //=====================================|========================================
|
---|
| 6 |
|
---|
| 7 | #undef TEST_Lex
|
---|
| 8 |
|
---|
| 9 | using System;
|
---|
| 10 | using System.Collections;
|
---|
| 11 | using System.Collections.Specialized;
|
---|
| 12 | using System.IO;
|
---|
| 13 | using System.Text;
|
---|
| 14 |
|
---|
| 15 | public class GPDefSyntaxAnalyzerLex {
|
---|
| 16 |
|
---|
| 17 | public const String MODULENAME = "GPDefSyntaxAnalyzerLex";
|
---|
| 18 | public const int EOFSPIX = 1;
|
---|
| 19 |
|
---|
| 20 | public static TextReader src;
|
---|
| 21 |
|
---|
| 22 | // --- token information ---
|
---|
| 23 | public static int token; // current token
|
---|
| 24 | public static int tokenLine, tokenCol; // position of current token
|
---|
| 25 | public static String tokenStr; // token string recognized
|
---|
| 26 |
|
---|
| 27 | // --- current char. info, for "power users" only ---
|
---|
| 28 | public static char curCh; // current input character
|
---|
| 29 | public static int curLine, curCol; // position of curCh
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | public static void GPDefSyntaxAnalyzerLexMethod(Utils.ModuleAction action, out String moduleName) {
|
---|
| 33 | //-----------------------------------|----------------------------------------
|
---|
| 34 | moduleName = MODULENAME;
|
---|
| 35 | switch (action) {
|
---|
| 36 | case Utils.ModuleAction.getModuleName:
|
---|
| 37 | return;
|
---|
| 38 | case Utils.ModuleAction.initModule:
|
---|
| 39 | caseSensitive = true;
|
---|
| 40 | lt = new LexicalTable();
|
---|
| 41 | tokenStrArr = new char[256];
|
---|
| 42 | kwHt = CreateHashtable();
|
---|
| 43 | nHt = CreateHashtable();
|
---|
| 44 | nl = new ArrayList();
|
---|
| 45 | break;
|
---|
| 46 | case Utils.ModuleAction.resetModule:
|
---|
| 47 | kwHt.Clear();
|
---|
| 48 | nHt.Clear();
|
---|
| 49 | nl.Clear();
|
---|
| 50 | break;
|
---|
| 51 | case Utils.ModuleAction.cleanupModule:
|
---|
| 52 | lt = null;
|
---|
| 53 | tokenStrArr = null;
|
---|
| 54 | kwHt = null;
|
---|
| 55 | nHt = null;
|
---|
| 56 | nl = null;
|
---|
| 57 | break;
|
---|
| 58 | } // switch
|
---|
| 59 | } // GPDefSyntaxAnalyzerLexMethod
|
---|
| 60 |
|
---|
| 61 | private static Hashtable CreateHashtable() {
|
---|
| 62 | if (caseSensitive)
|
---|
| 63 | return new Hashtable();
|
---|
| 64 | else
|
---|
| 65 | return CollectionsUtil.CreateCaseInsensitiveHashtable();
|
---|
| 66 | } // CreateHashtable
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | public static void InitLex() {
|
---|
| 70 | //-----------------------------------|----------------------------------------
|
---|
| 71 | // --- initialize keyword hash table ---
|
---|
| 72 | kwHt.Clear();
|
---|
| 73 | EnterKeyword( 1, "PROBLEM");
|
---|
| 74 | EnterKeyword( 2, "END");
|
---|
| 75 | EnterKeyword( 3, "EPS");
|
---|
| 76 | EnterKeyword( 4, "LOCAL");
|
---|
| 77 | EnterKeyword( 5, "NONTERMINALS");
|
---|
| 78 | EnterKeyword( 6, "RULES");
|
---|
| 79 | EnterKeyword( 7, "SEM");
|
---|
| 80 | EnterKeyword( 8, "MAXIMIZE");
|
---|
| 81 | EnterKeyword( 9, "MINIMIZE");
|
---|
| 82 | EnterKeyword( 10, "TERMINALS");
|
---|
| 83 | EnterKeyword( 11, "CONSTRAINTS");
|
---|
| 84 | EnterKeyword( 12, "INIT");
|
---|
| 85 | EnterKeyword( 13, "CODE");
|
---|
| 86 | EnterKeyword( 14, "IN");
|
---|
| 87 | EnterKeyword( 15, "SET");
|
---|
| 88 | EnterKeyword( 16, "RANGE");
|
---|
| 89 | // --- initialize name data structures ---
|
---|
| 90 | nHt.Clear();
|
---|
| 91 | nl.Clear();
|
---|
| 92 | nl.Add(""); // so spix = 0 is the empty string
|
---|
| 93 | nl.Add("!EOF!"); // so EOFSPIX = 1
|
---|
| 94 | // --- (re)set global data ---
|
---|
| 95 | curLineStr = "";
|
---|
| 96 | curCh = ' ';
|
---|
| 97 | curLine = 0;
|
---|
| 98 | curCol = 2;
|
---|
| 99 | tokenStr = null;
|
---|
| 100 | token = 0;
|
---|
| 101 | tokenCol = 0;
|
---|
| 102 | tokenLine = 0;
|
---|
| 103 | tokenStrLen = 0;
|
---|
| 104 | pendingEOLs = 0;
|
---|
| 105 | apxLen = pendingEOLs; // to prevent warning
|
---|
| 106 | } // InitLex
|
---|
| 107 |
|
---|
| 108 | private static void EnterKeyword(int token, String keyword) {
|
---|
| 109 | kwHt.Add(keyword, token);
|
---|
| 110 | } // EnterKeyword
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | // *** start of global LEX declarations from ATG ***
|
---|
| 114 |
|
---|
| 115 | // *** end of global LEX declarations from ATG ***
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | public static bool caseSensitive;
|
---|
| 119 |
|
---|
| 120 | private class LexicalTable {
|
---|
| 121 | public int header = 4;
|
---|
| 122 | public int[] startTab = {
|
---|
| 123 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 125 | 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0, 17, 0,
|
---|
| 126 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 2, 10, 0,
|
---|
| 127 | 0, 16, 16, 16, 16, 16, 13, 16, 13, 16, 13, 13, 16, 16, 16, 16,
|
---|
| 128 | 16, 13, 16, 16, 16, 16, 13, 13, 16, 13, 16, 6, 0, 7, 0, 0,
|
---|
| 129 | 0, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
---|
| 130 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 8, 3, 9, 0, 0,
|
---|
| 131 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 132 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 133 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 137 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
---|
| 138 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
---|
| 139 | };
|
---|
| 140 | public Sets.Set256 ignoredChars = new Sets.Set256(
|
---|
| 141 | 0x0600, 0x0000, 0x0001
|
---|
| 142 | );
|
---|
| 143 | public Sets.Set256 commentStart = new Sets.Set256(
|
---|
| 144 | 0x0000, 0x0000, 0x8000
|
---|
| 145 | );
|
---|
| 146 | public Sets.Set256[] cls = {
|
---|
| 147 | new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x0000, 0xfffe, 0x07ff, 0xfffe, 0x07ff),
|
---|
| 148 | new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x03ff),
|
---|
| 149 | new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x0000, 0xf2be, 0x053d),
|
---|
| 150 | new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x0000, 0x0d40, 0x02c2, 0xfffe, 0x07ff)
|
---|
| 151 | };
|
---|
| 152 | } // LexicalTable
|
---|
| 153 |
|
---|
| 154 | private static LexicalTable lt;
|
---|
| 155 | private static Hashtable kwHt; // hash table for keywords: string -> token
|
---|
| 156 | private static Hashtable nHt; // hash table for names: string -> null
|
---|
| 157 | private static ArrayList nl; // name list for names, index is spix
|
---|
| 158 |
|
---|
| 159 | private static String curLineStr; // current source line
|
---|
| 160 | private static char[] tokenStrArr; // token string in an array
|
---|
| 161 | private static int tokenStrLen; // length of token string in tokenStrArr
|
---|
| 162 | private static int state; // current automaton state
|
---|
| 163 | private static int apxLen; // length of appendix in token string
|
---|
| 164 | private static int pendingEOLs; // nr of EOLs found in comment
|
---|
| 165 |
|
---|
| 166 | // --- to save and restore scanner state ---
|
---|
| 167 | private static char savedCh;
|
---|
| 168 | private static int savedCol, savedLine;
|
---|
| 169 |
|
---|
| 170 | private static void SaveScannerState() {
|
---|
| 171 | savedCh = curCh;
|
---|
| 172 | savedCol = curCol;
|
---|
| 173 | savedLine = curLine;
|
---|
| 174 | } // SaveScannerState
|
---|
| 175 |
|
---|
| 176 | private static void RestoreScannerState() {
|
---|
| 177 | tokenStrLen -= apxLen;
|
---|
| 178 | apxLen = 0;
|
---|
| 179 | curCh = savedCh;
|
---|
| 180 | curCol = savedCol;
|
---|
| 181 | curLine = savedLine;
|
---|
| 182 | } // RestoreScannerState
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | public static void NextCh() {
|
---|
| 186 | //-----------------------------------|----------------------------------------
|
---|
| 187 | for (;;) {
|
---|
| 188 |
|
---|
| 189 | if (curCol < curLineStr.Length) { // within line
|
---|
| 190 | curCol++;
|
---|
| 191 | curCh = curLineStr[curCol - 1];
|
---|
| 192 | return;
|
---|
| 193 | } else if (curCol == curLineStr.Length) { // end of line
|
---|
| 194 | curCol++;
|
---|
| 195 | curCh = Utils.LF; // to separate lines
|
---|
| 196 | return;
|
---|
| 197 | } else { // curCol > curLineStr.Length
|
---|
| 198 | curLineStr = src.ReadLine();
|
---|
| 199 | curLine++;
|
---|
| 200 | curCol = 0;
|
---|
| 201 | if (curLineStr == null) { // end of file
|
---|
| 202 | curLineStr = "";
|
---|
| 203 | curCh = Utils.EF;
|
---|
| 204 | return;
|
---|
| 205 | } // if
|
---|
| 206 | } // else
|
---|
| 207 | } // for
|
---|
| 208 | } // NextCh
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 | private static void CommentErr() {
|
---|
| 212 | Errors.LexError(curLine, curCol, "end of file in comment");
|
---|
| 213 | } // CommentErr
|
---|
| 214 |
|
---|
| 215 | private static bool Comment() {
|
---|
| 216 |
|
---|
| 217 | int level = 1;
|
---|
| 218 |
|
---|
| 219 | SaveScannerState();
|
---|
| 220 | // --- from '/' '*' to '*' '/' nested ---
|
---|
| 221 | NextCh();
|
---|
| 222 | if (curCh == '*') {
|
---|
| 223 | NextCh();
|
---|
| 224 | for (;;)
|
---|
| 225 | switch (curCh) {
|
---|
| 226 | case '*':
|
---|
| 227 | NextCh();
|
---|
| 228 | if (curCh == '/') {
|
---|
| 229 | NextCh();
|
---|
| 230 | level--;
|
---|
| 231 | if (level == 0) {
|
---|
| 232 | return true;
|
---|
| 233 | } // if
|
---|
| 234 | } // if
|
---|
| 235 | break;
|
---|
| 236 | case '/':
|
---|
| 237 | NextCh();
|
---|
| 238 | if (curCh == '*') {
|
---|
| 239 | level++;
|
---|
| 240 | NextCh();
|
---|
| 241 | }
|
---|
| 242 | break;
|
---|
| 243 | case Utils.EF:
|
---|
| 244 | CommentErr();
|
---|
| 245 | return true;
|
---|
| 246 | default:
|
---|
| 247 | NextCh();
|
---|
| 248 | break;
|
---|
| 249 | } // switch
|
---|
| 250 | } else
|
---|
| 251 | RestoreScannerState();
|
---|
| 252 | return false;
|
---|
| 253 | } // Comment
|
---|
| 254 |
|
---|
| 255 | private static void HandleLexErr() {
|
---|
| 256 | Errors.LexError(curLine, curCol,
|
---|
| 257 | "invalid character '{0}' (hex {1:X})", curCh, (int)curCh);
|
---|
| 258 | if (apxLen > 0)
|
---|
| 259 | RestoreScannerState();
|
---|
| 260 | } // HandleLexErr
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | public static void GetToken() {
|
---|
| 264 | //-----------------------------------|----------------------------------------
|
---|
| 265 | token = -1;
|
---|
| 266 | tokenStr = null;
|
---|
| 267 | do {
|
---|
| 268 | // --- skip ignored chars and comments ---
|
---|
| 269 | for (;;) {
|
---|
| 270 | while (Sets.member(curCh, lt.ignoredChars))
|
---|
| 271 | NextCh();
|
---|
| 272 | if (!Sets.member(curCh, lt.commentStart))
|
---|
| 273 | break;
|
---|
| 274 | if (!Comment())
|
---|
| 275 | break;
|
---|
| 276 | } // for
|
---|
| 277 | // --- scan for next token ---
|
---|
| 278 | tokenLine = curLine;
|
---|
| 279 | tokenCol = curCol;
|
---|
| 280 | tokenStrLen = 0;
|
---|
| 281 | apxLen = 0;
|
---|
| 282 | state = (curCh == Utils.EF) ? 1 : lt.startTab[curCh];
|
---|
| 283 | for (;;) {
|
---|
| 284 | tokenStrArr[tokenStrLen++]= curCh;
|
---|
| 285 | NextCh();
|
---|
| 286 | switch (state) {
|
---|
| 287 | case 0:
|
---|
| 288 | Errors.LexError(tokenLine, tokenCol, "invalid token start");
|
---|
| 289 | break;
|
---|
| 290 | case 1:
|
---|
| 291 | token = 0;
|
---|
| 292 | tokenStrLen = 0;
|
---|
| 293 | break; // EOF recognized
|
---|
| 294 | case 2:
|
---|
| 295 | token = 17;
|
---|
| 296 | break; // '=' recognized
|
---|
| 297 | case 3:
|
---|
| 298 | token = 18;
|
---|
| 299 | break; // '|' recognized
|
---|
| 300 | case 4:
|
---|
| 301 | token = 20;
|
---|
| 302 | break; // '(' recognized
|
---|
| 303 | case 5:
|
---|
| 304 | token = 21;
|
---|
| 305 | break; // ')' recognized
|
---|
| 306 | case 6:
|
---|
| 307 | token = 22;
|
---|
| 308 | break; // '[' recognized
|
---|
| 309 | case 7:
|
---|
| 310 | token = 23;
|
---|
| 311 | break; // ']' recognized
|
---|
| 312 | case 8:
|
---|
| 313 | token = 24;
|
---|
| 314 | break; // '{' recognized
|
---|
| 315 | case 9:
|
---|
| 316 | token = 25;
|
---|
| 317 | break; // '}' recognized
|
---|
| 318 | case 10:
|
---|
| 319 | if (curCh == '>') {
|
---|
| 320 | state = 11;
|
---|
| 321 | continue;
|
---|
| 322 | } // if
|
---|
| 323 | else {
|
---|
| 324 | HandleLexErr();
|
---|
| 325 | break;
|
---|
| 326 | } // else
|
---|
| 327 | case 11:
|
---|
| 328 | token = 26;
|
---|
| 329 | break; // '>>' recognized
|
---|
| 330 | case 12:
|
---|
| 331 | token = 27;
|
---|
| 332 | break; // '..' recognized
|
---|
| 333 | case 13:
|
---|
| 334 | if (Sets.member(curCh, lt.cls[0])) {
|
---|
| 335 | state = 13;
|
---|
| 336 | continue;
|
---|
| 337 | } // if
|
---|
| 338 | else if (Sets.member(curCh, lt.cls[1])) {
|
---|
| 339 | state = 13;
|
---|
| 340 | continue;
|
---|
| 341 | } // if
|
---|
| 342 | else {
|
---|
| 343 | token = KeywordCheck();
|
---|
| 344 | if (token >= 0)
|
---|
| 345 | break; // keyword recognized
|
---|
| 346 | token = 28;
|
---|
| 347 | break; // ident recognized
|
---|
| 348 | } // else
|
---|
| 349 | case 14:
|
---|
| 350 | if (curCh == '<') {
|
---|
| 351 | state = 15;
|
---|
| 352 | continue;
|
---|
| 353 | } // if
|
---|
| 354 | else {
|
---|
| 355 | HandleLexErr();
|
---|
| 356 | break;
|
---|
| 357 | } // else
|
---|
| 358 | case 15:
|
---|
| 359 | token = 29;
|
---|
| 360 | break; // source recognized
|
---|
| 361 | case 16:
|
---|
| 362 | if (Sets.member(curCh, lt.cls[1])) {
|
---|
| 363 | state = 13;
|
---|
| 364 | continue;
|
---|
| 365 | } // if
|
---|
| 366 | else if (Sets.member(curCh, lt.cls[2])) {
|
---|
| 367 | state = 16;
|
---|
| 368 | continue;
|
---|
| 369 | } // if
|
---|
| 370 | else if (Sets.member(curCh, lt.cls[3])) {
|
---|
| 371 | state = 13;
|
---|
| 372 | continue;
|
---|
| 373 | } // if
|
---|
| 374 | else {
|
---|
| 375 | token = KeywordCheck();
|
---|
| 376 | if (token >= 0)
|
---|
| 377 | break; // keyword recognized
|
---|
| 378 | token = 28;
|
---|
| 379 | break; // ident recognized
|
---|
| 380 | } // else
|
---|
| 381 | case 17:
|
---|
| 382 | if (curCh == '.') {
|
---|
| 383 | state = 12;
|
---|
| 384 | continue;
|
---|
| 385 | } // if
|
---|
| 386 | else {
|
---|
| 387 | token = 19;
|
---|
| 388 | break; // '.' recognized
|
---|
| 389 | } // else
|
---|
| 390 | } // switch
|
---|
| 391 | break;
|
---|
| 392 | } // for
|
---|
| 393 | } while (token < 0);
|
---|
| 394 | if (tokenStr == null)
|
---|
| 395 | tokenStr = new String(tokenStrArr, 0, tokenStrLen);
|
---|
| 396 | } // GetToken
|
---|
| 397 |
|
---|
| 398 | private static int KeywordCheck() {
|
---|
| 399 | tokenStr = new String(tokenStrArr, 0, tokenStrLen - apxLen);
|
---|
| 400 | Object token = kwHt[tokenStr];
|
---|
| 401 | return (token == null) ? -1 : (int)token;
|
---|
| 402 | } // KeywordCheck
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | public static int Hash(String s) {
|
---|
| 406 | //-----------------------------------|----------------------------------------
|
---|
| 407 | Object spix = nHt[s];
|
---|
| 408 | if (spix == null) {
|
---|
| 409 | if (caseSensitive)
|
---|
| 410 | nl.Add(s);
|
---|
| 411 | else
|
---|
| 412 | nl.Add(s.ToUpper());
|
---|
| 413 | spix = nl.Count - 1;
|
---|
| 414 | nHt[s] = spix;
|
---|
| 415 | } // if
|
---|
| 416 | return (int)spix;
|
---|
| 417 | } // Hash
|
---|
| 418 |
|
---|
| 419 |
|
---|
| 420 | public static String HashedStr(int spix) {
|
---|
| 421 | //-----------------------------------|----------------------------------------
|
---|
| 422 | return (String)nl[spix];
|
---|
| 423 | } // HashedStr
|
---|
| 424 |
|
---|
| 425 |
|
---|
| 426 | public static void GETidentAttr() {
|
---|
| 427 | } // GETidentAttr
|
---|
| 428 |
|
---|
| 429 | public static void GETsourceAttr() {
|
---|
| 430 | } // GETsourceAttr
|
---|
| 431 |
|
---|
| 432 | } // GPDefSyntaxAnalyzerLex
|
---|
| 433 |
|
---|
| 434 | // End of GPDefSyntaxAnalyzerLex.cs
|
---|
| 435 | //=====================================|========================================
|
---|