version 1.2, 2000/01/16 07:55:41 |
version 1.3, 2001/05/04 01:06:25 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/kan96xx/Kan/scanner2.c,v 1.2 2000/01/16 07:55:41 takayama Exp $ */ |
/* scanner2.c (SM StackMachine) */ |
/* scanner2.c (SM StackMachine) */ |
/* export: struct tokens decompostToTokens(char *str,int *sizep); |
/* export: struct tokens decompostToTokens(char *str,int *sizep); |
scanner2.c is for getting tokens from a string. |
scanner2.c is for getting tokens from a string. |
Line 10 struct tokens lookupTokens(struct tokens t); |
|
Line 10 struct tokens lookupTokens(struct tokens t); |
|
int isLiteral(char *s); |
int isLiteral(char *s); |
struct object lookupLiteralString(char *s); |
struct object lookupLiteralString(char *s); |
/**************** defined in stackm.h **************************** |
/**************** defined in stackm.h **************************** |
typedef enum {INIT,GET,PUT,OPEN} actionType; |
typedef enum {INIT,GET,PUT,OPEN} actionType; |
|
|
struct tokens{ |
struct tokens{ |
char *token; |
char *token; |
int kind; |
int kind; |
}; |
}; |
|
|
|
|
#define ID 2 |
#define ID 2 |
#define DOLLAR 3 strings enclosed by dollar sign |
#define DOLLAR 3 strings enclosed by dollar sign |
#define EXECUTABLE_STRING 4 strings enclosed by {} |
#define EXECUTABLE_STRING 4 strings enclosed by {} |
#define EXECUTABLE_ARRAY 8 Don't set it in this file. |
#define EXECUTABLE_ARRAY 8 Don't set it in this file. |
******************************************************************/ |
******************************************************************/ |
|
|
|
|
Line 59 static struct tokens getokenSM2(); |
|
Line 59 static struct tokens getokenSM2(); |
|
/**************** code part of lexical analizer ********************/ |
/**************** code part of lexical analizer ********************/ |
|
|
struct tokens *decomposeToTokens(str,sizep) |
struct tokens *decomposeToTokens(str,sizep) |
char *str; |
char *str; |
int *sizep; |
int *sizep; |
{ |
{ |
struct tokens *tArray; |
struct tokens *tArray; |
struct tokens token; |
struct tokens token; |
|
|
|
|
|
|
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 104 static int getSM() |
|
Line 104 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 140 static struct tokens flushSM() |
|
Line 140 static struct tokens flushSM() |
|
r.kind = TypeSM; |
r.kind = TypeSM; |
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); |
}else{ |
}else{ |
r = lookupTokens(r); /* Compute hashing values */ |
r = lookupTokens(r); /* Compute hashing values */ |
} |
} |
Line 149 static struct tokens flushSM() |
|
Line 149 static struct tokens flushSM() |
|
} |
} |
|
|
static isSpaceSM(c) |
static isSpaceSM(c) |
int c; |
int c; |
{ |
{ |
if ((c <= ' ') && (c!= EOF)) return(1); |
if ((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 == '}') || |
|
|
} |
} |
|
|
static struct tokens getokenSM2(kind,str) |
static struct tokens getokenSM2(kind,str) |
actionType kind; |
actionType kind; |
char *str; |
char *str; |
{ |
{ |
static int c; |
static int c; |
static struct tokens rnull; |
static struct tokens rnull; |
|
|
else return(rnull); |
else return(rnull); |
} 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()); |
} |
} |
|
|
|
|
|
|
errorScanner2(str) |
errorScanner2(str) |
char *str; |
char *str; |
{ |
{ |
fprintf(stderr,"Error (scanner2.c): %s\n",str); |
fprintf(stderr,"Error (scanner2.c): %s\n",str); |
exit(10); |
exit(10); |