version 1.1, 1999/10/08 02:12:01 |
version 1.9, 2005/07/18 10:55:16 |
|
|
|
/*$OpenXM: OpenXM/src/kan96xx/Kan/scanner.c,v 1.8 2005/07/03 11:08:54 ohara Exp $*/ |
/* scanner.c (SM StackMachine) */ |
/* scanner.c (SM StackMachine) */ |
/* export: struct tokens getokenSM(actionType kind,char *str); |
/* export: struct tokens getokenSM(actionType kind,char *str); |
scanner.c is used to get tokens from streams. |
scanner.c is used to get tokens from streams. |
files: none |
files: none |
*/ |
*/ |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
#include "datatype.h" |
#include "datatype.h" |
#include "stackm.h" |
#include "stackm.h" |
struct tokens lookupTokens(struct tokens t); |
struct tokens lookupTokens(struct tokens t); |
|
|
|
|
|
|
|
|
FILE *BaseFp = stdin; /* file pointer of the first file. */ |
FILE *BaseFp = NULL; /* Initialized to stdin in stackmachine_init(). |
|
file pointer of the first file. */ |
|
|
int EchoInScanner = 0; /* echo in scanner */ |
int EchoInScanner = 0; /* echo in scanner */ |
|
|
Line 68 static isSymbolSM(); |
|
Line 72 static isSymbolSM(); |
|
static mygetchar(); |
static mygetchar(); |
static myungetchar(); |
static myungetchar(); |
|
|
|
int ScannerWhich = 0; |
|
unsigned char ScannerBuf[SCANNERBUF_SIZE]; |
|
int ScannerPt = 0; |
|
|
/* |
/* |
static mygetchar() |
static mygetchar() |
{ |
{ |
|
|
return( getc(Cfp) ); |
return( getc(Cfp) ); |
} |
} |
*/ |
*/ |
|
|
static mygetchar() |
static mygetchar() |
{ int c; |
{ int c; |
if (EchoInScanner) { |
c = getc(Cfp); |
c = getc(Cfp); |
if (c > 0) { /* ungetchar is ignored */ |
if (c==EOF) { |
ScannerPt++; if (ScannerPt >= SCANNERBUF_SIZE) ScannerPt = 0; |
printf("\n%% EOF of file %x\n",(int) Cfp); |
ScannerBuf[ScannerPt] = c; |
}else{ |
|
printf("%c",c); |
|
} |
|
return( c ); |
|
}else{ |
|
return( getc(Cfp) ); |
|
} |
} |
|
if (EchoInScanner) { |
|
if (c==EOF) { |
|
printf("\n%% EOF of file %x\n",(int) Cfp); |
|
}else{ |
|
printf("%c",c); |
|
} |
|
return( c ); |
|
}else{ |
|
return( c ); |
|
} |
} |
} |
|
|
|
|
static myungetchar(c) |
static myungetchar(c) |
int c; |
int c; |
{ |
{ |
return( ungetc(c,Cfp) ); |
return( ungetc(c,Cfp) ); |
} |
} |
|
|
/**************** code part of lexical analizer ********************/ |
/**************** code part of lexical analizer ********************/ |
static int getSM() |
static int getSM() |
/* get a letter from StringSM */ |
/* get a letter from StringSM */ |
{ |
{ |
int c; |
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++]; |
c = StringSM[StrpSM++]; |
if (c == '\0') { |
if (c == '\0') { |
StrpSM--;return(EOF); |
StrpSM--;return(EOF); |
Line 110 static int getSM() |
|
Line 131 static int getSM() |
|
} |
} |
|
|
static putSM(c) |
static putSM(c) |
int c; |
int c; |
/* put a letter on BufSM */ |
/* put a letter on BufSM */ |
{ |
{ |
char *new; int i; |
char *new; int i; |
BufSM[ExistSM++] = ((c=='\n')? ' ' : c); |
BufSM[ExistSM++] = ((c=='\n')? ' ' : c); |
Line 144 static struct tokens flushSM() |
|
Line 165 static struct tokens flushSM() |
|
strcpy(token,BufSM); |
strcpy(token,BufSM); |
r.token = token; |
r.token = token; |
r.kind = TypeSM; |
r.kind = TypeSM; |
|
r.tflag = 0; |
if (r.kind == ID) { |
if (r.kind == ID) { |
if (isLiteral(r.token)) { |
if (isLiteral(r.token)) { |
r.object = lookupLiteralString(r.token); |
r.object = lookupLiteralString(r.token); |
Line 155 static struct tokens flushSM() |
|
Line 177 static struct tokens flushSM() |
|
} |
} |
|
|
static isSpaceSM(c) |
static isSpaceSM(c) |
int c; |
int c; |
{ |
{ |
if ((c <= ' ') && (c!= EOF)) return(1); |
if (((c <= ' ') || c == ',') && (c!= EOF)) return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isDollarSM(c) |
static isDollarSM(c) |
int c; |
int c; |
{ |
{ |
if (c == '$') return(1); |
if (c == '$') return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isBraceSM(c) |
static isBraceSM(c) |
int c; |
int c; |
{ |
{ |
if (c == '{') return(1); |
if (c == '{') return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isKakkoSM(c) |
static isKakkoSM(c) |
int c; |
int c; |
{ |
{ |
if (c == '(') return(1); |
if (c == '(') return(1); |
else return(0); |
else return(0); |
} |
} |
|
|
static isSymbolSM(c) |
static isSymbolSM(c) |
int c; |
int c; |
{ |
{ |
if ((c == '{') || |
if ((c == '{') || |
(c == '}') || |
(c == '}') || |
|
|
} |
} |
|
|
struct tokens getokenSM(kind,str) |
struct tokens getokenSM(kind,str) |
actionType kind; |
actionType kind; |
char *str; |
char *str; |
{ |
{ |
static int c; |
static int c; |
static struct tokens rnull; |
static struct tokens rnull; |
|
|
char fname[1024]; |
char fname[1024]; |
|
|
if (kind == INIT) { |
if (kind == INIT) { |
|
ScannerWhich = 2; |
|
ScannerPt = 0; |
|
ScannerBuf[0] = 0; |
|
|
StrpSM = 0; |
StrpSM = 0; |
ExistSM = 0; |
ExistSM = 0; |
|
|
|
|
|
|
myungetchar('\n'); /* dummy */ |
myungetchar('\n'); /* dummy */ |
c = mygetchar(); /* Notice that you need at least on input to return |
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; |
rnull.token = (char *)NULL; rnull.kind = -1; |
return(rnull); |
return(rnull); |
} |
} |
|
|
}else { |
}else { |
Cfp = fopen(str,"r"); |
Cfp = fopen(str,"r"); |
if (EchoInScanner) |
if (EchoInScanner) |
printf("\n%% I open the file %s.\n",str); |
printf("\n%% I open the file %s.\n",str); |
if (Cfp == (FILE *)NULL) { |
if (Cfp == (FILE *)NULL) { |
|
|
strcpy(fname,getLOAD_SM1_PATH()); |
strcpy(fname,getLOAD_SM1_PATH()); |
strcat(fname,str); |
strcat(fname,str); |
Cfp = fopen(fname,"r"); |
Cfp = fopen(fname,"r"); |
if (Cfp == (FILE *)NULL) { |
if (Cfp == (FILE *)NULL) { |
strcpy(fname,LOAD_SM1_PATH); |
strcpy(fname,LOAD_SM1_PATH); |
strcat(fname,str); |
strcat(fname,str); |
Cfp = fopen(fname,"r"); |
Cfp = fopen(fname,"r"); |
if (Cfp == (FILE *)NULL) { |
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); |
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]; |
Cfp = FileStack[--FileStackP]; |
fprintf(stderr,"\nWarning: I could not open the file %s\n",str); |
fprintf(stderr,"\nWarning: I could not open the file %s\n",str); |
c = mygetchar(); |
c = mygetchar(); |
rnull.kind = -3; |
rnull.kind = -3; |
return(rnull); /* error */ |
return(rnull); /* error */ |
}else{ |
}else{ |
c = mygetchar(); /* input from the new file */ |
c = mygetchar(); /* input from the new file */ |
return(rnull); |
return(rnull); |
} |
} |
}else{ |
}else{ |
c = mygetchar(); /* input from the new file */ |
c = mygetchar(); /* input from the new file */ |
return(rnull); |
return(rnull); |
} |
} |
}else{ |
}else{ |
c = mygetchar(); /* input from the new file */ |
c = mygetchar(); /* input from the new file */ |
return(rnull); |
return(rnull); |
} |
} |
} |
} |
} |
} |
|
|
TypeSM = ID; |
TypeSM = ID; |
if (c == EOF) { |
if (c == EOF) { |
if (FileStackP <= 0) { |
if (FileStackP <= 0) { |
if (ExistSM) return(flushSM()); |
if (ExistSM) return(flushSM()); |
else return(rnull); |
else return(rnull); |
}else { /* return to the previous file */ |
}else { /* return to the previous file */ |
fclose(Cfp); /* close the file */ |
fclose(Cfp); /* close the file */ |
Cfp = FileStack[--FileStackP]; |
Cfp = FileStack[--FileStackP]; |
c = mygetchar(Cfp); |
c = mygetchar(Cfp); |
} |
} |
} else if (isSpaceSM(c)) { |
} else if (isSpaceSM(c)) { |
if (ExistSM) { |
if (ExistSM) { |
c = mygetchar(); return(flushSM()); |
c = mygetchar(); return(flushSM()); |
}else { |
}else { |
while (isSpaceSM(c=mygetchar())) ; |
while (isSpaceSM(c=mygetchar())) ; |
} |
} |
} else if (isDollarSM(c)) { /* output contents in dollar signs. */ |
} else if (isDollarSM(c)) { /* output contents in dollar signs. */ |
if (ExistSM) return(flushSM()); |
if (ExistSM) return(flushSM()); |
else { |
else { |
c = mygetchar(); |
c = mygetchar(); |
while ((c != EOF) && (c != '$')) { |
while ((c != EOF) && (c != '$')) { |
putSM(c); |
putSM(c); |
c = mygetchar(); |
c = mygetchar(); |
} |
} |
if (c=='$') c=mygetchar(); |
if (c=='$') c=mygetchar(); |
TypeSM = DOLLAR; |
TypeSM = DOLLAR; |
return(flushSM()); |
return(flushSM()); |
} |
} |
} else if (isBraceSM(c)) { /* output contents in { } */ |
} else if (isBraceSM(c)) { /* output contents in { } */ |
/* { { } } */ |
/* { { } } */ |
level = 0; |
level = 0; |
if (ExistSM) return(flushSM()); |
if (ExistSM) return(flushSM()); |
else { |
else { |
c = mygetchar(); |
c = mygetchar(); |
while (1) { |
while (1) { |
if (c == '%') { /* skip the comment in the brace. */ |
if (c == '%') { /* skip the comment in the brace. */ |
while (((c=mygetchar()) != '\n') && (c != EOF)) ; |
while (((c=mygetchar()) != '\n') && (c != EOF)) ; |
} |
} |
if (c == EOF) break; |
if (c == EOF) break; |
if ((c == '}') && (level <= 0)) break; |
if ((c == '}') && (level <= 0)) break; |
if ( c == '{') ++level; |
if ( c == '{') ++level; |
if ( c == '}') --level; |
if ( c == '}') --level; |
putSM(c); |
putSM(c); |
c = mygetchar(); |
c = mygetchar(); |
} |
} |
if (c=='}') c=mygetchar(); |
if (c=='}') c=mygetchar(); |
TypeSM = EXECUTABLE_STRING; |
TypeSM = EXECUTABLE_STRING; |
return(flushSM()); |
return(flushSM()); |
} |
} |
} else if (isKakkoSM(c)) { /* output contents in ( ) */ |
} else if (isKakkoSM(c)) { /* output contents in ( ) */ |
level = 0; |
level = 0; |
if (ExistSM) return(flushSM()); |
if (ExistSM) return(flushSM()); |
else { |
else { |
c = mygetchar(); |
c = mygetchar(); |
while (1) { |
while (1) { |
if (c == EOF) break; |
if (c == EOF) break; |
if (c == '\\') { /* e.g. \( */ |
if (c == '\\') { /* e.g. \( */ |
putSM(c); |
putSM(c); |
c = mygetchar(); |
c = mygetchar(); |
if (c == EOF) break; |
if (c == EOF) break; |
}else{ |
}else{ |
if ((c == ')') && (level <= 0)) break; |
if ((c == ')') && (level <= 0)) break; |
if ( c == '(') ++level; |
if ( c == '(') ++level; |
if ( c == ')') --level; |
if ( c == ')') --level; |
} |
} |
putSM(c); |
putSM(c); |
c = mygetchar(); |
c = mygetchar(); |
} |
} |
if (c==')') c=mygetchar(); |
if (c==')') c=mygetchar(); |
TypeSM = DOLLAR; |
TypeSM = DOLLAR; |
return(flushSM()); |
return(flushSM()); |
} |
} |
} else if (c=='%') { /* comment */ |
} else if (c=='%') { /* comment */ |
while (((c=mygetchar()) != '\n') && (c != EOF)) ; |
while (((c=mygetchar()) != '\n') && (c != EOF)) ; |
|
|
} else if (isSymbolSM(c)) { /* symbols. {,} etc */ |
} else if (isSymbolSM(c)) { /* symbols. {,} etc */ |
if(ExistSM) return(flushSM()); |
if(ExistSM) return(flushSM()); |
else { |
else { |
putSM(c); |
putSM(c); |
c = mygetchar(); |
c = mygetchar(); |
return(flushSM()); |
return(flushSM()); |
} |
} |
} else { /* identifier */ |
} else { /* identifier */ |
putSM(c); |
putSM(c); |
c =mygetchar(); |
c =mygetchar(); |
while ((!isDollarSM(c)) && |
while ((!isDollarSM(c)) && |
(!isSpaceSM(c)) && |
(!isSpaceSM(c)) && |
(!isSymbolSM(c)) && |
(!isSymbolSM(c)) && |
(c != EOF)) { |
(c != EOF)) { |
putSM(c); |
putSM(c); |
c = mygetchar(); |
c = mygetchar(); |
} |
} |
return(flushSM()); |
return(flushSM()); |
} |
} |
|
|
char *getLOAD_SM1_PATH() { |
char *getLOAD_SM1_PATH() { |
char *p; |
char *p; |
char *p2; |
char *p2; |
char *getenv(char *s); |
char *getenv(const char *s); |
p = getenv("LOAD_SM1_PATH"); |
p = getenv("LOAD_SM1_PATH"); |
if (p == NULL) { |
if (p == NULL) { |
return("/usr/local/lib/sm1/"); |
p = getenv("OpenXM_HOME"); |
|
if (p == NULL) { |
|
return("/usr/local/lib/sm1/"); |
|
}else{ |
|
if (strlen(p) == 0) return(p); |
|
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/"); |
|
}else{ |
|
strcpy(p2,p); strcat(p2,"lib/sm1/"); |
|
} |
|
return(p2); |
|
} |
}else{ |
}else{ |
if (strlen(p) == 0) return(p); |
if (strlen(p) == 0) return(p); |
if (p[strlen(p)-1] == '/') return(p); |
if (p[strlen(p)-1] == '/') return(p); |
Line 403 char *getLOAD_SM1_PATH() { |
|
Line 442 char *getLOAD_SM1_PATH() { |
|
strcpy(p2,p); strcat(p2,"/"); |
strcpy(p2,p); strcat(p2,"/"); |
return(p2); |
return(p2); |
} |
} |
|
} |
|
|
|
char *traceShowScannerBuf() { |
|
char *s; |
|
int i,k; |
|
s = NULL; |
|
/* |
|
printf("ScannerPt=%d\n",ScannerPt); |
|
for (i=0; i<SCANNERBUF_SIZE; i++) { |
|
printf("%x ",(int) (ScannerBuf[i])); |
|
} |
|
printf("\n"); fflush(NULL); sleep(10); |
|
*/ |
|
if ((ScannerPt == 0) && (ScannerBuf[0] == 0)) { |
|
s = sGC_malloc(1); s[0] = 0; |
|
return s; |
|
} |
|
if ((ScannerPt > 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; i++) { |
|
s[i] = ScannerBuf[k]; s[i+1] = 0; |
|
k++; if (k >= SCANNERBUF_SIZE) k = 0; |
|
} |
|
return s; |
|
} |
|
return s; |
} |
} |