=================================================================== RCS file: /home/cvs/OpenXM/src/ox_toolkit/parse.c,v retrieving revision 1.4 retrieving revision 1.14 diff -u -p -r1.4 -r1.14 --- OpenXM/src/ox_toolkit/parse.c 2000/03/10 12:24:39 1.4 +++ OpenXM/src/ox_toolkit/parse.c 2003/06/02 10:25:57 1.14 @@ -1,9 +1,9 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.3 2000/01/17 19:55:56 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.13 2003/03/30 08:05:23 ohara Exp $ */ /* This module is a parser for OX/CMO expressions. - Some commnets is written in Japanese by using the EUC-JP coded + Some commnets are written in Japanese by using the EUC-JP coded character set. */ @@ -12,6 +12,7 @@ #include #include #include +#include #include "ox_toolkit.h" #include "parse.h" @@ -47,12 +48,12 @@ static union{ static int pflag_cmo_addrev = 1; /* definitions of local functions */ -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 mpz_ptr parse_integer(); +static void parse_error(char *s); +static void parse_right_parenthesis(); +static void parse_left_parenthesis(); +static void parse_comma(); +static mpz_ptr parse_mpz_integer(); +static int parse_integer(); static char *parse_string(); static cmo *parse_cmo_null(); static cmo *parse_cmo_int32(); @@ -72,7 +73,10 @@ static int parse_sm(); static ox *parse_ox(); static ox *parse_ox_command(); static ox *parse_ox_data(); +static void init_lex(char *s); +static int lex(); + static int is_token_cmo(int token) { return (token >= MIN_T_CMO && token < MAX_T_CMO) || token == TOKEN(CMO_ERROR2); @@ -91,21 +95,21 @@ static int is_token_ox(int token) static jmp_buf env_parse; /* This is a parsing fault. */ -static int parse_error(char *s) +static void parse_error(char *s) { - fprintf(stderr, "syntax error: %s\n", s); + ox_printf("syntax error: %s\n", s); longjmp(env_parse, 1); } -int setflag_parse(int flag) +void setflag_parse(int flag) { pflag_cmo_addrev = flag; } -int init_parser(char *s) +void init_parser(char *s) { - setflag_parse(PFLAG_ADDREV); - init_lex(s); + setflag_parse(PFLAG_ADDREV); + init_lex(s); } cmo *parse() @@ -114,10 +118,10 @@ cmo *parse() if (setjmp(env_parse) != 0) { return NULL; - /* This is an error. */ + /* This is an error. */ } - token = lex(); + token = lex(); if (token == '(') { token = lex(); if (is_token_cmo(token)) { @@ -248,7 +252,7 @@ static cmo *parse_cmo() return m; } -static int parse_left_parenthesis() +static void parse_left_parenthesis() { if (token != '(') { parse_error("no left parenthesis."); @@ -256,7 +260,7 @@ static int parse_left_parenthesis() token = lex(); } -static int parse_right_parenthesis() +static void parse_right_parenthesis() { if (token != ')') { parse_error("no right parenthesis."); @@ -264,7 +268,7 @@ static int parse_right_parenthesis() token = lex(); } -static int parse_comma() +static void parse_comma() { if (token != ',') { parse_error("no comma."); @@ -274,48 +278,66 @@ static int parse_comma() static mpz_ptr new_mpz_set_str(char *s) { - mpz_ptr z = malloc(sizeof(mpz_t)); - mpz_init_set_str(z, s, 10); - return z; + mpz_ptr z = MALLOC(sizeof(mpz_t)); + mpz_init_set_str(z, s, 10); + return z; } static mpz_ptr my_mpz_neg(mpz_ptr src) { - mpz_ptr z = malloc(sizeof(mpz_t)); - mpz_init(z); - mpz_neg(z, src); -#ifndef DEBUG - free(src); -#endif - return z; + mpz_ptr z = MALLOC(sizeof(mpz_t)); + mpz_init(z); + mpz_neg(z, src); + return z; } -static mpz_ptr parse_integer() +static mpz_ptr parse_mpz_integer() { - int sign = 1; - mpz_ptr val; + int sign = 1; + mpz_ptr val; - if (token == '+') { - token = lex(); - }else if (token == '-') { - sign = -1; - token = lex(); - } + if (token == '+') { + token = lex(); + }else if (token == '-') { + sign = -1; + token = lex(); + } if (token != T_DIGIT) { parse_error("no integer."); } - val = new_mpz_set_str(yylval.sym); - if (sign == -1) { - val = my_mpz_neg(val); - } -#ifndef DEBUG - free(yylval.sym); -#endif + val = new_mpz_set_str(yylval.sym); + if (sign == -1) { + val = my_mpz_neg(val); + } token = lex(); return val; } +static int parse_integer() +{ +#if 0 + return mpz_get_si(parse_mpz_integer()); +#else + int sign = 1; + int val; + + if (token == '+') { + token = lex(); + }else if (token == '-') { + sign = -1; + token = lex(); + } + + if (token != T_DIGIT) { + parse_error("no integer."); + } + val = sign*atoi(yylval.sym); + token = lex(); + return val; +#endif +} + static char *parse_string() { char *s; @@ -335,12 +357,12 @@ static cmo *parse_cmo_null() static cmo *parse_cmo_int32() { - mpz_ptr z; + int z; parse_comma(); z = parse_integer(); parse_right_parenthesis(); - return (cmo *)new_cmo_int32(mpz_get_si(z)); + return (cmo *)new_cmo_int32(z); } static cmo *parse_cmo_string() @@ -374,8 +396,6 @@ static cmo *parse_cmo_mathcap() static cmo *parse_cmo_list() { - int length=0; - int i=0; cmo_list *m = new_cmo_list(); cmo *newcmo; @@ -392,7 +412,7 @@ static cmo *parse_cmo_list() while(token == '(') { parse_left_parenthesis(); newcmo = parse_cmo(); - append_cmo_list(m, newcmo); + list_append(m, newcmo); if (token != ',') { break; } @@ -408,13 +428,12 @@ static cmo *parse_cmo_list() static cmo *parse_cmo_monomial32() { int size; - int *exps; int i; cmo_monomial32 *m; int tag; parse_comma(); - size = mpz_get_si(parse_integer()); + size = parse_integer(); if (size < 0) { parse_error("invalid value."); } @@ -422,7 +441,7 @@ static cmo *parse_cmo_monomial32() for(i=0; iexps[i] = mpz_get_si(parse_integer()); + m->exps[i] = parse_integer(); } parse_comma(); parse_left_parenthesis(); @@ -444,18 +463,18 @@ static cmo *parse_cmo_zz() int length; int i=0; cmo_zz *m= NULL; - mpz_ptr z; + mpz_ptr z; parse_comma(); - z = parse_integer(); + z = parse_mpz_integer(); if (token == ',') { - length = mpz_get_si(z); + length = mpz_get_si(z); m = new_cmo_zz_size(length); length = abs(length); for(i=0; impz->_mp_d[i] = mpz_get_si(parse_integer()); + m->mpz->_mp_d[i] = parse_integer(); } }else if (pflag_cmo_addrev) { m = new_cmo_zz_set_mpz(z); @@ -497,8 +516,6 @@ static cmo *parse_cmo_ring_by_name() static cmo *parse_cmo_distributed_polynomial() { - int length=0; - int i=0; cmo_distributed_polynomial *m = new_cmo_distributed_polynomial(); cmo *ob; int tag; @@ -530,7 +547,7 @@ static cmo *parse_cmo_distributed_polynomial() if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { parse_error("invalid cmo."); } - append_cmo_list((cmo_list *)m, ob); + list_append((cmo_list *)m, ob); if (token != ',') { break; } @@ -574,46 +591,42 @@ static int c = ' '; static char *mygetc_ptr; static int mygetc() { - return *mygetc_ptr++; + return *mygetc_ptr++; } -int init_lex(char *s) +static void init_lex(char *s) { + c=' '; mygetc_ptr=s; } #define SIZE_BUFFER 8192 static char buffer[SIZE_BUFFER]; -static char *mkstr(char *src) +static char *new_string(char *s) { - int len; - char *s; - len = strlen(src); - s = malloc(len+1); - strcpy(s, src); - return s; + char *t = MALLOC(strlen(s)+1); + strcpy(t, s); + return t; } /* no measure for buffer overflow */ static char *lex_digit() { - static char buff[SIZE_BUFFER]; - int i; - char *s; - int len; + static char buff[SIZE_BUFFER]; + int i; - for(i=0; ikey; + symbol_t symp = lookup_by_tag(tag); + return (symp != NULL)? symp->key: NULL; } /* no measure for buffer overflow */ @@ -704,7 +718,7 @@ static char *lex_quoted_string() if(c == '"') { c = mygetc(); buffer[i]='\0'; - return mkstr(buffer); + return new_string(buffer); }else if (c == '\\') { c0 = c; c = mygetc(); @@ -714,7 +728,7 @@ static char *lex_quoted_string() } buffer[i]=c; } - fprintf(stderr, "buffer overflow!\n"); + ox_printf("buffer overflow!\n"); exit(1); /* return NULL; */ } @@ -726,9 +740,7 @@ static int token_of_symbol(char *key) yylval.d = symp->tag; return symp->token; } -#if DEBUG - fprintf(stderr, "lex error:: \"%s\" is unknown symbol.\n", key); -#endif + ox_printf("lex error:: \"%s\" is unknown symbol.\n", key); return 0; } @@ -743,12 +755,12 @@ static int lex_symbol() buffer[i]=c; c = mygetc(); } - fprintf(stderr, "buffer overflow!\n"); + ox_printf("buffer overflow!\n"); return 0; } -/* return する前に一文字先読みしておく. */ -int lex() +/* Remark: prefetching a character before return. */ +static int lex() { int c_dash = 0; @@ -761,8 +773,8 @@ int lex() case '(': case ')': case ',': - case '+': - case '-': + case '+': + case '-': c_dash = c; c = ' '; return c_dash; @@ -776,11 +788,11 @@ int lex() } if (isalpha(c)) { - /* symbols */ + /* symbols */ return lex_symbol(); } - /* digit */ + /* digit */ if (isdigit(c)){ yylval.sym = lex_digit(); return T_DIGIT;