=================================================================== RCS file: /home/cvs/OpenXM/src/ox_math/Attic/parse.c,v retrieving revision 1.3 retrieving revision 1.8 diff -u -p -r1.3 -r1.8 --- OpenXM/src/ox_math/Attic/parse.c 1999/11/02 18:58:25 1.3 +++ OpenXM/src/ox_math/Attic/parse.c 1999/11/07 12:12:55 1.8 @@ -1,6 +1,6 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_math/parse.c,v 1.2 1999/11/02 06:11:58 ohara Exp $ */ -/* $Id: parse.c,v 1.3 1999/11/02 18:58:25 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_math/parse.c,v 1.7 1999/11/06 21:39:37 ohara Exp $ */ + /* OX expression, CMO expression パーサ */ #include @@ -18,10 +18,9 @@ CMO_MONOMIAL32 は無視しない. (つまりおかしいときは構文エラーになる) */ - /* parse.c, lex.c では, Lisp 表現された CMO 文字列を読み込み, - バイト列を出力する. 中間表現として、cmo *を利用する. - parse() はトークンの列から cmo *(の指す構造体)を生成する. + バイト列を出力する. 中間表現として、cmo 構造体を利用する. + parse() はトークンの列から cmo 構造体を生成し、そのポインタを返す. */ /* 重要なことはパーサ(の各サブルーチン)は @@ -33,8 +32,8 @@ static int token = 0; /* トークンの属性値. yylval は lex() によってセットされる. */ static union{ - int d; - char* sym; + int d; + char *sym; } yylval; /* pflag_cmo_addrev がセットされていれば、厳密には CMO expression では @@ -43,28 +42,31 @@ static union{ static int pflag_cmo_addrev = 1; /* CMO の省略記法を許すか否かのフラグ */ /* 関数の宣言 */ -static int parse_error(char *s); -static int parse_lf(); -static int parse_right_parenthesis(); -static int parse_left_parenthesis(); -static int parse_comma(); -static int parse_integer(); -static cmo *parse_cmo_null(); -static cmo *parse_cmo_int32(); -static cmo *parse_cmo_string(); -static cmo *parse_cmo_mathcap(); -static cmo *parse_cmo_dms_generic(); -static cmo *parse_cmo_ring_by_name(); -static cmo *parse_cmo_error2(); -static cmo *parse_cmo_zero(); -static cmo *parse_cmo_zz(); -static cmo *parse_cmo_list(); -static cmo *parse_cmo(); +static int parse_error(char *s); +static int parse_lf(); +static int parse_right_parenthesis(); +static int parse_left_parenthesis(); +static int parse_comma(); +static int parse_integer(); static char *parse_string(); -static int parse_sm(); -static ox* parse_ox(); -static ox* parse_ox_command(); -static ox* parse_ox_data(); +static cmo *parse_cmo_null(); +static cmo *parse_cmo_int32(); +static cmo *parse_cmo_string(); +static cmo *parse_cmo_mathcap(); +static cmo *parse_cmo_list(); +static cmo *parse_cmo_monomial32(); +static cmo *parse_cmo_zz(); +static cmo *parse_cmo_zero(); +static cmo *parse_cmo_dms_generic(); +static cmo *parse_cmo_ring_by_name(); +static cmo *parse_cmo_distributed_polynomial(); +static cmo *parse_cmo_indeterminate(); +static cmo *parse_cmo_error2(); +static cmo *parse_cmo(); +static int parse_sm(); +static ox *parse_ox(); +static ox *parse_ox_command(); +static ox *parse_ox_data(); static int is_token_cmo(int token) { @@ -73,7 +75,7 @@ static int is_token_cmo(int token) static int is_token_sm(int token) { - return token >= MIN_T_SM && token < MAX_T_SM; + return token == TOKEN(SM); } static int is_token_ox(int token) @@ -86,8 +88,8 @@ static jmp_buf env_parse; /* 構文解析に失敗したことを意味する. */ static int parse_error(char *s) { - fprintf(stderr, "syntax error: %s\n", s); - longjmp(env_parse, 1); + fprintf(stderr, "syntax error: %s\n", s); + longjmp(env_parse, 1); } /* この部分は書き換え予定. */ @@ -95,9 +97,9 @@ cmo *parse() { cmo *m; - if (setjmp(env_parse) != 0) { - return NULL; /* 構文解析に失敗したら NULL を返す. */ - } + if (setjmp(env_parse) != 0) { + return NULL; /* 構文解析に失敗したら NULL を返す. */ + } do{ token = lex(); @@ -128,7 +130,7 @@ static int parse_lf() } -static ox* parse_ox() +static ox *parse_ox() { ox *m = NULL; @@ -147,9 +149,9 @@ static ox* parse_ox() return m; } -static ox* parse_ox_data() +static ox *parse_ox_data() { - ox* m; + ox *m; parse_comma(); parse_left_parenthesis(); @@ -158,28 +160,30 @@ static ox* parse_ox_data() return m; } +static ox *parse_ox_command() +{ + ox *m; + + parse_comma(); + parse_left_parenthesis(); + m = (ox *)new_ox_command(parse_sm()); + parse_right_parenthesis(); + return m; +} + static int parse_sm() { int sm_code; - if (!is_token_sm(token)) { - parse_error("invalid SM opecode."); + if (token != TOKEN(SM)) { + parse_error("no opecode."); } - sm_code = token - T_MAGIC; + sm_code = yylval.d; token = lex(); + parse_right_parenthesis(); return sm_code; } -static ox* parse_ox_command() -{ - ox* m; - - parse_comma(); - m = (ox *)new_ox_command(parse_sm()); - parse_right_parenthesis(); - return m; -} - /* 正しい入力ならば, parse_cmo を呼ぶ時点で, token には TOKEN(CMO_xxx), TOKEN(OX_xxx) のいずれかがセットされている. */ static cmo *parse_cmo() @@ -231,6 +235,10 @@ static cmo *parse_cmo() token = lex(); m = parse_cmo_distributed_polynomial(); break; + case TOKEN(CMO_INDETERMINATE): + token = lex(); + m = parse_cmo_indeterminate(); + break; case TOKEN(CMO_ERROR2): token = lex(); m = parse_cmo_error2(); @@ -315,7 +323,7 @@ static cmo *parse_cmo_string() }else if (!pflag_cmo_addrev) { parse_error("invalid cmo string."); } - s = parse_string(); + s = parse_string(); m = new_cmo_string(s); parse_right_parenthesis(); return (cmo *)m; @@ -323,11 +331,11 @@ static cmo *parse_cmo_string() static cmo *parse_cmo_mathcap() { - cmo *ob; + cmo *ob; parse_comma(); - parse_left_parenthesis(); - ob = parse_cmo(); + parse_left_parenthesis(); + ob = parse_cmo(); parse_right_parenthesis(); return (cmo *)new_cmo_mathcap(ob); } @@ -339,60 +347,60 @@ static cmo *parse_cmo_list() cmo_list *m = new_cmo_list(); cmo *newcmo; - if (token == ',') { - parse_comma(); + if (token == ',') { + parse_comma(); - if (token == T_INTEGER) { - parse_integer(); - parse_comma(); - }else if (!pflag_cmo_addrev) { - parse_error("invalid cmo_list."); - } + if (token == T_INTEGER) { + parse_integer(); + parse_comma(); + }else if (!pflag_cmo_addrev) { + parse_error("invalid cmo_list."); + } - while(token == '(') { - parse_left_parenthesis(); - newcmo = parse_cmo(); - append_cmo_list(m, newcmo); - if (token != ',') { - break; - } - parse_comma(); - } - }else if (!pflag_cmo_addrev) { - parse_error("invalid cmo_list."); - } + while(token == '(') { + parse_left_parenthesis(); + newcmo = parse_cmo(); + append_cmo_list(m, newcmo); + if (token != ',') { + break; + } + parse_comma(); + } + }else if (!pflag_cmo_addrev) { + parse_error("invalid cmo_list."); + } parse_right_parenthesis(); return (cmo *)m; } static cmo *parse_cmo_monomial32() { - int size; - int *exps; - int i; - cmo_monomial32 *m; - int tag; + int size; + int *exps; + int i; + cmo_monomial32 *m; + int tag; - parse_comma(); - size = parse_integer(); - if (size < 0) { - parse_error("invalid value."); - } - m = new_cmo_monomial32_size(size); + parse_comma(); + size = parse_integer(); + if (size < 0) { + parse_error("invalid value."); + } + m = new_cmo_monomial32_size(size); - for(i=0; iexps[i] = parse_integer(); - } - parse_comma(); - parse_left_parenthesis(); - m->coef = parse_cmo(); - tag = m->coef->tag; + for(i=0; iexps[i] = parse_integer(); + } + parse_comma(); + parse_left_parenthesis(); + m->coef = parse_cmo(); + tag = m->coef->tag; /* m->coef は CMO_ZZ 型か CMO_INT32 型でなければならない */ - if (tag != CMO_ZZ && tag != CMO_INT32) { - parse_error("invalid cmo."); - } + if (tag != CMO_ZZ && tag != CMO_INT32) { + parse_error("invalid cmo."); + } parse_right_parenthesis(); return (cmo *)m; } @@ -438,16 +446,16 @@ static cmo *parse_cmo_dms_generic() static cmo *parse_cmo_ring_by_name() { - cmo *ob; + cmo *ob; parse_comma(); - parse_left_parenthesis(); - ob = parse_cmo(); + parse_left_parenthesis(); + ob = parse_cmo(); /* ob は CMO_STRING 型でなければならない */ - if (ob->tag != CMO_STRING) { - parse_error("invalid cmo."); - } + if (ob->tag != CMO_STRING) { + parse_error("invalid cmo."); + } parse_right_parenthesis(); return (cmo *)new_cmo_ring_by_name(ob); } @@ -458,56 +466,66 @@ static cmo *parse_cmo_distributed_polynomial() int i=0; cmo_distributed_polynomial *m = new_cmo_distributed_polynomial(); cmo *ob; - int tag; + int tag; - if (token == ',') { - parse_comma(); + if (token == ',') { + parse_comma(); - if (token == T_INTEGER) { - parse_integer(); - parse_comma(); - }else if (!pflag_cmo_addrev) { - parse_error("invalid d-polynomial."); - } + if (token == T_INTEGER) { + parse_integer(); + parse_comma(); + }else if (!pflag_cmo_addrev) { + parse_error("invalid d-polynomial."); + } - parse_left_parenthesis(); - m->ringdef = parse_cmo(); - tag = m->ringdef->tag; - /* m->ringdef は DringDefinition でなければならない */ - if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC - && tag != CMO_DMS_OF_N_VARIABLES) { - parse_error("invalid cmo."); - } + parse_left_parenthesis(); + m->ringdef = parse_cmo(); + tag = m->ringdef->tag; + /* m->ringdef は DringDefinition でなければならない */ + if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC + && tag != CMO_DMS_OF_N_VARIABLES) { + parse_error("invalid cmo."); + } - parse_comma(); + parse_comma(); - while(token == '(') { - parse_left_parenthesis(); - ob = parse_cmo(); - if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { - parse_error("invalid cmo."); - } - append_cmo_list(m, ob); - if (token != ',') { - break; - } - parse_comma(); - } - }else if (!pflag_cmo_addrev) { - parse_error("invalid d-polynomial."); - } + while(token == '(') { + parse_left_parenthesis(); + ob = parse_cmo(); + if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { + parse_error("invalid cmo."); + } + append_cmo_list((cmo_list *)m, ob); + if (token != ',') { + break; + } + parse_comma(); + } + }else if (!pflag_cmo_addrev) { + parse_error("invalid d-polynomial."); + } parse_right_parenthesis(); return (cmo *)m; } +static cmo *parse_cmo_indeterminate() +{ + cmo *ob; + parse_comma(); + parse_left_parenthesis(); + ob = parse_cmo(); + parse_right_parenthesis(); + return (cmo *)new_cmo_indeterminate(ob); +} + static cmo *parse_cmo_error2() { - cmo *ob; + cmo *ob; parse_comma(); - parse_left_parenthesis(); - ob = parse_cmo(); + parse_left_parenthesis(); + ob = parse_cmo(); parse_right_parenthesis(); return (cmo *)new_cmo_error2(ob); } @@ -544,6 +562,77 @@ static int lex_digit() 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() { @@ -573,52 +662,15 @@ static char *lex_quoted_string() /* return NULL; */ } -typedef struct { - char *key; - int token; -} symbol; - -#define MK_KEY_CMO(x) { #x , TOKEN(x) } -#define MK_KEY(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_popCMO), - MK_KEY(SM_popString), - MK_KEY(SM_mathcap), - MK_KEY(SM_pops), - MK_KEY(SM_executeStringByLocalParser), - MK_KEY(SM_executeFunction), - MK_KEY(SM_setMathCap), - MK_KEY(SM_control_kill), - MK_KEY(SM_control_reset_connection), - MK_KEY(OX_COMMAND), MK_KEY(OX_DATA), - {NULL, 0} /* a gate keeper */ -}; - static int token_of_symbol(char *key) { - symbol *kp; - for(kp = symbol_list; kp->key != NULL; kp++) { - if (strcmp(key, kp->key)==0) { - return kp->token; - } - } + symbol *symp = lookup_by_symbol(key); + if (symp != NULL) { + yylval.d = symp->tag; + return symp->token; + } #if DEBUG - fprintf(stderr, "lex error\n"); + fprintf(stderr, "lex error:: \"%s\" is unknown symbol.\n", key); #endif return 0; } @@ -635,10 +687,10 @@ static int lex_symbol() c = GETC(); } fprintf(stderr, "buffer overflow!\n"); - return 0; + return 0; } -/* return する前に一文字先読みしておく。 */ +/* return する前に一文字先読みしておく. */ int lex() { int c_dash = 0; @@ -690,20 +742,29 @@ int lex() return 0; } +/* 一文字読み込む関数 */ static char *mygetc_line; static int mygetc_counter; static int mygetc_counter_max; +static int mygetc_nonlf_flag; int mygetc() { - if (mygetc_counter <= mygetc_counter_max) { - return mygetc_line[mygetc_counter++]; + int c = '\0'; + + if (mygetc_nonlf_flag && mygetc_counter <= mygetc_counter_max) { + c = mygetc_line[mygetc_counter++]; + if (c == '\0') { + c = '\n'; + mygetc_nonlf_flag = 0; + } } - return 0; + return c; } int setmode_mygetc(char *s, int len) { + mygetc_nonlf_flag=1; mygetc_counter=0; mygetc_counter_max=len; mygetc_line=s; @@ -711,5 +772,5 @@ int setmode_mygetc(char *s, int len) int setflag_parse(int flag) { - pflag_cmo_addrev = flag; + pflag_cmo_addrev = flag; }