Changeset 6c8772 in git for modules/tools/scanner.l
- Timestamp:
- Apr 1, 1999, 12:03:24 AM (25 years ago)
- Branches:
- (u'spielwiese', '8e0ad00ce244dfd0756200662572aef8402f13d5')
- Children:
- dcafdbfd5e7cf5689011aad6c9b81fd43bd118b1
- Parents:
- 740e1f7291b169d818475089a70366a328feb8d1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
modules/tools/scanner.l
r740e1f7 r6c8772 3 3 * Computer Algebra System SINGULAR * 4 4 ****************************************/ 5 /* $Id: scanner.l,v 1. 3 1999-03-24 13:04:21krueger Exp $ */5 /* $Id: scanner.l,v 1.4 1999-03-31 22:03:23 krueger Exp $ */ 6 6 #include <stdio.h> 7 7 #include <string.h> … … 20 20 #include <tok.h> 21 21 22 #define DEBUG 0 23 24 # define YYLP_ERR_NONE 0 25 # define YYLP_DEF_BR2 1 26 # define YYLP_BODY_BR2 2 27 # define YYLP_BODY_BR3 3 28 # define YYLP_BODY_TMBR2 4 29 # define YYLP_BODY_TMBR3 5 30 # define YYLP_EX_BR2 6 31 # define YYLP_EX_BR3 7 32 # define YYLP_BAD_CHAR 8 33 # define YYLP_MISSQUOT 9 34 # define YYLP_MISS_BR1 10 35 # define YYLP_MISS_BR2 11 36 # define YYLP_MISS_BR3 12 37 22 38 const char sNoName[]="_"; 23 char my_yylinebuf[80]; 24 int old_state = 0; 39 int brace1 = 0; /* { } */ 40 int brace2 = 0; /* ( ) */ 41 int brace3 = 0; /* [ ] */ 42 int quote = 0; /* " */ 43 int offset = 0; 44 45 char my_yylinebuf[80]; 46 int *old_states = NULL; 47 int state_level = -1; 48 int state_max = 0; 25 49 int yylineno = 1; 26 50 int tok; … … 28 52 int traceit = 0; 29 53 moddef module_def; 54 int yylplineno = 1; 55 int yylp_errno = 0; 56 57 long C_start; 58 long C_end; 59 char c_codetype = 0; 60 #define C_CODE_NONE 0 61 #define C_CODE_PROC 1 62 #define C_CODE_MAIN 2 63 64 char string_type = 0; 65 #define STRING_NONE 0 66 #define STRING_INFO 1 67 #define STRING_VERS 2 68 long string_start = 0; 69 long string_end = 0; 70 71 char *yylp_buffer_start; 72 73 int libread(FILE* f, char* buf, int max_size); 74 int current_pos(int i); 75 int read_string(char **buffer, long *start, long end); 76 77 void push_state(int state, int new_state); 78 void pop_state(); 79 80 # undef YY_INPUT 81 # define YY_INPUT(buf,result,max_size) \ 82 if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \ 83 && ferror( yyin ) ) \ 84 YY_FATAL_ERROR( "read in flex scanner failed" ); 85 30 86 31 87 extern "C" … … 52 108 tos ({taborspace}*) 53 109 eq (=|{tos}+=|=+{tos}|{tos}+=+{tos}) 110 tnl ([ \t\n]*) 111 col (;|{tos}+;) 112 eqnl ([ \t\n]*+=[ \t\n]*) 54 113 55 114 /* %start START */ … … 57 116 %x pdef 58 117 %x comment 118 %x procdef 119 %x ctext 120 %x string 121 %x cstring 59 122 60 123 %% 61 124 (\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { } 62 \/\/* { old_state = YYSTATE; BEGIN(comment); }63 64 (module+{eq }+\"+{name}\") {125 \/\/* { push_state(YYSTATE, comment); } 126 127 (module+{eqnl}+{quote}+{name}+{quote}) { 65 128 char *buff = (char *)malloc(yyleng+4); 129 memset(buff, '\0', yyleng+4); 66 130 sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff); 67 131 module_def.name = (char *)malloc(strlen(buff)+1); 68 strcpy(module_def.name, buff); 132 memset(module_def.name, '\0', strlen(buff)+1); 133 memcpy(module_def.name, buff, strlen(buff)); 69 134 strcat(buff, ".cc"); 70 Add2files(&module_def, buff);135 Add2files(&module_def, buff); 71 136 free(buff); 72 137 } 73 138 74 (version+{eq}+\"+{strings}\") { 75 char *buff = (char *)malloc(yyleng); 76 sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff); 77 module_def.version = (char *)malloc(strlen(buff)+1); 78 strcpy(module_def.version, buff); 79 make_version(module_def.version, &module_def); 80 free(buff); 81 } 82 83 (helpfile+{eq}+\"+{fname}\") { 84 char *buff = (char *)malloc(yyleng); 139 (version+{eqnl}+{quote}+{strings}+{quote}) { 140 yyless(4); 141 string_type = STRING_VERS; 142 push_state(YYSTATE, cstring); 143 } 144 145 (helpfile+{eqnl}+{quote}+{fname}+{quote}) { 146 char *buff = (char *)malloc(yyleng+1); 147 memset(buff, '\0', yyleng+1); 85 148 sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff); 86 149 module_def.helpfile = (char *)malloc(strlen(buff)+1); 87 strcpy(module_def.helpfile, buff); 150 memset(module_def.helpfile, '\0', strlen(buff)+1); 151 memcpy(module_def.helpfile, buff, strlen(buff)); 152 printf("==>HELP:'%s'\n", module_def.helpfile); 88 153 free(buff); 89 154 } 90 155 91 (cxxsource+{eq}+{fname}) { 92 char *buff = (char *)malloc(yyleng); 156 (cxxsource+{eqnl}+{fname}) { 157 char *buff = (char *)malloc(yyleng+1); 158 memset(buff, '\0', yyleng+1); 93 159 sscanf( yytext, "%*[^=]=%s", buff); 94 160 Add2files(&module_def,buff); … … 96 162 } 97 163 98 (info+{eq}+\"+{strings}\") { 99 char *buff = (char *)malloc(yyleng); 100 sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff); 101 module_def.info = (char *)malloc(strlen(buff)+1); 102 strcpy(module_def.info, buff); 103 free(buff); 104 } 105 164 (info+{eqnl}+{quote}+{strings}+{quote}) { 165 yyless(4); 166 string_type = STRING_INFO; 167 push_state(YYSTATE, cstring); 168 } 169 170 <cstring>{quote} { quote++; push_state(YYSTATE, string); 171 string_start = current_pos(yyleng); } 172 <cstring>\n { yylplineno++; } 173 <cstring>{col} { 174 switch(string_type) { 175 case STRING_INFO: 176 read_string(&module_def.info, &string_start, string_end); 177 break; 178 179 case STRING_VERS: 180 read_string(&module_def.version, &string_start, string_end); 181 make_version(module_def.version, &module_def); 182 break; 183 } 184 string_type = STRING_NONE; 185 pop_state(); } 186 <cstring>. { } 187 188 189 190 (proc+{tos}+{name})|({tos}+proc+{tos}+{name}) { 191 char *proc = (char *)malloc(yyleng+1); 192 memset(proc, '\0', yyleng+1); 193 push_state(YYSTATE, pdef); 194 sscanf( yytext, "%*[^p]proc %s", proc); 195 if(strlen(proc)<1) sscanf( yytext, "proc %s", proc); 196 Add2proclist(&module_def, proc, "none", "NONE", NONE); 197 free(proc); 198 } 106 199 107 200 (proc+{tos}+{name}+{tos}+{name})|({tos}+proc+{tos}+{name}+{tos}+{name}) { 108 char proc[256], ret_val[256], n2[32]; 201 char *proc = (char *)malloc(yyleng+1); 202 char *ret_val = (char *)malloc(yyleng+1); 203 memset(proc, '\0', yyleng+1); 204 memset(ret_val, '\0', yyleng+1); 205 206 char n2[32]; 109 207 int cmd; 110 memset(proc, '\0', 256);111 memset(ret_val, '\0', 256);112 208 memset(n2, '\0', 32); 113 old_state = YYSTATE; 114 BEGIN(pdef); 209 push_state(YYSTATE, pdef); 115 210 sscanf( yytext, "%*[^p]proc %s %s", ret_val, proc); 116 211 if(strlen(proc)<1) sscanf( yytext, "proc %s %s", ret_val, proc); … … 121 216 Add2proclist(&module_def, proc, ret_val, 122 217 decl2str(tok,n2), tok); 123 else printf("proc '%s': Invalid return parameter %s.\n", 124 module_def.procs[module_def.proccnt-1].procname, 125 ret_val); 126 } 127 128 (proc+{tos}+{name})|({tos}+proc+{tos}+{name}) { 129 char proc[256]; 130 old_state = YYSTATE; 131 BEGIN(pdef); 132 proc[0]='\0'; 133 sscanf( yytext, "%*[^p]proc %s", proc); 134 if(strlen(proc)<1) sscanf( yytext, "proc %s", proc); 135 Add2proclist(&module_def, proc, "none", "NONE", 0); 218 else { 219 printf("proc '%s': Invalid return parameter %s.\n", 220 proc, ret_val); 221 Add2proclist(&module_def, proc, "none", 222 "NONE", NONE); 223 } 224 free(proc); 225 free(ret_val); 136 226 } 137 227 138 228 <pdef>[ \t] { } 139 <pdef>\( { 140 } 229 <pdef>"(" { } 141 230 <pdef>{name}+, { 142 231 char param[256], n2[32]; … … 152 241 param); 153 242 } 154 <pdef>{name}+\)+{eq}+{name} { 243 <pdef>{name}+")"+{tnl}+"{" { 244 char param[256], n2[32]; 245 int cmd; 246 BEGIN(procdef); 247 memset(param, '\0', 256); 248 memset(n2, '\0', 32); 249 sscanf( yytext, "%[^)])", param); 250 cmd = IsCmd(param, tok); 251 if(cmd!=0)AddParam(&module_def, 252 param, decl2str(tok,n2), tok); 253 else printf("proc '%s': Invalid parameter %s.\n", 254 module_def.procs[module_def.proccnt-1].procname, 255 param); 256 } 257 <pdef>")"+{tnl}+"{" { 258 BEGIN(procdef); 259 } 260 261 <pdef>"{" { 262 BEGIN(procdef); 263 } 264 265 <pdef>(")"+{eqnl}+{name})|({eq}+{name}) { 266 char funcname[256]; 267 pop_state(); 268 sscanf( yytext, "%*[^=]=%s", funcname); 269 if(strlen(funcname)<=0) 270 sscanf( yytext, "=%s", funcname); 271 free(module_def.procs[module_def.proccnt-1].funcname); 272 module_def.procs[module_def.proccnt-1].funcname = 273 (char *)malloc(strlen(funcname)+1); 274 memset(module_def.procs[module_def.proccnt-1].funcname, 275 '\0', strlen(funcname)+1); 276 memcpy(module_def.procs[module_def.proccnt-1].funcname, 277 funcname, strlen(funcname)); 278 } 279 280 <pdef>({name}+")"+{eqnl}+{name}) { 155 281 char param[256], n2[32],funcname[256]; 156 282 int cmd; 157 BEGIN(old_state);283 pop_state(); 158 284 memset(param, '\0', 256); 159 285 memset(n2, '\0', 32); … … 175 301 module_def.procs[module_def.proccnt-1].procname, param); 176 302 } 177 <pdef>\)+{eq}+{name}|{eq}+{tos}+{name} {178 char funcname[256];179 BEGIN(old_state);180 sscanf( yytext, "%*[^=]=%s", funcname);181 if(strlen(funcname)<=0)182 sscanf( yytext, "=%s", funcname);183 free(module_def.procs[module_def.proccnt-1].funcname);184 module_def.procs[module_def.proccnt-1].funcname =185 (char *)malloc(strlen(funcname)+1);186 memset(module_def.procs[module_def.proccnt-1].funcname,187 '\0', strlen(funcname)+1);188 memcpy(module_def.procs[module_def.proccnt-1].funcname,189 funcname, strlen(funcname));190 }191 303 192 304 <pdef>. { } 193 305 194 <comment>\*\/ { BEGIN(old_state); } 306 <procdef>"}" { pop_state(); } 307 <procdef>RETURN+{eqnl}+{name}+{col} { 308 printf("Return:\n");} 309 <procdef>function+{eqnl}+{name}+{col} { 310 char *funcname = (char *)malloc(yyleng+1); 311 memset(funcname, '\0', yyleng+1); 312 sscanf( yytext, "%*[^=]=%[^;];", funcname); 313 if(strlen(funcname)<=0) 314 sscanf( yytext, "=%[^;];", funcname); 315 free(module_def.procs[module_def.proccnt-1].funcname); 316 module_def.procs[module_def.proccnt-1].funcname = 317 (char *)malloc(strlen(funcname)+ 1); 318 memset(module_def.procs[module_def.proccnt-1].funcname, 319 '\0', strlen(funcname)+1); 320 memcpy(module_def.procs[module_def.proccnt-1].funcname, 321 funcname, strlen(funcname)); 322 } 323 324 <procdef>checkring+{col} { 325 printf("Do checkring\n"); } 326 <procdef>C+{eqnl}+"{" { 327 push_state(YYSTATE, ctext); 328 C_start=current_pos(yyleng); 329 yyless(yyleng-1); 330 c_codetype = C_CODE_PROC; 331 } 332 <procdef>. { printf("%s", yytext); } 333 334 <comment>\*\/ { pop_state(); } 195 335 <comment>\n { yylineno++; } 196 336 <comment>. { } 197 337 198 199 \n { yylineno++; } 200 \r { } 201 . { } 338 <ctext>({comment}[^\n]*) { } 339 <ctext>\/\/* { push_state(YYSTATE, comment); } 340 <ctext>{quote} { quote++; push_state(YYSTATE, string); } 341 <ctext>"{" { brace1++; } 342 <ctext>"}" { 343 brace1--; 344 if(brace2>0) { 345 yylp_errno = YYLP_BODY_BR2; 346 return(1); 347 } 348 if(brace3>0) { 349 yylp_errno = YYLP_BODY_BR3; 350 return(1); 351 } 352 if(brace1<=0) { 353 C_end = current_pos(yyleng)-1; 354 printf("Ctext: %d-%d\n", C_start, C_end); 355 switch(c_codetype) { 356 case C_CODE_PROC: 357 read_string(&(module_def.procs[module_def.proccnt-1].c_code), 358 &C_start, C_end); 359 break; 360 361 case C_CODE_MAIN: 362 break; 363 } 364 c_codetype = C_CODE_NONE; 365 pop_state(); 366 } 367 } 368 <ctext>"(" { brace2++; } 369 <ctext>")" { brace2--; 370 if(brace2<0) { 371 yylp_errno = YYLP_BODY_TMBR2; 372 return(1); 373 } 374 } 375 <ctext>"[" { brace3++; } 376 <ctext>"]" { brace3--; 377 if(brace3<0) { 378 yylp_errno = YYLP_BODY_TMBR3; 379 return(1); 380 } 381 } 382 <ctext>\n { yylineno++; } 383 <ctext>. { } 384 385 <string>{quote} { quote--; string_end = current_pos(yyleng)-1; pop_state(); } 386 <string>(\\\\)|(\\\") { } 387 <string>\n { yylplineno++; } 388 <string>. { } 389 390 391 C+{eqnl}+"{" { push_state(YYSTATE, ctext); 392 C_start=current_pos(yyleng); 393 yyless(yyleng-1); 394 } 395 \n { yylineno++; } 396 \r { } 397 . { } 202 398 203 399 %% … … 208 404 return 1; 209 405 } 406 } 407 408 int libread(FILE* f, char* buf, int max_size) 409 { int rc; 410 411 offset = ftell(f); 412 rc = fread( buf, 1, max_size, f ); 413 #if YYLPDEBUG >2 414 printf("fread: %d of %d\n", rc, max_size); 415 #endif 416 yylp_buffer_start = buf; 417 return rc; 418 } 419 420 int current_pos(int i) 421 { 422 return(i+offset+(int)(yytext-yylp_buffer_start)); 423 } 424 425 int read_string(char **p, long *start, long end) 426 { 427 char *buffer; 428 429 if(*start > 0 ) { 430 long len = end - *start; 431 if(len>=0) { 432 long cur = ftell(yyin); 433 buffer = (char *)malloc(len+1); 434 memset(buffer, '\0', len+1); 435 fseek(yyin, *start, SEEK_SET); 436 fread(buffer, 1, len, yyin); 437 fseek(yyin, cur, SEEK_SET); 438 *p = buffer; 439 } 440 *start = 0; 441 } 442 } 443 444 void push_state(int state, int new_state) 445 { 446 state_level++; 447 #if DEBUG 448 printf("====>PUSH to new state %d/%d l=%d\n", state, new_state, state_level); 449 #endif 450 if(state_level>=state_max) { 451 state_max++; 452 if(old_states == NULL) 453 old_states = (int *)malloc(sizeof(int)); 454 else { 455 old_states = (int *)realloc(old_states, state_max*sizeof(int)); 456 } 457 } 458 old_states[state_level] = state; 459 BEGIN(new_state); 460 } 461 462 void pop_state() 463 { 464 #if DEBUG 465 printf("====>Back to old state %d, l=%d\n", old_states[state_level], 466 state_level); 467 #endif 468 BEGIN(old_states[state_level]); 469 state_level--; 470 if(state_level<0) state_level = -1; 210 471 } 211 472
Note: See TracChangeset
for help on using the changeset viewer.