| version 1.5, 2000/10/10 05:23:21 |
version 1.16, 2005/07/20 17:48:03 |
|
|
| /* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
| /* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.4 2000/03/10 12:24:39 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. |
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. |
character set. |
| */ |
*/ |
| |
|
|
|
| static int pflag_cmo_addrev = 1; |
static int pflag_cmo_addrev = 1; |
| |
|
| /* definitions of local functions */ |
/* definitions of local functions */ |
| |
static void init_parser(char *s); |
| |
static cmo *parse(); |
| static void parse_error(char *s); |
static void parse_error(char *s); |
| static void parse_right_parenthesis(); |
static void parse_right_parenthesis(); |
| static void parse_left_parenthesis(); |
static void parse_left_parenthesis(); |
| static void parse_comma(); |
static void parse_comma(); |
| static mpz_ptr parse_integer(); |
static mpz_ptr parse_mpz_integer(); |
| |
static int parse_integer(); |
| static char *parse_string(); |
static char *parse_string(); |
| static cmo *parse_cmo_null(); |
static cmo *parse_cmo_null(); |
| static cmo *parse_cmo_int32(); |
static cmo *parse_cmo_int32(); |
| Line 75 static ox *parse_ox_data(); |
|
| Line 78 static ox *parse_ox_data(); |
|
| static void init_lex(char *s); |
static void init_lex(char *s); |
| static int lex(); |
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) |
static int is_token_cmo(int token) |
| { |
{ |
| Line 96 static jmp_buf env_parse; |
|
| Line 104 static jmp_buf env_parse; |
|
| /* This is a parsing fault. */ |
/* This is a parsing fault. */ |
| static void 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); |
longjmp(env_parse, 1); |
| } |
} |
| |
|
| void setflag_parse(int flag) |
static void setflag_parse(int flag) |
| { |
{ |
| pflag_cmo_addrev = flag; |
pflag_cmo_addrev = flag; |
| } |
} |
| |
|
| void init_parser(char *s) |
static void init_parser(char *s) |
| { |
{ |
| setflag_parse(PFLAG_ADDREV); |
setflag_parse(PFLAG_ADDREV); |
| init_lex(s); |
init_lex(s); |
| } |
} |
| |
|
| cmo *parse() |
static cmo *parse() |
| { |
{ |
| cmo *m; |
cmo *m = NULL; |
| |
if (setjmp(env_parse) == 0) { |
| if (setjmp(env_parse) != 0) { |
|
| return NULL; |
|
| /* This is an error. */ |
|
| } |
|
| |
|
| token = lex(); |
|
| if (token == '(') { |
|
| token = lex(); |
token = lex(); |
| if (is_token_cmo(token)) { |
if (token == '(') { |
| m = parse_cmo(); |
token = lex(); |
| }else if(is_token_ox(token)) { |
if (is_token_cmo(token)) { |
| m = parse_ox(); |
m = parse_cmo(); |
| }else { |
}else if(is_token_ox(token)) { |
| parse_error("invalid symbol."); |
m = parse_ox(); |
| |
}else { |
| |
parse_error("invalid symbol."); |
| |
} |
| } |
} |
| return m; |
|
| } |
} |
| return NULL; |
return m; |
| } |
} |
| |
|
| static ox *parse_ox() |
static ox *parse_ox() |
| Line 277 static void parse_comma() |
|
| Line 280 static void parse_comma() |
|
| |
|
| static mpz_ptr new_mpz_set_str(char *s) |
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); |
mpz_init_set_str(z, s, 10); |
| return z; |
return z; |
| } |
} |
| |
|
| static mpz_ptr my_mpz_neg(mpz_ptr src) |
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_init(z); |
| mpz_neg(z, src); |
mpz_neg(z, src); |
| #ifndef DEBUG |
|
| free(src); |
|
| #endif |
|
| return z; |
return z; |
| } |
} |
| |
|
| static mpz_ptr parse_integer() |
static mpz_ptr parse_mpz_integer() |
| { |
{ |
| int sign = 1; |
int sign = 1; |
| mpz_ptr val; |
mpz_ptr val; |
| Line 312 static mpz_ptr parse_integer() |
|
| Line 312 static mpz_ptr parse_integer() |
|
| if (sign == -1) { |
if (sign == -1) { |
| val = my_mpz_neg(val); |
val = my_mpz_neg(val); |
| } |
} |
| #ifndef DEBUG |
|
| free(yylval.sym); |
|
| #endif |
|
| token = lex(); |
token = lex(); |
| return val; |
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() |
static char *parse_string() |
| { |
{ |
| char *s; |
char *s; |
| Line 338 static cmo *parse_cmo_null() |
|
| Line 359 static cmo *parse_cmo_null() |
|
| |
|
| static cmo *parse_cmo_int32() |
static cmo *parse_cmo_int32() |
| { |
{ |
| mpz_ptr z; |
int z; |
| |
|
| parse_comma(); |
parse_comma(); |
| z = parse_integer(); |
z = parse_integer(); |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)new_cmo_int32(mpz_get_si(z)); |
return (cmo *)new_cmo_int32(z); |
| } |
} |
| |
|
| static cmo *parse_cmo_string() |
static cmo *parse_cmo_string() |
| Line 414 static cmo *parse_cmo_monomial32() |
|
| Line 435 static cmo *parse_cmo_monomial32() |
|
| int tag; |
int tag; |
| |
|
| parse_comma(); |
parse_comma(); |
| size = mpz_get_si(parse_integer()); |
size = parse_integer(); |
| if (size < 0) { |
if (size < 0) { |
| parse_error("invalid value."); |
parse_error("invalid value."); |
| } |
} |
| Line 422 static cmo *parse_cmo_monomial32() |
|
| Line 443 static cmo *parse_cmo_monomial32() |
|
| |
|
| for(i=0; i<size; i++) { |
for(i=0; i<size; i++) { |
| parse_comma(); |
parse_comma(); |
| m->exps[i] = mpz_get_si(parse_integer()); |
m->exps[i] = parse_integer(); |
| } |
} |
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| Line 447 static cmo *parse_cmo_zz() |
|
| Line 468 static cmo *parse_cmo_zz() |
|
| mpz_ptr z; |
mpz_ptr z; |
| |
|
| parse_comma(); |
parse_comma(); |
| z = parse_integer(); |
z = parse_mpz_integer(); |
| if (token == ',') { |
if (token == ',') { |
| length = mpz_get_si(z); |
length = mpz_get_si(z); |
| m = new_cmo_zz_size(length); |
m = new_cmo_zz_size(length); |
| Line 455 static cmo *parse_cmo_zz() |
|
| Line 476 static cmo *parse_cmo_zz() |
|
| length = abs(length); |
length = abs(length); |
| for(i=0; i<length; i++) { |
for(i=0; i<length; i++) { |
| parse_comma(); |
parse_comma(); |
| m->mpz->_mp_d[i] = mpz_get_si(parse_integer()); |
m->mpz->_mp_d[i] = parse_integer(); |
| } |
} |
| }else if (pflag_cmo_addrev) { |
}else if (pflag_cmo_addrev) { |
| m = new_cmo_zz_set_mpz(z); |
m = new_cmo_zz_set_mpz(z); |
| Line 577 static int mygetc() |
|
| Line 598 static int mygetc() |
|
| |
|
| static void init_lex(char *s) |
static void init_lex(char *s) |
| { |
{ |
| |
c=' '; |
| mygetc_ptr=s; |
mygetc_ptr=s; |
| } |
} |
| |
|
| #define SIZE_BUFFER 8192 |
#define SIZE_BUFFER 8192 |
| static char buffer[SIZE_BUFFER]; |
static char buffer[SIZE_BUFFER]; |
| |
|
| static char *mkstr(char *src) |
static char *new_string(char *s) |
| { |
{ |
| int len; |
char *t = MALLOC(strlen(s)+1); |
| char *s; |
strcpy(t, s); |
| len = strlen(src); |
return t; |
| s = malloc(len+1); |
|
| strcpy(s, src); |
|
| return s; |
|
| } |
} |
| |
|
| /* no measure for buffer overflow */ |
/* no measure for buffer overflow */ |
| Line 604 static char *lex_digit() |
|
| Line 623 static char *lex_digit() |
|
| buff[i] = c; |
buff[i] = c; |
| }else { |
}else { |
| buff[i] = '\0'; |
buff[i] = '\0'; |
| return mkstr(buff); |
return new_string(buff); |
| } |
} |
| c = mygetc(); |
c = mygetc(); |
| } |
} |
| buff[SIZE_BUFFER-1] = '\0'; |
buff[SIZE_BUFFER-1] = '\0'; |
| return mkstr(buff); |
return new_string(buff); |
| } |
} |
| |
|
| #define MK_KEY_CMO(x) { #x , x , TOKEN(x) , IS_CMO } |
#define MK_KEY_CMO(x) { #x , x , TOKEN(x) , IS_CMO } |
| Line 684 symbol_t lookup(int i) |
|
| Line 703 symbol_t lookup(int i) |
|
| return &symbol_list[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 */ |
/* no measure for buffer overflow */ |
| Line 700 static char *lex_quoted_string() |
|
| Line 720 static char *lex_quoted_string() |
|
| if(c == '"') { |
if(c == '"') { |
| c = mygetc(); |
c = mygetc(); |
| buffer[i]='\0'; |
buffer[i]='\0'; |
| return mkstr(buffer); |
return new_string(buffer); |
| }else if (c == '\\') { |
}else if (c == '\\') { |
| c0 = c; |
c0 = c; |
| c = mygetc(); |
c = mygetc(); |
| Line 710 static char *lex_quoted_string() |
|
| Line 730 static char *lex_quoted_string() |
|
| } |
} |
| buffer[i]=c; |
buffer[i]=c; |
| } |
} |
| fprintf(stderr, "buffer overflow!\n"); |
ox_printf("buffer overflow!\n"); |
| exit(1); |
exit(1); |
| /* return NULL; */ |
/* return NULL; */ |
| } |
} |
| Line 722 static int token_of_symbol(char *key) |
|
| Line 742 static int token_of_symbol(char *key) |
|
| yylval.d = symp->tag; |
yylval.d = symp->tag; |
| return symp->token; |
return symp->token; |
| } |
} |
| #if DEBUG |
ox_printf("lex error:: \"%s\" is unknown symbol.\n", key); |
| fprintf(stderr, "lex error:: \"%s\" is unknown symbol.\n", key); |
|
| #endif |
|
| return 0; |
return 0; |
| } |
} |
| |
|
| Line 739 static int lex_symbol() |
|
| Line 757 static int lex_symbol() |
|
| buffer[i]=c; |
buffer[i]=c; |
| c = mygetc(); |
c = mygetc(); |
| } |
} |
| fprintf(stderr, "buffer overflow!\n"); |
ox_printf("buffer overflow!\n"); |
| return 0; |
return 0; |
| } |
} |
| |
|
| /* return する前に一文字先読みしておく. */ |
/* Remark: prefetching a character before return. */ |
| static int lex() |
static int lex() |
| { |
{ |
| int c_dash = 0; |
int c_dash = 0; |
| Line 769 static int lex() |
|
| Line 787 static int lex() |
|
| yylval.sym = lex_quoted_string(); |
yylval.sym = lex_quoted_string(); |
| return T_STRING; |
return T_STRING; |
| default: |
default: |
| |
; |
| } |
} |
| |
|
| if (isalpha(c)) { |
if (isalpha(c)) { |