=================================================================== RCS file: /home/cvs/OpenXM/src/ox_toolkit/parse.c,v retrieving revision 1.6 retrieving revision 1.16 diff -u -p -r1.6 -r1.16 --- OpenXM/src/ox_toolkit/parse.c 2000/12/03 16:15:03 1.6 +++ OpenXM/src/ox_toolkit/parse.c 2005/07/20 17:48:03 1.16 @@ -1,5 +1,5 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.5 2000/10/10 05:23:21 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.15 2004/12/01 17:32:26 ohara Exp $ */ /* This module is a parser for OX/CMO expressions. @@ -48,11 +48,14 @@ static union{ static int pflag_cmo_addrev = 1; /* definitions of local functions */ +static void init_parser(char *s); +static cmo *parse(); static void parse_error(char *s); static void parse_right_parenthesis(); static void parse_left_parenthesis(); static void parse_comma(); -static mpz_ptr parse_integer(); +static mpz_ptr parse_mpz_integer(); +static int parse_integer(); static char *parse_string(); static cmo *parse_cmo_null(); static cmo *parse_cmo_int32(); @@ -75,6 +78,11 @@ static ox *parse_ox_data(); static void init_lex(char *s); static int lex(); +/* Parsing a Lisp-style expression of CMO. */ +cmo *ox_parse_lisp(char *s) +{ + return (s != NULL && strlen(s) > 0)? init_parser(s), parse(): NULL; +} static int is_token_cmo(int token) { @@ -96,43 +104,38 @@ static jmp_buf env_parse; /* This is a parsing fault. */ static void parse_error(char *s) { - fprintf(stderr, "syntax error: %s\n", s); + ox_printf("syntax error: %s\n", s); longjmp(env_parse, 1); } -void setflag_parse(int flag) +static void setflag_parse(int flag) { pflag_cmo_addrev = flag; } -void init_parser(char *s) +static void init_parser(char *s) { setflag_parse(PFLAG_ADDREV); init_lex(s); } -cmo *parse() +static cmo *parse() { - cmo *m; - - if (setjmp(env_parse) != 0) { - return NULL; - /* This is an error. */ - } - - token = lex(); - if (token == '(') { + cmo *m = NULL; + if (setjmp(env_parse) == 0) { token = lex(); - if (is_token_cmo(token)) { - m = parse_cmo(); - }else if(is_token_ox(token)) { - m = parse_ox(); - }else { - parse_error("invalid symbol."); + if (token == '(') { + token = lex(); + if (is_token_cmo(token)) { + m = parse_cmo(); + }else if(is_token_ox(token)) { + m = parse_ox(); + }else { + parse_error("invalid symbol."); + } } - return m; } - return NULL; + return m; } static ox *parse_ox() @@ -277,23 +280,20 @@ static void parse_comma() static mpz_ptr new_mpz_set_str(char *s) { - mpz_ptr z = malloc(sizeof(mpz_t)); + 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_ptr z = MALLOC(sizeof(mpz_t)); mpz_init(z); mpz_neg(z, src); -#ifndef DEBUG - free(src); -#endif return z; } -static mpz_ptr parse_integer() +static mpz_ptr parse_mpz_integer() { int sign = 1; mpz_ptr val; @@ -312,13 +312,34 @@ static mpz_ptr parse_integer() if (sign == -1) { val = my_mpz_neg(val); } -#ifndef DEBUG - free(yylval.sym); -#endif 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; @@ -338,12 +359,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() @@ -414,7 +435,7 @@ static cmo *parse_cmo_monomial32() int tag; parse_comma(); - size = mpz_get_si(parse_integer()); + size = parse_integer(); if (size < 0) { parse_error("invalid value."); } @@ -422,7 +443,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(); @@ -447,7 +468,7 @@ static cmo *parse_cmo_zz() mpz_ptr z; parse_comma(); - z = parse_integer(); + z = parse_mpz_integer(); if (token == ',') { length = mpz_get_si(z); m = new_cmo_zz_size(length); @@ -455,7 +476,7 @@ static cmo *parse_cmo_zz() 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); @@ -577,20 +598,18 @@ static int mygetc() 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 */ @@ -604,12 +623,12 @@ static char *lex_digit() buff[i] = c; }else { buff[i] = '\0'; - return mkstr(buff); + return new_string(buff); } c = mygetc(); } buff[SIZE_BUFFER-1] = '\0'; - return mkstr(buff); + return new_string(buff); } #define MK_KEY_CMO(x) { #x , x , TOKEN(x) , IS_CMO } @@ -684,9 +703,10 @@ symbol_t lookup(int i) return &symbol_list[i]; } -char *symbol_get_key(symbol_t sp) +char *get_symbol_by_tag(int tag) { - return sp->key; + symbol_t symp = lookup_by_tag(tag); + return (symp != NULL)? symp->key: NULL; } /* no measure for buffer overflow */ @@ -700,7 +720,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(); @@ -710,7 +730,7 @@ static char *lex_quoted_string() } buffer[i]=c; } - fprintf(stderr, "buffer overflow!\n"); + ox_printf("buffer overflow!\n"); exit(1); /* return NULL; */ } @@ -722,9 +742,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; } @@ -739,7 +757,7 @@ static int lex_symbol() buffer[i]=c; c = mygetc(); } - fprintf(stderr, "buffer overflow!\n"); + ox_printf("buffer overflow!\n"); return 0; } @@ -769,6 +787,7 @@ static int lex() yylval.sym = lex_quoted_string(); return T_STRING; default: + ; } if (isalpha(c)) {