| version 1.6, 1999/11/04 18:15:20 |
version 1.8, 1999/11/07 12:12:55 |
|
|
| /* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
| /* $OpenXM: OpenXM/src/ox_math/parse.c,v 1.5 1999/11/03 10:56:40 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_math/parse.c,v 1.7 1999/11/06 21:39:37 ohara Exp $ */ |
| |
|
| /* OX expression, CMO expression パーサ */ |
/* OX expression, CMO expression パーサ */ |
| |
|
| Line 88 static jmp_buf env_parse; |
|
| Line 88 static jmp_buf env_parse; |
|
| /* 構文解析に失敗したことを意味する. */ |
/* 構文解析に失敗したことを意味する. */ |
| static int parse_error(char *s) |
static int parse_error(char *s) |
| { |
{ |
| fprintf(stderr, "syntax error: %s\n", s); |
fprintf(stderr, "syntax error: %s\n", s); |
| longjmp(env_parse, 1); |
longjmp(env_parse, 1); |
| } |
} |
| |
|
| /* この部分は書き換え予定. */ |
/* この部分は書き換え予定. */ |
|
|
| { |
{ |
| cmo *m; |
cmo *m; |
| |
|
| if (setjmp(env_parse) != 0) { |
if (setjmp(env_parse) != 0) { |
| return NULL; /* 構文解析に失敗したら NULL を返す. */ |
return NULL; /* 構文解析に失敗したら NULL を返す. */ |
| } |
} |
| |
|
| do{ |
do{ |
| token = lex(); |
token = lex(); |
| Line 323 static cmo *parse_cmo_string() |
|
| Line 323 static cmo *parse_cmo_string() |
|
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| parse_error("invalid cmo string."); |
parse_error("invalid cmo string."); |
| } |
} |
| s = parse_string(); |
s = parse_string(); |
| m = new_cmo_string(s); |
m = new_cmo_string(s); |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)m; |
return (cmo *)m; |
| Line 331 static cmo *parse_cmo_string() |
|
| Line 331 static cmo *parse_cmo_string() |
|
| |
|
| static cmo *parse_cmo_mathcap() |
static cmo *parse_cmo_mathcap() |
| { |
{ |
| cmo *ob; |
cmo *ob; |
| |
|
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| ob = parse_cmo(); |
ob = parse_cmo(); |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)new_cmo_mathcap(ob); |
return (cmo *)new_cmo_mathcap(ob); |
| } |
} |
| Line 347 static cmo *parse_cmo_list() |
|
| Line 347 static cmo *parse_cmo_list() |
|
| cmo_list *m = new_cmo_list(); |
cmo_list *m = new_cmo_list(); |
| cmo *newcmo; |
cmo *newcmo; |
| |
|
| if (token == ',') { |
if (token == ',') { |
| parse_comma(); |
parse_comma(); |
| |
|
| if (token == T_INTEGER) { |
if (token == T_INTEGER) { |
| parse_integer(); |
parse_integer(); |
| parse_comma(); |
parse_comma(); |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| parse_error("invalid cmo_list."); |
parse_error("invalid cmo_list."); |
| } |
} |
| |
|
| while(token == '(') { |
while(token == '(') { |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| newcmo = parse_cmo(); |
newcmo = parse_cmo(); |
| append_cmo_list(m, newcmo); |
append_cmo_list(m, newcmo); |
| if (token != ',') { |
if (token != ',') { |
| break; |
break; |
| } |
} |
| parse_comma(); |
parse_comma(); |
| } |
} |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| parse_error("invalid cmo_list."); |
parse_error("invalid cmo_list."); |
| } |
} |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)m; |
return (cmo *)m; |
| } |
} |
| |
|
| static cmo *parse_cmo_monomial32() |
static cmo *parse_cmo_monomial32() |
| { |
{ |
| int size; |
int size; |
| int *exps; |
int *exps; |
| int i; |
int i; |
| cmo_monomial32 *m; |
cmo_monomial32 *m; |
| int tag; |
int tag; |
| |
|
| parse_comma(); |
parse_comma(); |
| size = parse_integer(); |
size = parse_integer(); |
| if (size < 0) { |
if (size < 0) { |
| parse_error("invalid value."); |
parse_error("invalid value."); |
| } |
} |
| m = new_cmo_monomial32_size(size); |
m = new_cmo_monomial32_size(size); |
| |
|
| for(i=0; i<size; i++) { |
for(i=0; i<size; i++) { |
| parse_comma(); |
parse_comma(); |
| m->exps[i] = parse_integer(); |
m->exps[i] = parse_integer(); |
| } |
} |
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| m->coef = parse_cmo(); |
m->coef = parse_cmo(); |
| tag = m->coef->tag; |
tag = m->coef->tag; |
| |
|
| /* m->coef は CMO_ZZ 型か CMO_INT32 型でなければならない */ |
/* m->coef は CMO_ZZ 型か CMO_INT32 型でなければならない */ |
| if (tag != CMO_ZZ && tag != CMO_INT32) { |
if (tag != CMO_ZZ && tag != CMO_INT32) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| } |
} |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)m; |
return (cmo *)m; |
| } |
} |
| Line 446 static cmo *parse_cmo_dms_generic() |
|
| Line 446 static cmo *parse_cmo_dms_generic() |
|
| |
|
| static cmo *parse_cmo_ring_by_name() |
static cmo *parse_cmo_ring_by_name() |
| { |
{ |
| cmo *ob; |
cmo *ob; |
| |
|
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| ob = parse_cmo(); |
ob = parse_cmo(); |
| |
|
| /* ob は CMO_STRING 型でなければならない */ |
/* ob は CMO_STRING 型でなければならない */ |
| if (ob->tag != CMO_STRING) { |
if (ob->tag != CMO_STRING) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| } |
} |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)new_cmo_ring_by_name(ob); |
return (cmo *)new_cmo_ring_by_name(ob); |
| } |
} |
| Line 466 static cmo *parse_cmo_distributed_polynomial() |
|
| Line 466 static cmo *parse_cmo_distributed_polynomial() |
|
| int i=0; |
int i=0; |
| cmo_distributed_polynomial *m = new_cmo_distributed_polynomial(); |
cmo_distributed_polynomial *m = new_cmo_distributed_polynomial(); |
| cmo *ob; |
cmo *ob; |
| int tag; |
int tag; |
| |
|
| if (token == ',') { |
if (token == ',') { |
| parse_comma(); |
parse_comma(); |
| |
|
| if (token == T_INTEGER) { |
if (token == T_INTEGER) { |
| parse_integer(); |
parse_integer(); |
| parse_comma(); |
parse_comma(); |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| parse_error("invalid d-polynomial."); |
parse_error("invalid d-polynomial."); |
| } |
} |
| |
|
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| m->ringdef = parse_cmo(); |
m->ringdef = parse_cmo(); |
| tag = m->ringdef->tag; |
tag = m->ringdef->tag; |
| /* m->ringdef は DringDefinition でなければならない */ |
/* m->ringdef は DringDefinition でなければならない */ |
| if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC |
if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC |
| && tag != CMO_DMS_OF_N_VARIABLES) { |
&& tag != CMO_DMS_OF_N_VARIABLES) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| } |
} |
| |
|
| parse_comma(); |
parse_comma(); |
| |
|
| while(token == '(') { |
while(token == '(') { |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| ob = parse_cmo(); |
ob = parse_cmo(); |
| if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { |
if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| } |
} |
| append_cmo_list((cmo_list *)m, ob); |
append_cmo_list((cmo_list *)m, ob); |
| if (token != ',') { |
if (token != ',') { |
| break; |
break; |
| } |
} |
| parse_comma(); |
parse_comma(); |
| } |
} |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| parse_error("invalid d-polynomial."); |
parse_error("invalid d-polynomial."); |
| } |
} |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)m; |
return (cmo *)m; |
| } |
} |
| |
|
| static cmo *parse_cmo_indeterminate() |
static cmo *parse_cmo_indeterminate() |
| { |
{ |
| cmo *ob; |
cmo *ob; |
| |
|
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| ob = parse_cmo(); |
ob = parse_cmo(); |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)new_cmo_indeterminate(ob); |
return (cmo *)new_cmo_indeterminate(ob); |
| } |
} |
| |
|
| static cmo *parse_cmo_error2() |
static cmo *parse_cmo_error2() |
| { |
{ |
| cmo *ob; |
cmo *ob; |
| |
|
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| ob = parse_cmo(); |
ob = parse_cmo(); |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)new_cmo_error2(ob); |
return (cmo *)new_cmo_error2(ob); |
| } |
} |
| Line 562 static int lex_digit() |
|
| Line 562 static int lex_digit() |
|
| return d; |
return d; |
| } |
} |
| |
|
| |
#define MK_KEY_CMO(x) { #x , x , TOKEN(x) , IS_CMO } |
| |
#define MK_KEY_SM(x) { #x , x , TOKEN(SM) , IS_SM } |
| |
#define MK_KEY_OX(x) { #x , x , TOKEN(x) , IS_OX } |
| |
|
| |
static symbol symbol_list[] = { |
| |
MK_KEY_CMO(CMO_NULL), |
| |
MK_KEY_CMO(CMO_INT32), |
| |
MK_KEY_CMO(CMO_DATUM), |
| |
MK_KEY_CMO(CMO_STRING), |
| |
MK_KEY_CMO(CMO_MATHCAP), |
| |
MK_KEY_CMO(CMO_LIST), |
| |
MK_KEY_CMO(CMO_MONOMIAL32), |
| |
MK_KEY_CMO(CMO_ZZ), |
| |
MK_KEY_CMO(CMO_ZERO), |
| |
MK_KEY_CMO(CMO_DMS_GENERIC), |
| |
MK_KEY_CMO(CMO_RING_BY_NAME), |
| |
MK_KEY_CMO(CMO_INDETERMINATE), |
| |
MK_KEY_CMO(CMO_DISTRIBUTED_POLYNOMIAL), |
| |
MK_KEY_CMO(CMO_ERROR2), |
| |
MK_KEY_SM(SM_popCMO), |
| |
MK_KEY_SM(SM_popString), |
| |
MK_KEY_SM(SM_mathcap), |
| |
MK_KEY_SM(SM_pops), |
| |
MK_KEY_SM(SM_executeStringByLocalParser), |
| |
MK_KEY_SM(SM_executeFunction), |
| |
MK_KEY_SM(SM_setMathCap), |
| |
MK_KEY_SM(SM_control_kill), |
| |
MK_KEY_SM(SM_control_reset_connection), |
| |
MK_KEY_OX(OX_COMMAND), |
| |
MK_KEY_OX(OX_DATA), |
| |
{NULL, 0, 0, 0} /* a gate keeper */ |
| |
}; |
| |
|
| |
symbol* lookup_by_symbol(char *key) |
| |
{ |
| |
symbol *symp; |
| |
for(symp = symbol_list; symp->key != NULL; symp++) { |
| |
if (strcmp(key, symp->key)==0) { |
| |
return symp; |
| |
} |
| |
} |
| |
return NULL; |
| |
} |
| |
|
| |
symbol* lookup_by_token(int tok) |
| |
{ |
| |
symbol *symp; |
| |
for(symp = symbol_list; symp->key != NULL; symp++) { |
| |
if (tok == symp->token) { |
| |
return symp; |
| |
} |
| |
} |
| |
return NULL; |
| |
} |
| |
|
| |
symbol* lookup_by_tag(int tag) |
| |
{ |
| |
symbol *symp; |
| |
for(symp = symbol_list; symp->key != NULL; symp++) { |
| |
if (tag == symp->tag) { |
| |
return symp; |
| |
} |
| |
} |
| |
return NULL; |
| |
} |
| |
|
| |
symbol* lookup(int i) |
| |
{ |
| |
return &symbol_list[i]; |
| |
} |
| |
|
| /* バッファあふれした場合の対策をちゃんと考えるべき */ |
/* バッファあふれした場合の対策をちゃんと考えるべき */ |
| static char *lex_quoted_string() |
static char *lex_quoted_string() |
| { |
{ |
| Line 591 static char *lex_quoted_string() |
|
| Line 662 static char *lex_quoted_string() |
|
| /* return NULL; */ |
/* return NULL; */ |
| } |
} |
| |
|
| typedef struct { |
|
| char *key; |
|
| int tag; |
|
| int token; |
|
| } symbol; |
|
| |
|
| #define MK_KEY_CMO(x) { #x , x , TOKEN(x) } |
|
| #define MK_KEY_SM(x) { #x , x , TOKEN(SM) } |
|
| #define MK_KEY(x) { #x , x , TOKEN(x) } |
|
| |
|
| static symbol symbol_list[] = { |
|
| MK_KEY_CMO(CMO_NULL), |
|
| MK_KEY_CMO(CMO_INT32), |
|
| MK_KEY_CMO(CMO_DATUM), |
|
| MK_KEY_CMO(CMO_STRING), |
|
| MK_KEY_CMO(CMO_MATHCAP), |
|
| MK_KEY_CMO(CMO_LIST), |
|
| MK_KEY_CMO(CMO_MONOMIAL32), |
|
| MK_KEY_CMO(CMO_ZZ), |
|
| MK_KEY_CMO(CMO_ZERO), |
|
| MK_KEY_CMO(CMO_DMS_GENERIC), |
|
| MK_KEY_CMO(CMO_RING_BY_NAME), |
|
| MK_KEY_CMO(CMO_INDETERMINATE), |
|
| MK_KEY_CMO(CMO_DISTRIBUTED_POLYNOMIAL), |
|
| MK_KEY_CMO(CMO_ERROR2), |
|
| MK_KEY_SM(SM_popCMO), |
|
| MK_KEY_SM(SM_popString), |
|
| MK_KEY_SM(SM_mathcap), |
|
| MK_KEY_SM(SM_pops), |
|
| MK_KEY_SM(SM_executeStringByLocalParser), |
|
| MK_KEY_SM(SM_executeFunction), |
|
| MK_KEY_SM(SM_setMathCap), |
|
| MK_KEY_SM(SM_control_kill), |
|
| MK_KEY_SM(SM_control_reset_connection), |
|
| MK_KEY(OX_COMMAND), |
|
| MK_KEY(OX_DATA), |
|
| {NULL, 0, 0} /* a gate keeper */ |
|
| }; |
|
| |
|
| static int token_of_symbol(char *key) |
static int token_of_symbol(char *key) |
| { |
{ |
| symbol *kp; |
symbol *symp = lookup_by_symbol(key); |
| for(kp = symbol_list; kp->key != NULL; kp++) { |
if (symp != NULL) { |
| if (strcmp(key, kp->key)==0) { |
yylval.d = symp->tag; |
| yylval.d = kp->tag; |
return symp->token; |
| return kp->token; |
} |
| } |
|
| } |
|
| #if DEBUG |
#if DEBUG |
| fprintf(stderr, "lex error\n"); |
fprintf(stderr, "lex error:: \"%s\" is unknown symbol.\n", key); |
| #endif |
#endif |
| return 0; |
return 0; |
| } |
} |
| Line 657 static int lex_symbol() |
|
| Line 687 static int lex_symbol() |
|
| c = GETC(); |
c = GETC(); |
| } |
} |
| fprintf(stderr, "buffer overflow!\n"); |
fprintf(stderr, "buffer overflow!\n"); |
| return 0; |
return 0; |
| } |
} |
| |
|
| /* return する前に一文字先読みしておく. */ |
/* return する前に一文字先読みしておく. */ |
| Line 720 static int mygetc_nonlf_flag; |
|
| Line 750 static int mygetc_nonlf_flag; |
|
| |
|
| int mygetc() |
int mygetc() |
| { |
{ |
| int c = '\0'; |
int c = '\0'; |
| |
|
| if (mygetc_nonlf_flag && mygetc_counter <= mygetc_counter_max) { |
if (mygetc_nonlf_flag && mygetc_counter <= mygetc_counter_max) { |
| c = mygetc_line[mygetc_counter++]; |
c = mygetc_line[mygetc_counter++]; |
| if (c == '\0') { |
if (c == '\0') { |
| c = '\n'; |
c = '\n'; |
| mygetc_nonlf_flag = 0; |
mygetc_nonlf_flag = 0; |
| } |
} |
| } |
} |
| return c; |
return c; |
| } |
} |
| |
|
| int setmode_mygetc(char *s, int len) |
int setmode_mygetc(char *s, int len) |
| { |
{ |
| mygetc_nonlf_flag=1; |
mygetc_nonlf_flag=1; |
| mygetc_counter=0; |
mygetc_counter=0; |
| mygetc_counter_max=len; |
mygetc_counter_max=len; |
| mygetc_line=s; |
mygetc_line=s; |
| Line 742 int setmode_mygetc(char *s, int len) |
|
| Line 772 int setmode_mygetc(char *s, int len) |
|
| |
|
| int setflag_parse(int flag) |
int setflag_parse(int flag) |
| { |
{ |
| pflag_cmo_addrev = flag; |
pflag_cmo_addrev = flag; |
| } |
} |