=================================================================== RCS file: /home/cvs/OpenXM/src/ox_toolkit/parse.c,v retrieving revision 1.3 retrieving revision 1.13 diff -u -p -r1.3 -r1.13 --- OpenXM/src/ox_toolkit/parse.c 2000/01/17 19:55:56 1.3 +++ OpenXM/src/ox_toolkit/parse.c 2003/03/30 08:05:23 1.13 @@ -1,9 +1,9 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.2 1999/12/22 11:26:37 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.12 2003/03/23 22:09:57 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,8 +12,9 @@ #include #include #include -#include "oxtag.h" -#include "ox.h" +#include + +#include "ox_toolkit.h" #include "parse.h" /* --- Parser --- */ @@ -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,75 @@ 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); + mpz_ptr z = malloc(sizeof(mpz_t)); + mpz_init(z); + mpz_neg(z, src); +#ifdef DEBUG + free(src); #endif - return z; + 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); + val = new_mpz_set_str(yylval.sym); + if (sign == -1) { + val = my_mpz_neg(val); + } +#ifdef 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); +#ifdef DEBUG + free(yylval.sym); +#endif + token = lex(); + return val; +#endif +} + static char *parse_string() { char *s; @@ -335,12 +366,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 +405,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 +421,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 +437,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 +450,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 +472,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 +525,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 +556,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,11 +600,12 @@ 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; } @@ -587,40 +614,38 @@ static char buffer[SIZE_BUFFER]; static char *mkstr(char *src) { - int len; - char *s; - len = strlen(src); - s = malloc(len+1); - strcpy(s, src); - return s; + int len; + char *s; + len = strlen(src); + s = malloc(len+1); + strcpy(s, src); + return s; } /* 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 != NULL; symp++) { if (strcmp(key, symp->key)==0) { return symp; @@ -661,9 +686,9 @@ symbol* lookup_by_symbol(char *key) return NULL; } -symbol* lookup_by_token(int tok) +symbol_t lookup_by_token(int tok) { - symbol *symp; + symbol_t symp; for(symp = symbol_list; symp->key != NULL; symp++) { if (tok == symp->token) { return symp; @@ -672,9 +697,9 @@ symbol* lookup_by_token(int tok) return NULL; } -symbol* lookup_by_tag(int tag) +symbol_t lookup_by_tag(int tag) { - symbol *symp; + symbol_t symp; for(symp = symbol_list; symp->key != NULL; symp++) { if (tag == symp->tag) { return symp; @@ -683,11 +708,17 @@ symbol* lookup_by_tag(int tag) return NULL; } -symbol* lookup(int i) +symbol_t lookup(int i) { return &symbol_list[i]; } +char *get_symbol_by_tag(int tag) +{ + symbol_t symp = lookup_by_tag(tag); + return (symp != NULL)? symp->key: NULL; +} + /* no measure for buffer overflow */ static char *lex_quoted_string() { @@ -699,7 +730,7 @@ static char *lex_quoted_string() if(c == '"') { c = mygetc(); buffer[i]='\0'; - return mkstr(buffer); + return mkstr(buffer); }else if (c == '\\') { c0 = c; c = mygetc(); @@ -709,21 +740,19 @@ static char *lex_quoted_string() } buffer[i]=c; } - fprintf(stderr, "buffer overflow!\n"); + ox_printf("buffer overflow!\n"); exit(1); /* return NULL; */ } static int token_of_symbol(char *key) { - symbol *symp = lookup_by_symbol(key); + symbol_t symp = lookup_by_symbol(key); if (symp != NULL) { 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; } @@ -738,12 +767,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; @@ -756,8 +785,8 @@ int lex() case '(': case ')': case ',': - case '+': - case '-': + case '+': + case '-': c_dash = c; c = ' '; return c_dash; @@ -771,11 +800,11 @@ int lex() } if (isalpha(c)) { - /* symbols */ + /* symbols */ return lex_symbol(); } - /* digit */ + /* digit */ if (isdigit(c)){ yylval.sym = lex_digit(); return T_DIGIT;