=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/Kan/scanner.c,v retrieving revision 1.3 retrieving revision 1.11 diff -u -p -r1.3 -r1.11 --- OpenXM/src/kan96xx/Kan/scanner.c 2000/03/20 01:53:46 1.3 +++ OpenXM/src/kan96xx/Kan/scanner.c 2020/10/06 11:33:47 1.11 @@ -1,10 +1,12 @@ -/*$OpenXM: OpenXM/src/kan96xx/Kan/scanner.c,v 1.2 1999/11/18 08:08:24 takayama Exp $*/ +/*$OpenXM: OpenXM/src/kan96xx/Kan/scanner.c,v 1.10 2018/09/07 00:15:44 takayama Exp $*/ /* scanner.c (SM StackMachine) */ /* export: struct tokens getokenSM(actionType kind,char *str); scanner.c is used to get tokens from streams. files: none */ #include +#include +#include #include "datatype.h" #include "stackm.h" struct tokens lookupTokens(struct tokens t); @@ -60,60 +62,77 @@ static int FileStackP = 0; /**************** end of declaration part of lexical analizer ******/ static int getSM(); -static putSM(); +static void putSM(); static struct tokens flushSM(); -static isSpaceSM(); -static isDollarSM(); -static isBraceSM(); -static isKakkoSM(); -static isSymbolSM(); -static mygetchar(); -static myungetchar(); +static int isSpaceSM(); +static int isDollarSM(); +static int isBraceSM(); +static int isKakkoSM(); +static int isSymbolSM(); +static int mygetchar(); +static int myungetchar(); +int ScannerWhich = 0; +unsigned char ScannerBuf[SCANNERBUF_SIZE]; +int ScannerPt = 0; /* static mygetchar() { + return( getc(Cfp) ); } */ -static mygetchar() +static int mygetchar() { int c; - if (EchoInScanner) { - c = getc(Cfp); - if (c==EOF) { - printf("\n%% EOF of file %x\n",(int) Cfp); - }else{ - printf("%c",c); - } - return( c ); - }else{ - return( getc(Cfp) ); + c = getc(Cfp); + if (c > 0) { /* ungetchar is ignored */ + ScannerPt++; if (ScannerPt >= SCANNERBUF_SIZE) ScannerPt = 0; + ScannerBuf[ScannerPt] = c; } + if (EchoInScanner) { + if (c==EOF) { + printf("\n%% EOF of file %p\n",Cfp); + }else{ + printf("%c",c); + } + return( c ); + }else{ + return( c ); + } } -static myungetchar(c) -int c; +static int myungetchar(c) + int c; { return( ungetc(c,Cfp) ); } /**************** code part of lexical analizer ********************/ static int getSM() -/* get a letter from StringSM */ + /* get a letter from StringSM */ { int c; + + if ((StrpSM > 0) && (StringSM[StrpSM] == ',') && (StringSM[StrpSM-1] == ',')) { int i; + fprintf(stderr,"Warning: ,, is found."); + for (i=(StrpSM-30>0?StrpSM-30:0); i<=StrpSM; i++) { + fprintf(stderr,"%c",StringSM[i]); + } + fprintf(stderr,"\n"); + } + c = StringSM[StrpSM++]; if (c == '\0') { StrpSM--;return(EOF); } else return(c); } -static putSM(c) -int c; -/* put a letter on BufSM */ +static void putSM(c) + int c; + /* put a letter on BufSM */ { char *new; int i; BufSM[ExistSM++] = ((c=='\n')? ' ' : c); @@ -146,6 +165,7 @@ static struct tokens flushSM() strcpy(token,BufSM); r.token = token; r.kind = TypeSM; + r.tflag = 0; if (r.kind == ID) { if (isLiteral(r.token)) { r.object = lookupLiteralString(r.token); @@ -156,36 +176,36 @@ static struct tokens flushSM() return(r); } -static isSpaceSM(c) -int c; +static int isSpaceSM(c) + int c; { - if ((c <= ' ') && (c!= EOF)) return(1); + if (((c <= ' ') || c == ',') && (c!= EOF)) return(1); else return(0); } -static isDollarSM(c) -int c; +static int isDollarSM(c) + int c; { if (c == '$') return(1); else return(0); } -static isBraceSM(c) -int c; +static int isBraceSM(c) + int c; { if (c == '{') return(1); else return(0); } -static isKakkoSM(c) -int c; +static int isKakkoSM(c) + int c; { if (c == '(') return(1); else return(0); } -static isSymbolSM(c) -int c; +static int isSymbolSM(c) + int c; { if ((c == '{') || (c == '}') || @@ -198,8 +218,8 @@ int c; } struct tokens getokenSM(kind,str) -actionType kind; -char *str; + actionType kind; + char *str; { static int c; static struct tokens rnull; @@ -207,6 +227,10 @@ char *str; char fname[1024]; if (kind == INIT) { + ScannerWhich = 2; + ScannerPt = 0; + ScannerBuf[0] = 0; + StrpSM = 0; ExistSM = 0; @@ -215,7 +239,7 @@ char *str; myungetchar('\n'); /* dummy */ c = mygetchar(); /* Notice that you need at least on input to return - from the getokenSM(INIT); ^^^^^^^^*/ + from the getokenSM(INIT); ^^^^^^^^*/ rnull.token = (char *)NULL; rnull.kind = -1; return(rnull); } @@ -234,35 +258,35 @@ char *str; }else { Cfp = fopen(str,"r"); if (EchoInScanner) - printf("\n%% I open the file %s.\n",str); + printf("\n%% I open the file %s.\n",str); if (Cfp == (FILE *)NULL) { - strcpy(fname,getLOAD_SM1_PATH()); - strcat(fname,str); - Cfp = fopen(fname,"r"); - if (Cfp == (FILE *)NULL) { - strcpy(fname,LOAD_SM1_PATH); - strcat(fname,str); - Cfp = fopen(fname,"r"); - if (Cfp == (FILE *)NULL) { - fprintf(stderr,"Warning: Cannot open the file <<%s>> for loading in the current directory nor the library directories %s and %s.\n",str,getLOAD_SM1_PATH(),LOAD_SM1_PATH); + strcpy(fname,getLOAD_SM1_PATH()); + strcat(fname,str); + Cfp = fopen(fname,"r"); + if (Cfp == (FILE *)NULL) { + strcpy(fname,LOAD_SM1_PATH); + strcat(fname,str); + Cfp = fopen(fname,"r"); + if (Cfp == (FILE *)NULL) { + fprintf(stderr,"Warning: Cannot open the file <<%s>> for loading in the current directory nor the library directories %s and %s.\n",str,getLOAD_SM1_PATH(),LOAD_SM1_PATH); - Cfp = FileStack[--FileStackP]; - fprintf(stderr,"\nWarning: I could not open the file %s\n",str); - c = mygetchar(); - rnull.kind = -3; - return(rnull); /* error */ - }else{ - c = mygetchar(); /* input from the new file */ - return(rnull); - } - }else{ - c = mygetchar(); /* input from the new file */ - return(rnull); - } + Cfp = FileStack[--FileStackP]; + fprintf(stderr,"\nWarning: I could not open the file %s\n",str); + c = mygetchar(); + rnull.kind = -3; + return(rnull); /* error */ + }else{ + c = mygetchar(); /* input from the new file */ + return(rnull); + } + }else{ + c = mygetchar(); /* input from the new file */ + return(rnull); + } }else{ - c = mygetchar(); /* input from the new file */ - return(rnull); + c = mygetchar(); /* input from the new file */ + return(rnull); } } } @@ -271,74 +295,74 @@ char *str; TypeSM = ID; if (c == EOF) { if (FileStackP <= 0) { - if (ExistSM) return(flushSM()); - else return(rnull); + if (ExistSM) return(flushSM()); + else return(rnull); }else { /* return to the previous file */ - fclose(Cfp); /* close the file */ - Cfp = FileStack[--FileStackP]; - c = mygetchar(Cfp); + fclose(Cfp); /* close the file */ + Cfp = FileStack[--FileStackP]; + c = mygetchar(Cfp); } } else if (isSpaceSM(c)) { if (ExistSM) { - c = mygetchar(); return(flushSM()); + c = mygetchar(); return(flushSM()); }else { - while (isSpaceSM(c=mygetchar())) ; + while (isSpaceSM(c=mygetchar())) ; } } else if (isDollarSM(c)) { /* output contents in dollar signs. */ if (ExistSM) return(flushSM()); else { - c = mygetchar(); - while ((c != EOF) && (c != '$')) { - putSM(c); - c = mygetchar(); - } - if (c=='$') c=mygetchar(); - TypeSM = DOLLAR; - return(flushSM()); + c = mygetchar(); + while ((c != EOF) && (c != '$')) { + putSM(c); + c = mygetchar(); + } + if (c=='$') c=mygetchar(); + TypeSM = DOLLAR; + return(flushSM()); } } else if (isBraceSM(c)) { /* output contents in { } */ /* { { } } */ level = 0; if (ExistSM) return(flushSM()); else { - c = mygetchar(); - while (1) { - if (c == '%') { /* skip the comment in the brace. */ - while (((c=mygetchar()) != '\n') && (c != EOF)) ; - } - if (c == EOF) break; - if ((c == '}') && (level <= 0)) break; - if ( c == '{') ++level; - if ( c == '}') --level; - putSM(c); - c = mygetchar(); - } - if (c=='}') c=mygetchar(); - TypeSM = EXECUTABLE_STRING; - return(flushSM()); + c = mygetchar(); + while (1) { + if (c == '%') { /* skip the comment in the brace. */ + while (((c=mygetchar()) != '\n') && (c != EOF)) ; + } + if (c == EOF) break; + if ((c == '}') && (level <= 0)) break; + if ( c == '{') ++level; + if ( c == '}') --level; + putSM(c); + c = mygetchar(); + } + if (c=='}') c=mygetchar(); + TypeSM = EXECUTABLE_STRING; + return(flushSM()); } } else if (isKakkoSM(c)) { /* output contents in ( ) */ level = 0; if (ExistSM) return(flushSM()); else { - c = mygetchar(); - while (1) { - if (c == EOF) break; - if (c == '\\') { /* e.g. \( */ - putSM(c); - c = mygetchar(); - if (c == EOF) break; - }else{ - if ((c == ')') && (level <= 0)) break; - if ( c == '(') ++level; - if ( c == ')') --level; - } - putSM(c); - c = mygetchar(); - } - if (c==')') c=mygetchar(); - TypeSM = DOLLAR; - return(flushSM()); + c = mygetchar(); + while (1) { + if (c == EOF) break; + if (c == '\\') { /* e.g. \( */ + putSM(c); + c = mygetchar(); + if (c == EOF) break; + }else{ + if ((c == ')') && (level <= 0)) break; + if ( c == '(') ++level; + if ( c == ')') --level; + } + putSM(c); + c = mygetchar(); + } + if (c==')') c=mygetchar(); + TypeSM = DOLLAR; + return(flushSM()); } } else if (c=='%') { /* comment */ while (((c=mygetchar()) != '\n') && (c != EOF)) ; @@ -346,19 +370,19 @@ char *str; } else if (isSymbolSM(c)) { /* symbols. {,} etc */ if(ExistSM) return(flushSM()); else { - putSM(c); - c = mygetchar(); - return(flushSM()); + putSM(c); + c = mygetchar(); + return(flushSM()); } } else { /* identifier */ putSM(c); c =mygetchar(); while ((!isDollarSM(c)) && - (!isSpaceSM(c)) && - (!isSymbolSM(c)) && - (c != EOF)) { - putSM(c); - c = mygetchar(); + (!isSpaceSM(c)) && + (!isSymbolSM(c)) && + (c != EOF)) { + putSM(c); + c = mygetchar(); } return(flushSM()); } @@ -392,7 +416,7 @@ main() { char *getLOAD_SM1_PATH() { char *p; char *p2; - char *getenv(char *s); + char *getenv(const char *s); p = getenv("LOAD_SM1_PATH"); if (p == NULL) { p = getenv("OpenXM_HOME"); @@ -403,9 +427,9 @@ char *getLOAD_SM1_PATH() { p2 = (char *) sGC_malloc(sizeof(char)*(strlen(p)+strlen("/lib/sm1/")+3)); if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); } if (p[strlen(p)-1] != '/') { - strcpy(p2,p); strcat(p2,"/lib/sm1/"); + strcpy(p2,p); strcat(p2,"/lib/sm1/"); }else{ - strcpy(p2,p); strcat(p2,"lib/sm1/"); + strcpy(p2,p); strcat(p2,"lib/sm1/"); } return(p2); } @@ -418,4 +442,39 @@ char *getLOAD_SM1_PATH() { strcpy(p2,p); strcat(p2,"/"); return(p2); } +} + +char *traceShowScannerBuf() { + char *s; + int i,k; + s = NULL; + /* + printf("ScannerPt=%d\n",ScannerPt); + for (i=0; i 0) && (ScannerBuf[0] == 0)) { + s = sGC_malloc(ScannerPt+1); + for (i=1; i<=ScannerPt; i++) { + s[i-1] = ScannerBuf[i]; s[i] = 0; + } + return s; + } + if (ScannerBuf[0] != 0) { + s = sGC_malloc(SCANNERBUF_SIZE+1); + k = ScannerPt+1; + if (k >= SCANNERBUF_SIZE) k = 0; + for (i=0; i= SCANNERBUF_SIZE) k = 0; + } + return s; + } + return s; }