version 1.1, 1999/10/08 02:12:01 |
version 1.9, 2005/07/18 10:55:16 |
|
|
|
/* $OpenXM: OpenXM/src/kan96xx/Kan/scanner2.c,v 1.8 2005/07/03 11:08:54 ohara 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. |
*/ |
*/ |
#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); |
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 55 static isKakkoSM(); |
|
Line 58 static isKakkoSM(); |
|
static isSymbolSM(); |
static isSymbolSM(); |
static struct tokens getokenSM2(); |
static struct tokens getokenSM2(); |
|
|
|
extern int ScannerWhich; |
|
extern unsigned char ScannerBuf[]; |
|
extern int ScannerPt; |
|
|
/**************** 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++]; |
|
|
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 = (unsigned char) StringSM[StrpSM++]; |
|
if (c != 0) { |
|
ScannerPt++; if (ScannerPt >= SCANNERBUF_SIZE) ScannerPt = 0; |
|
ScannerBuf[ScannerPt] = c; |
|
} |
if (c == '\0') { |
if (c == '\0') { |
StrpSM--;return(EOF); |
StrpSM--;return(EOF); |
} else return(c); |
} else return(c); |
} |
} |
|
|
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 137 static struct tokens flushSM() |
|
Line 157 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); |
}else{ |
}else{ |
r = lookupTokens(r); /* Compute hashing values */ |
r = lookupTokens(r); /* Compute hashing values */ |
} |
} |
Line 148 static struct tokens flushSM() |
|
Line 169 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 == '}') || |
|
|
} |
} |
|
|
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; |
int level; |
int level; |
|
|
if (kind == INIT) { |
if (kind == INIT) { |
|
ScannerWhich = 2; |
|
ScannerPt = 0; |
|
ScannerBuf[0] = 0; |
|
|
StrpSM = 0; |
StrpSM = 0; |
ExistSM = 0; |
ExistSM = 0; |
|
|
|
|
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); |