| version 1.3, 2000/03/20 01:53:46 |
version 1.5, 2004/09/10 13:20:23 |
|
|
| /*$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.4 2001/05/04 01:06:25 takayama 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. |
| Line 80 static mygetchar() |
|
| Line 80 static mygetchar() |
|
| |
|
| static mygetchar() |
static mygetchar() |
| { int c; |
{ int c; |
| if (EchoInScanner) { |
if (EchoInScanner) { |
| c = getc(Cfp); |
c = getc(Cfp); |
| if (c==EOF) { |
if (c==EOF) { |
| printf("\n%% EOF of file %x\n",(int) Cfp); |
printf("\n%% EOF of file %x\n",(int) Cfp); |
| }else{ |
}else{ |
| printf("%c",c); |
printf("%c",c); |
| } |
} |
| return( c ); |
return( c ); |
| }else{ |
}else{ |
| return( getc(Cfp) ); |
return( getc(Cfp) ); |
| } |
} |
| } |
} |
| |
|
| |
|
| 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; |
| c = StringSM[StrpSM++]; |
c = StringSM[StrpSM++]; |
| Line 112 static int getSM() |
|
| Line 112 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 157 static struct tokens flushSM() |
|
| Line 157 static struct tokens flushSM() |
|
| } |
} |
| |
|
| static isSpaceSM(c) |
static isSpaceSM(c) |
| int c; |
int c; |
| { |
{ |
| if ((c <= ' ') && (c!= EOF)) return(1); |
static int prev=0; |
| |
if ((c == ',') && (prev == ',')) fprintf(stderr,"Warning! ,, is found.\n"); |
| |
prev = c; |
| |
|
| |
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; |
|
|
| |
|
| 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()); |
| } |
} |
| Line 403 char *getLOAD_SM1_PATH() { |
|
| Line 407 char *getLOAD_SM1_PATH() { |
|
| p2 = (char *) sGC_malloc(sizeof(char)*(strlen(p)+strlen("/lib/sm1/")+3)); |
p2 = (char *) sGC_malloc(sizeof(char)*(strlen(p)+strlen("/lib/sm1/")+3)); |
| if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); } |
if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); } |
| if (p[strlen(p)-1] != '/') { |
if (p[strlen(p)-1] != '/') { |
| strcpy(p2,p); strcat(p2,"/lib/sm1/"); |
strcpy(p2,p); strcat(p2,"/lib/sm1/"); |
| }else{ |
}else{ |
| strcpy(p2,p); strcat(p2,"lib/sm1/"); |
strcpy(p2,p); strcat(p2,"lib/sm1/"); |
| } |
} |
| return(p2); |
return(p2); |
| } |
} |