version 1.3, 2000/01/17 19:55:56 |
version 1.5, 2000/10/10 05:23:21 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- 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.4 2000/03/10 12:24:39 ohara Exp $ */ |
|
|
/* |
/* |
This module is a parser for OX/CMO expressions. |
This module is a parser for OX/CMO expressions. |
|
|
#include <string.h> |
#include <string.h> |
#include <sys/param.h> |
#include <sys/param.h> |
#include <setjmp.h> |
#include <setjmp.h> |
#include "oxtag.h" |
#include <ctype.h> |
#include "ox.h" |
|
|
#include "ox_toolkit.h" |
#include "parse.h" |
#include "parse.h" |
|
|
/* --- Parser --- */ |
/* --- Parser --- */ |
|
|
static int pflag_cmo_addrev = 1; |
static int pflag_cmo_addrev = 1; |
|
|
/* definitions of local functions */ |
/* definitions of local functions */ |
static int parse_error(char *s); |
static void parse_error(char *s); |
static int parse_lf(); |
static void parse_right_parenthesis(); |
static int parse_right_parenthesis(); |
static void parse_left_parenthesis(); |
static int parse_left_parenthesis(); |
static void parse_comma(); |
static int parse_comma(); |
|
static mpz_ptr parse_integer(); |
static mpz_ptr parse_integer(); |
static char *parse_string(); |
static char *parse_string(); |
static cmo *parse_cmo_null(); |
static cmo *parse_cmo_null(); |
Line 72 static int parse_sm(); |
|
Line 72 static int parse_sm(); |
|
static ox *parse_ox(); |
static ox *parse_ox(); |
static ox *parse_ox_command(); |
static ox *parse_ox_command(); |
static ox *parse_ox_data(); |
static ox *parse_ox_data(); |
|
static void init_lex(char *s); |
|
static int lex(); |
|
|
|
|
static int is_token_cmo(int token) |
static int is_token_cmo(int token) |
{ |
{ |
return (token >= MIN_T_CMO && token < MAX_T_CMO) || token == TOKEN(CMO_ERROR2); |
return (token >= MIN_T_CMO && token < MAX_T_CMO) || token == TOKEN(CMO_ERROR2); |
Line 91 static int is_token_ox(int token) |
|
Line 94 static int is_token_ox(int token) |
|
static jmp_buf env_parse; |
static jmp_buf env_parse; |
|
|
/* This is a parsing fault. */ |
/* This is a parsing fault. */ |
static int parse_error(char *s) |
static void parse_error(char *s) |
{ |
{ |
fprintf(stderr, "syntax error: %s\n", s); |
fprintf(stderr, "syntax error: %s\n", s); |
longjmp(env_parse, 1); |
longjmp(env_parse, 1); |
} |
} |
|
|
int setflag_parse(int flag) |
void setflag_parse(int flag) |
{ |
{ |
pflag_cmo_addrev = flag; |
pflag_cmo_addrev = flag; |
} |
} |
|
|
int init_parser(char *s) |
void init_parser(char *s) |
{ |
{ |
setflag_parse(PFLAG_ADDREV); |
setflag_parse(PFLAG_ADDREV); |
init_lex(s); |
init_lex(s); |
} |
} |
|
|
cmo *parse() |
cmo *parse() |
|
|
|
|
if (setjmp(env_parse) != 0) { |
if (setjmp(env_parse) != 0) { |
return NULL; |
return NULL; |
/* This is an error. */ |
/* This is an error. */ |
} |
} |
|
|
token = lex(); |
token = lex(); |
if (token == '(') { |
if (token == '(') { |
token = lex(); |
token = lex(); |
if (is_token_cmo(token)) { |
if (is_token_cmo(token)) { |
Line 248 static cmo *parse_cmo() |
|
Line 251 static cmo *parse_cmo() |
|
return m; |
return m; |
} |
} |
|
|
static int parse_left_parenthesis() |
static void parse_left_parenthesis() |
{ |
{ |
if (token != '(') { |
if (token != '(') { |
parse_error("no left parenthesis."); |
parse_error("no left parenthesis."); |
Line 256 static int parse_left_parenthesis() |
|
Line 259 static int parse_left_parenthesis() |
|
token = lex(); |
token = lex(); |
} |
} |
|
|
static int parse_right_parenthesis() |
static void parse_right_parenthesis() |
{ |
{ |
if (token != ')') { |
if (token != ')') { |
parse_error("no right parenthesis."); |
parse_error("no right parenthesis."); |
Line 264 static int parse_right_parenthesis() |
|
Line 267 static int parse_right_parenthesis() |
|
token = lex(); |
token = lex(); |
} |
} |
|
|
static int parse_comma() |
static void parse_comma() |
{ |
{ |
if (token != ',') { |
if (token != ',') { |
parse_error("no comma."); |
parse_error("no comma."); |
Line 274 static int parse_comma() |
|
Line 277 static int 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 |
#ifndef DEBUG |
free(src); |
free(src); |
#endif |
#endif |
return z; |
return z; |
} |
} |
|
|
static mpz_ptr parse_integer() |
static mpz_ptr parse_integer() |
{ |
{ |
int sign = 1; |
int sign = 1; |
mpz_ptr val; |
mpz_ptr val; |
|
|
if (token == '+') { |
if (token == '+') { |
token = lex(); |
token = lex(); |
}else if (token == '-') { |
}else if (token == '-') { |
sign = -1; |
sign = -1; |
token = lex(); |
token = lex(); |
} |
} |
|
|
if (token != T_DIGIT) { |
if (token != T_DIGIT) { |
parse_error("no integer."); |
parse_error("no integer."); |
} |
} |
val = new_mpz_set_str(yylval.sym); |
val = new_mpz_set_str(yylval.sym); |
if (sign == -1) { |
if (sign == -1) { |
val = my_mpz_neg(val); |
val = my_mpz_neg(val); |
} |
} |
#ifndef DEBUG |
#ifndef DEBUG |
free(yylval.sym); |
free(yylval.sym); |
#endif |
#endif |
token = lex(); |
token = lex(); |
return val; |
return val; |
Line 335 static cmo *parse_cmo_null() |
|
Line 338 static cmo *parse_cmo_null() |
|
|
|
static cmo *parse_cmo_int32() |
static cmo *parse_cmo_int32() |
{ |
{ |
mpz_ptr z; |
mpz_ptr z; |
|
|
parse_comma(); |
parse_comma(); |
z = parse_integer(); |
z = parse_integer(); |
Line 374 static cmo *parse_cmo_mathcap() |
|
Line 377 static cmo *parse_cmo_mathcap() |
|
|
|
static cmo *parse_cmo_list() |
static cmo *parse_cmo_list() |
{ |
{ |
int length=0; |
|
int i=0; |
|
cmo_list *m = new_cmo_list(); |
cmo_list *m = new_cmo_list(); |
cmo *newcmo; |
cmo *newcmo; |
|
|
Line 392 static cmo *parse_cmo_list() |
|
Line 393 static cmo *parse_cmo_list() |
|
while(token == '(') { |
while(token == '(') { |
parse_left_parenthesis(); |
parse_left_parenthesis(); |
newcmo = parse_cmo(); |
newcmo = parse_cmo(); |
append_cmo_list(m, newcmo); |
list_append(m, newcmo); |
if (token != ',') { |
if (token != ',') { |
break; |
break; |
} |
} |
Line 408 static cmo *parse_cmo_list() |
|
Line 409 static cmo *parse_cmo_list() |
|
static cmo *parse_cmo_monomial32() |
static cmo *parse_cmo_monomial32() |
{ |
{ |
int size; |
int size; |
int *exps; |
|
int i; |
int i; |
cmo_monomial32 *m; |
cmo_monomial32 *m; |
int tag; |
int tag; |
Line 444 static cmo *parse_cmo_zz() |
|
Line 444 static cmo *parse_cmo_zz() |
|
int length; |
int length; |
int i=0; |
int i=0; |
cmo_zz *m= NULL; |
cmo_zz *m= NULL; |
mpz_ptr z; |
mpz_ptr z; |
|
|
parse_comma(); |
parse_comma(); |
z = parse_integer(); |
z = parse_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); |
|
|
length = abs(length); |
length = abs(length); |
Line 497 static cmo *parse_cmo_ring_by_name() |
|
Line 497 static cmo *parse_cmo_ring_by_name() |
|
|
|
static cmo *parse_cmo_distributed_polynomial() |
static cmo *parse_cmo_distributed_polynomial() |
{ |
{ |
int length=0; |
|
int i=0; |
|
cmo_distributed_polynomial *m = new_cmo_distributed_polynomial(); |
cmo_distributed_polynomial *m = new_cmo_distributed_polynomial(); |
cmo *ob; |
cmo *ob; |
int tag; |
int tag; |
Line 530 static cmo *parse_cmo_distributed_polynomial() |
|
Line 528 static cmo *parse_cmo_distributed_polynomial() |
|
if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { |
if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) { |
parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
} |
} |
append_cmo_list((cmo_list *)m, ob); |
list_append((cmo_list *)m, ob); |
if (token != ',') { |
if (token != ',') { |
break; |
break; |
} |
} |
Line 574 static int c = ' '; |
|
Line 572 static int c = ' '; |
|
static char *mygetc_ptr; |
static char *mygetc_ptr; |
static int mygetc() |
static int mygetc() |
{ |
{ |
return *mygetc_ptr++; |
return *mygetc_ptr++; |
} |
} |
|
|
int init_lex(char *s) |
static void init_lex(char *s) |
{ |
{ |
mygetc_ptr=s; |
mygetc_ptr=s; |
} |
} |
Line 587 static char buffer[SIZE_BUFFER]; |
|
Line 585 static char buffer[SIZE_BUFFER]; |
|
|
|
static char *mkstr(char *src) |
static char *mkstr(char *src) |
{ |
{ |
int len; |
int len; |
char *s; |
char *s; |
len = strlen(src); |
len = strlen(src); |
s = malloc(len+1); |
s = malloc(len+1); |
strcpy(s, src); |
strcpy(s, src); |
return s; |
return s; |
} |
} |
|
|
/* no measure for buffer overflow */ |
/* no measure for buffer overflow */ |
static char *lex_digit() |
static char *lex_digit() |
{ |
{ |
static char buff[SIZE_BUFFER]; |
static char buff[SIZE_BUFFER]; |
int i; |
int i; |
char *s; |
|
int len; |
|
|
|
for(i=0; i<SIZE_BUFFER-1; i++) { |
for(i=0; i<SIZE_BUFFER-1; i++) { |
if(isdigit(c)) { |
if(isdigit(c)) { |
buff[i] = c; |
buff[i] = c; |
}else { |
}else { |
buff[i] = '\0'; |
buff[i] = '\0'; |
return mkstr(buff); |
return mkstr(buff); |
} |
} |
c = mygetc(); |
c = mygetc(); |
} |
} |
buff[SIZE_BUFFER-1] = '\0'; |
buff[SIZE_BUFFER-1] = '\0'; |
return mkstr(buff); |
return mkstr(buff); |
} |
} |
|
|
#define MK_KEY_CMO(x) { #x , x , TOKEN(x) , IS_CMO } |
#define MK_KEY_CMO(x) { #x , x , TOKEN(x) , IS_CMO } |
#define MK_KEY_SM(x) { #x , x , TOKEN(SM) , IS_SM } |
#define MK_KEY_SM(x) { #x , x , TOKEN(SM) , IS_SM } |
#define MK_KEY_OX(x) { #x , x , TOKEN(x) , IS_OX } |
#define MK_KEY_OX(x) { #x , x , TOKEN(x) , IS_OX } |
|
|
static symbol symbol_list[] = { |
static struct symbol symbol_list[] = { |
MK_KEY_CMO(CMO_NULL), |
MK_KEY_CMO(CMO_NULL), |
MK_KEY_CMO(CMO_INT32), |
MK_KEY_CMO(CMO_INT32), |
MK_KEY_CMO(CMO_DATUM), |
MK_KEY_CMO(CMO_DATUM), |
Line 650 static symbol symbol_list[] = { |
|
Line 646 static symbol symbol_list[] = { |
|
{NULL, 0, 0, 0} /* a gate keeper */ |
{NULL, 0, 0, 0} /* a gate keeper */ |
}; |
}; |
|
|
symbol* lookup_by_symbol(char *key) |
symbol_t lookup_by_symbol(char *key) |
{ |
{ |
symbol *symp; |
symbol_t symp; |
for(symp = symbol_list; symp->key != NULL; symp++) { |
for(symp = symbol_list; symp->key != NULL; symp++) { |
if (strcmp(key, symp->key)==0) { |
if (strcmp(key, symp->key)==0) { |
return symp; |
return symp; |
Line 661 symbol* lookup_by_symbol(char *key) |
|
Line 657 symbol* lookup_by_symbol(char *key) |
|
return NULL; |
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++) { |
for(symp = symbol_list; symp->key != NULL; symp++) { |
if (tok == symp->token) { |
if (tok == symp->token) { |
return symp; |
return symp; |
Line 672 symbol* lookup_by_token(int tok) |
|
Line 668 symbol* lookup_by_token(int tok) |
|
return NULL; |
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++) { |
for(symp = symbol_list; symp->key != NULL; symp++) { |
if (tag == symp->tag) { |
if (tag == symp->tag) { |
return symp; |
return symp; |
Line 683 symbol* lookup_by_tag(int tag) |
|
Line 679 symbol* lookup_by_tag(int tag) |
|
return NULL; |
return NULL; |
} |
} |
|
|
symbol* lookup(int i) |
symbol_t lookup(int i) |
{ |
{ |
return &symbol_list[i]; |
return &symbol_list[i]; |
} |
} |
|
|
|
char *symbol_get_key(symbol_t sp) |
|
{ |
|
return sp->key; |
|
} |
|
|
/* no measure for buffer overflow */ |
/* no measure for buffer overflow */ |
static char *lex_quoted_string() |
static char *lex_quoted_string() |
{ |
{ |
Line 699 static char *lex_quoted_string() |
|
Line 700 static char *lex_quoted_string() |
|
if(c == '"') { |
if(c == '"') { |
c = mygetc(); |
c = mygetc(); |
buffer[i]='\0'; |
buffer[i]='\0'; |
return mkstr(buffer); |
return mkstr(buffer); |
}else if (c == '\\') { |
}else if (c == '\\') { |
c0 = c; |
c0 = c; |
c = mygetc(); |
c = mygetc(); |
Line 716 static char *lex_quoted_string() |
|
Line 717 static char *lex_quoted_string() |
|
|
|
static int token_of_symbol(char *key) |
static int token_of_symbol(char *key) |
{ |
{ |
symbol *symp = lookup_by_symbol(key); |
symbol_t symp = lookup_by_symbol(key); |
if (symp != NULL) { |
if (symp != NULL) { |
yylval.d = symp->tag; |
yylval.d = symp->tag; |
return symp->token; |
return symp->token; |
Line 743 static int lex_symbol() |
|
Line 744 static int lex_symbol() |
|
} |
} |
|
|
/* return する前に一文字先読みしておく. */ |
/* return する前に一文字先読みしておく. */ |
int lex() |
static int lex() |
{ |
{ |
int c_dash = 0; |
int c_dash = 0; |
|
|
|
|
case '(': |
case '(': |
case ')': |
case ')': |
case ',': |
case ',': |
case '+': |
case '+': |
case '-': |
case '-': |
c_dash = c; |
c_dash = c; |
c = ' '; |
c = ' '; |
return c_dash; |
return c_dash; |
|
|
} |
} |
|
|
if (isalpha(c)) { |
if (isalpha(c)) { |
/* symbols */ |
/* symbols */ |
return lex_symbol(); |
return lex_symbol(); |
} |
} |
|
|
/* digit */ |
/* digit */ |
if (isdigit(c)){ |
if (isdigit(c)){ |
yylval.sym = lex_digit(); |
yylval.sym = lex_digit(); |
return T_DIGIT; |
return T_DIGIT; |