| version 1.11, 2003/03/23 20:17:35 |
version 1.18, 2018/04/05 06:05:44 |
|
|
| /* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
| /* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.10 2003/02/03 23:13:23 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.17 2015/08/27 03:03:34 ohara Exp $ */ |
| |
|
| /* |
/* |
| This module is a parser for OX/CMO expressions. |
This module is a parser for OX/CMO expressions. |
|
|
| #include <stdio.h> |
#include <stdio.h> |
| #include <stdlib.h> |
#include <stdlib.h> |
| #include <string.h> |
#include <string.h> |
| #include <sys/param.h> |
|
| #include <setjmp.h> |
#include <setjmp.h> |
| #include <ctype.h> |
#include <ctype.h> |
| |
#if !defined(_MSC_VER) |
| |
#include <sys/param.h> |
| |
#endif |
| |
|
| #include "ox_toolkit.h" |
#include "ox_toolkit.h" |
| #include "parse.h" |
#include "parse.h" |
|
|
| 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(); |
| #if defined(WITH_GMP) |
|
| static mpz_ptr parse_mpz_integer(); |
static mpz_ptr parse_mpz_integer(); |
| #endif /* WITH_GMP */ |
|
| static int parse_integer(); |
static int parse_integer(); |
| static char *parse_string(); |
static char *parse_string(); |
| static cmo *parse_cmo_null(); |
static cmo *parse_cmo_null(); |
| Line 63 static cmo *parse_cmo_string(); |
|
| Line 65 static cmo *parse_cmo_string(); |
|
| static cmo *parse_cmo_mathcap(); |
static cmo *parse_cmo_mathcap(); |
| static cmo *parse_cmo_list(); |
static cmo *parse_cmo_list(); |
| static cmo *parse_cmo_monomial32(); |
static cmo *parse_cmo_monomial32(); |
| #if defined(WITH_GMP) |
|
| static cmo *parse_cmo_zz(); |
static cmo *parse_cmo_zz(); |
| #endif /* WITH_GMP */ |
|
| static cmo *parse_cmo_zero(); |
static cmo *parse_cmo_zero(); |
| static cmo *parse_cmo_dms_generic(); |
static cmo *parse_cmo_dms_generic(); |
| static cmo *parse_cmo_ring_by_name(); |
static cmo *parse_cmo_ring_by_name(); |
| Line 80 static ox *parse_ox_data(); |
|
| Line 80 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 105 static void parse_error(char *s) |
|
| Line 110 static void parse_error(char *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 222 static cmo *parse_cmo() |
|
| Line 222 static cmo *parse_cmo() |
|
| token = lex(); |
token = lex(); |
| m = parse_cmo_monomial32(); |
m = parse_cmo_monomial32(); |
| break; |
break; |
| #if defined(WITH_GMP) |
|
| case TOKEN(CMO_ZZ): |
case TOKEN(CMO_ZZ): |
| token = lex(); |
token = lex(); |
| m = parse_cmo_zz(); |
m = parse_cmo_zz(); |
| break; |
break; |
| #endif /* WITH_GMP */ |
|
| case TOKEN(CMO_ZERO): |
case TOKEN(CMO_ZERO): |
| token = lex(); |
token = lex(); |
| m = parse_cmo_zero(); |
m = parse_cmo_zero(); |
| Line 282 static void parse_comma() |
|
| Line 280 static void parse_comma() |
|
| token = lex(); |
token = lex(); |
| } |
} |
| |
|
| #if defined(WITH_GMP) |
|
| 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); |
| #ifdef DEBUG |
|
| free(src); |
|
| #endif |
|
| return z; |
return z; |
| } |
} |
| |
|
| Line 320 static mpz_ptr parse_mpz_integer() |
|
| Line 314 static mpz_ptr parse_mpz_integer() |
|
| if (sign == -1) { |
if (sign == -1) { |
| val = my_mpz_neg(val); |
val = my_mpz_neg(val); |
| } |
} |
| #ifdef DEBUG |
|
| free(yylval.sym); |
|
| #endif |
|
| token = lex(); |
token = lex(); |
| return val; |
return val; |
| } |
} |
| #endif /* WITH_GMP */ |
|
| |
|
| static int parse_integer() |
static int parse_integer() |
| { |
{ |
| #if defined(WITH_GMP) |
#if 0 |
| return mpz_get_si(parse_mpz_integer()); |
return mpz_get_si(parse_mpz_integer()); |
| #else |
#else |
| abort(); /* not implemented */ |
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 |
#endif |
| } |
} |
| |
|
| Line 456 static cmo *parse_cmo_monomial32() |
|
| Line 461 static cmo *parse_cmo_monomial32() |
|
| return (cmo *)m; |
return (cmo *)m; |
| } |
} |
| |
|
| #if defined(WITH_GMP) |
|
| /* the following function rewrite internal data of mpz/cmo_zz. */ |
/* the following function rewrite internal data of mpz/cmo_zz. */ |
| static cmo *parse_cmo_zz() |
static cmo *parse_cmo_zz() |
| { |
{ |
| Line 485 static cmo *parse_cmo_zz() |
|
| Line 489 static cmo *parse_cmo_zz() |
|
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)m; |
return (cmo *)m; |
| } |
} |
| #endif /* WITH_GMP */ |
|
| |
|
| static cmo *parse_cmo_zero() |
static cmo *parse_cmo_zero() |
| { |
{ |
| Line 604 static void init_lex(char *s) |
|
| Line 607 static void init_lex(char *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 625 static char *lex_digit() |
|
| Line 625 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 647 static struct symbol symbol_list[] = { |
|
| Line 647 static struct symbol symbol_list[] = { |
|
| MK_KEY_CMO(CMO_MONOMIAL32), |
MK_KEY_CMO(CMO_MONOMIAL32), |
| MK_KEY_CMO(CMO_ZZ), |
MK_KEY_CMO(CMO_ZZ), |
| MK_KEY_CMO(CMO_ZERO), |
MK_KEY_CMO(CMO_ZERO), |
| |
MK_KEY_CMO(CMO_TREE), |
| MK_KEY_CMO(CMO_DMS_GENERIC), |
MK_KEY_CMO(CMO_DMS_GENERIC), |
| MK_KEY_CMO(CMO_RING_BY_NAME), |
MK_KEY_CMO(CMO_RING_BY_NAME), |
| MK_KEY_CMO(CMO_INDETERMINATE), |
MK_KEY_CMO(CMO_INDETERMINATE), |
| MK_KEY_CMO(CMO_DISTRIBUTED_POLYNOMIAL), |
MK_KEY_CMO(CMO_DISTRIBUTED_POLYNOMIAL), |
| |
MK_KEY_CMO(CMO_POLYNOMIAL_IN_ONE_VARIABLE), |
| |
MK_KEY_CMO(CMO_RECURSIVE_POLYNOMIAL), |
| MK_KEY_CMO(CMO_ERROR2), |
MK_KEY_CMO(CMO_ERROR2), |
| MK_KEY_SM(SM_popCMO), |
MK_KEY_SM(SM_popCMO), |
| MK_KEY_SM(SM_popString), |
MK_KEY_SM(SM_popString), |
| Line 722 static char *lex_quoted_string() |
|
| Line 725 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 789 static int lex() |
|
| Line 792 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)) { |