| version 1.2, 1999/12/22 11:26:37 |
version 1.3, 2000/01/17 19:55:56 |
|
|
| /* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
| /* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.1 1999/12/09 22:44:56 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.2 1999/12/22 11:26:37 ohara Exp $ */ |
| |
|
| /* OX expression, CMO expression パーサ */ |
/* |
| |
This module is a parser for OX/CMO expressions. |
| |
Some commnets is written in Japanese by using the EUC-JP coded |
| |
character set. |
| |
*/ |
| |
|
| #include <stdio.h> |
#include <stdio.h> |
| #include <stdlib.h> |
#include <stdlib.h> |
|
|
| #include "ox.h" |
#include "ox.h" |
| #include "parse.h" |
#include "parse.h" |
| |
|
| /* --- 構文解析部 --- */ |
/* --- Parser --- */ |
| /* (重要)セマンティックスについての注意. |
/* Remarks for semantics. |
| CMO_LIST, CMO_STRING は、あらかじめ与えられた要素の個数を無視する. |
CMO_LIST, CMO_STRING は、あらかじめ与えられた要素の個数を無視する. |
| CMO_MONOMIAL32 は無視しない. (つまりおかしいときは構文エラーになる) |
CMO_MONOMIAL32 は無視しない. (つまりおかしいときは構文エラーになる) |
| */ |
*/ |
| |
|
| /* parse.c, lex.c では, Lisp 表現された CMO 文字列を読み込み, |
/* |
| |
parse.c では, Lisp 表現された CMO 文字列を読み込み, |
| バイト列を出力する. 中間表現として、cmo 構造体を利用する. |
バイト列を出力する. 中間表現として、cmo 構造体を利用する. |
| parse() はトークンの列から cmo 構造体を生成し、そのポインタを返す. |
parse() はトークンの列から cmo 構造体を生成し、そのポインタを返す. |
| */ |
重要なことはパーサ(の各サブルーチン)は |
| |
|
| /* 重要なことはパーサ(の各サブルーチン)は |
|
| 常にトークンをひとつ先読みしていると言うことである. |
常にトークンをひとつ先読みしていると言うことである. |
| */ |
*/ |
| |
|
|
|
| char *sym; |
char *sym; |
| } yylval; |
} yylval; |
| |
|
| /* pflag_cmo_addrev がセットされていれば、厳密には CMO expression では |
/* |
| ないもの, 例えば (CMO_STRING, "hello") も CMO に変換される. */ |
If `pflag_cmo_addrev' sets, then we admit extended CMO expressions. |
| |
For example, (CMO_STRING, "hello") is not a real CMO expression |
| |
but it is admitted. |
| |
*/ |
| |
static int pflag_cmo_addrev = 1; |
| |
|
| static int pflag_cmo_addrev = 1; /* CMO の省略記法を許すか否かのフラグ */ |
/* definitions of local functions */ |
| |
|
| /* 関数の宣言 */ |
|
| static int parse_error(char *s); |
static int parse_error(char *s); |
| static int parse_lf(); |
static int parse_lf(); |
| static int parse_right_parenthesis(); |
static int parse_right_parenthesis(); |
| static int parse_left_parenthesis(); |
static int parse_left_parenthesis(); |
| static int parse_comma(); |
static int parse_comma(); |
| static int 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(); |
| static cmo *parse_cmo_int32(); |
static cmo *parse_cmo_int32(); |
| Line 85 static int is_token_ox(int token) |
|
| Line 90 static int is_token_ox(int token) |
|
| |
|
| static jmp_buf env_parse; |
static jmp_buf env_parse; |
| |
|
| /* 構文解析に失敗したことを意味する. */ |
/* This is a parsing fault. */ |
| static int parse_error(char *s) |
static int parse_error(char *s) |
| { |
{ |
| fprintf(stderr, "syntax error: %s\n", s); |
fprintf(stderr, "syntax error: %s\n", s); |
| Line 100 int setflag_parse(int flag) |
|
| Line 105 int setflag_parse(int flag) |
|
| int init_parser(char *s) |
int init_parser(char *s) |
| { |
{ |
| setflag_parse(PFLAG_ADDREV); |
setflag_parse(PFLAG_ADDREV); |
| setmode_mygetc(s); |
init_lex(s); |
| } |
} |
| |
|
| /* この部分は書き換え予定. */ |
|
| cmo *parse() |
cmo *parse() |
| { |
{ |
| cmo *m; |
cmo *m; |
| |
|
| if (setjmp(env_parse) != 0) { |
if (setjmp(env_parse) != 0) { |
| return NULL; /* 構文解析に失敗したら NULL を返す. */ |
return NULL; |
| |
/* This is an error. */ |
| } |
} |
| |
|
| do{ |
token = lex(); |
| token = lex(); |
|
| }while (token == '\n'); |
|
| |
|
| if (token == '(') { |
if (token == '(') { |
| token = lex(); |
token = lex(); |
| if (is_token_cmo(token)) { |
if (is_token_cmo(token)) { |
|
|
| }else { |
}else { |
| parse_error("invalid symbol."); |
parse_error("invalid symbol."); |
| } |
} |
| /* parse_lf(); */ |
|
| return m; |
return m; |
| } |
} |
| return NULL; |
return NULL; |
| } |
} |
| |
|
| /* トークンを先読みしない(重要). */ |
|
| static int parse_lf() |
|
| { |
|
| if (token != '\n') { |
|
| parse_error("no new line."); |
|
| } |
|
| return 0; |
|
| } |
|
| |
|
| |
|
| static ox *parse_ox() |
static ox *parse_ox() |
| { |
{ |
| ox *m = NULL; |
ox *m = NULL; |
| Line 194 static int parse_sm() |
|
| Line 185 static int parse_sm() |
|
| return sm_code; |
return sm_code; |
| } |
} |
| |
|
| |
|
| /* 正しい入力ならば, parse_cmo を呼ぶ時点で, token には |
|
| TOKEN(CMO_xxx), TOKEN(OX_xxx) のいずれかがセットされている. */ |
|
| static cmo *parse_cmo() |
static cmo *parse_cmo() |
| { |
{ |
| cmo *m = NULL; |
cmo *m = NULL; |
| Line 284 static int parse_comma() |
|
| Line 272 static int parse_comma() |
|
| token = lex(); |
token = lex(); |
| } |
} |
| |
|
| static int parse_integer() |
static mpz_ptr new_mpz_set_str(char *s) |
| { |
{ |
| int val; |
mpz_ptr z = malloc(sizeof(mpz_t)); |
| if (token != T_INTEGER) { |
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); |
| |
#endif |
| |
return z; |
| |
} |
| |
|
| |
static mpz_ptr parse_integer() |
| |
{ |
| |
int sign = 1; |
| |
mpz_ptr val; |
| |
|
| |
if (token == '+') { |
| |
token = lex(); |
| |
}else if (token == '-') { |
| |
sign = -1; |
| |
token = lex(); |
| |
} |
| |
|
| |
if (token != T_DIGIT) { |
| parse_error("no integer."); |
parse_error("no integer."); |
| } |
} |
| val = yylval.d; |
val = new_mpz_set_str(yylval.sym); |
| |
if (sign == -1) { |
| |
val = my_mpz_neg(val); |
| |
} |
| |
#ifndef DEBUG |
| |
free(yylval.sym); |
| |
#endif |
| token = lex(); |
token = lex(); |
| return val; |
return val; |
| } |
} |
| Line 314 static cmo *parse_cmo_null() |
|
| Line 335 static cmo *parse_cmo_null() |
|
| |
|
| static cmo *parse_cmo_int32() |
static cmo *parse_cmo_int32() |
| { |
{ |
| int i; |
mpz_ptr z; |
| |
|
| parse_comma(); |
parse_comma(); |
| i = parse_integer(); |
z = parse_integer(); |
| parse_right_parenthesis(); |
parse_right_parenthesis(); |
| return (cmo *)new_cmo_int32(i); |
return (cmo *)new_cmo_int32(mpz_get_si(z)); |
| } |
} |
| |
|
| static cmo *parse_cmo_string() |
static cmo *parse_cmo_string() |
| Line 328 static cmo *parse_cmo_string() |
|
| Line 349 static cmo *parse_cmo_string() |
|
| char *s; |
char *s; |
| |
|
| parse_comma(); |
parse_comma(); |
| if (token == T_INTEGER) { |
if (token == T_DIGIT) { |
| parse_integer(); |
parse_integer(); |
| parse_comma(); |
parse_comma(); |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| Line 361 static cmo *parse_cmo_list() |
|
| Line 382 static cmo *parse_cmo_list() |
|
| if (token == ',') { |
if (token == ',') { |
| parse_comma(); |
parse_comma(); |
| |
|
| if (token == T_INTEGER) { |
if (token == T_DIGIT) { |
| parse_integer(); |
parse_integer(); |
| parse_comma(); |
parse_comma(); |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| Line 393 static cmo *parse_cmo_monomial32() |
|
| Line 414 static cmo *parse_cmo_monomial32() |
|
| int tag; |
int tag; |
| |
|
| parse_comma(); |
parse_comma(); |
| size = parse_integer(); |
size = mpz_get_si(parse_integer()); |
| if (size < 0) { |
if (size < 0) { |
| parse_error("invalid value."); |
parse_error("invalid value."); |
| } |
} |
| Line 401 static cmo *parse_cmo_monomial32() |
|
| Line 422 static cmo *parse_cmo_monomial32() |
|
| |
|
| for(i=0; i<size; i++) { |
for(i=0; i<size; i++) { |
| parse_comma(); |
parse_comma(); |
| m->exps[i] = parse_integer(); |
m->exps[i] = mpz_get_si(parse_integer()); |
| } |
} |
| parse_comma(); |
parse_comma(); |
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| m->coef = parse_cmo(); |
m->coef = parse_cmo(); |
| tag = m->coef->tag; |
tag = m->coef->tag; |
| |
|
| /* m->coef は CMO_ZZ 型か CMO_INT32 型でなければならない */ |
/* semantics: |
| |
The tag of m->coef must be CMO_ZZ or CMO_INT32. */ |
| if (tag != CMO_ZZ && tag != CMO_INT32) { |
if (tag != CMO_ZZ && tag != CMO_INT32) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| } |
} |
| Line 416 static cmo *parse_cmo_monomial32() |
|
| Line 438 static cmo *parse_cmo_monomial32() |
|
| return (cmo *)m; |
return (cmo *)m; |
| } |
} |
| |
|
| /* cmo_zz の内部を直接いじる. */ |
/* the following function rewrite internal data of mpz/cmo_zz. */ |
| static cmo *parse_cmo_zz() |
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; |
| |
|
| parse_comma(); |
parse_comma(); |
| length = parse_integer(); |
z = parse_integer(); |
| if (token == ',') { |
if (token == ',') { |
| |
length = mpz_get_si(z); |
| m = new_cmo_zz_size(length); |
m = new_cmo_zz_size(length); |
| |
|
| 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] = parse_integer(); |
m->mpz->_mp_d[i] = mpz_get_si(parse_integer()); |
| } |
} |
| }else if (pflag_cmo_addrev) { |
}else if (pflag_cmo_addrev) { |
| m = new_cmo_zz_set_si(length); |
m = new_cmo_zz_set_mpz(z); |
| }else { |
}else { |
| parse_error("no comma."); |
parse_error("no comma."); |
| } |
} |
| Line 463 static cmo *parse_cmo_ring_by_name() |
|
| Line 487 static cmo *parse_cmo_ring_by_name() |
|
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| ob = parse_cmo(); |
ob = parse_cmo(); |
| |
|
| /* ob は CMO_STRING 型でなければならない */ |
/* The ob has a type of CMO_STRING. */ |
| if (ob->tag != CMO_STRING) { |
if (ob->tag != CMO_STRING) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| } |
} |
| Line 482 static cmo *parse_cmo_distributed_polynomial() |
|
| Line 506 static cmo *parse_cmo_distributed_polynomial() |
|
| if (token == ',') { |
if (token == ',') { |
| parse_comma(); |
parse_comma(); |
| |
|
| if (token == T_INTEGER) { |
if (token == T_DIGIT) { |
| parse_integer(); |
parse_integer(); |
| parse_comma(); |
parse_comma(); |
| }else if (!pflag_cmo_addrev) { |
}else if (!pflag_cmo_addrev) { |
| Line 492 static cmo *parse_cmo_distributed_polynomial() |
|
| Line 516 static cmo *parse_cmo_distributed_polynomial() |
|
| parse_left_parenthesis(); |
parse_left_parenthesis(); |
| m->ringdef = parse_cmo(); |
m->ringdef = parse_cmo(); |
| tag = m->ringdef->tag; |
tag = m->ringdef->tag; |
| /* m->ringdef は DringDefinition でなければならない */ |
/* m->ringdef needs to be a DringDefinition. */ |
| if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC |
if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC |
| && tag != CMO_DMS_OF_N_VARIABLES) { |
&& tag != CMO_DMS_OF_N_VARIABLES) { |
| parse_error("invalid cmo."); |
parse_error("invalid cmo."); |
| Line 541 static cmo *parse_cmo_error2() |
|
| Line 565 static cmo *parse_cmo_error2() |
|
| return (cmo *)new_cmo_error2(ob); |
return (cmo *)new_cmo_error2(ob); |
| } |
} |
| |
|
| /* --- 字句解析部 --- */ |
/* --- lexical analyzer --- */ |
| |
|
| /* lexical analyzer で読み飛ばされる文字なら何を初期値にしてもよい */ |
/* A white space is ignored by lexical analyzer. */ |
| static int c = ' '; |
static int c = ' '; |
| |
|
| /* 一文字読み込む関数 */ |
/* getting a character from string. */ |
| static char *mygetc_ptr; |
static char *mygetc_ptr; |
| static int mygetc() |
static int mygetc() |
| { |
{ |
| return *mygetc_ptr++; |
return *mygetc_ptr++; |
| } |
} |
| |
|
| int setmode_mygetc(char *s) |
int init_lex(char *s) |
| { |
{ |
| mygetc_ptr=s; |
mygetc_ptr=s; |
| } |
} |
| |
|
| static int (*GETC)() = mygetc; |
#define SIZE_BUFFER 8192 |
| |
static char buffer[SIZE_BUFFER]; |
| |
|
| /* 一文字読み込む関数の選択 (ex. setgetc(getchar); ) */ |
static char *mkstr(char *src) |
| int setgetc(int (*foo)()) |
|
| { |
{ |
| if (foo == NULL) { |
int len; |
| GETC = mygetc; |
char *s; |
| }else { |
len = strlen(src); |
| GETC = foo; |
s = malloc(len+1); |
| } |
strcpy(s, src); |
| |
return s; |
| } |
} |
| |
|
| #define SIZE_BUFFER 8192 |
/* no measure for buffer overflow */ |
| static char buffer[SIZE_BUFFER]; |
static char *lex_digit() |
| |
|
| /* 桁溢れの場合の対策はない */ |
|
| static int lex_digit() |
|
| { |
{ |
| int d = 0; |
static char buff[SIZE_BUFFER]; |
| do { |
int i; |
| d = 10*d + (c - '0'); |
char *s; |
| c = GETC(); |
int len; |
| } while(isdigit(c)); |
|
| return d; |
for(i=0; i<SIZE_BUFFER-1; i++) { |
| |
if(isdigit(c)) { |
| |
buff[i] = c; |
| |
}else { |
| |
buff[i] = '\0'; |
| |
return mkstr(buff); |
| |
} |
| |
c = mygetc(); |
| |
} |
| |
buff[SIZE_BUFFER-1] = '\0'; |
| |
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 } |
| Line 656 symbol* lookup(int i) |
|
| Line 688 symbol* lookup(int i) |
|
| return &symbol_list[i]; |
return &symbol_list[i]; |
| } |
} |
| |
|
| /* バッファあふれした場合の対策をちゃんと考えるべき */ |
/* no measure for buffer overflow */ |
| static char *lex_quoted_string() |
static char *lex_quoted_string() |
| { |
{ |
| int i; |
int i; |
| char c0 = ' '; |
char c0 = ' '; |
| char *s = NULL; |
|
| for (i=0; i<SIZE_BUFFER; i++) { |
for (i=0; i<SIZE_BUFFER; i++) { |
| c = GETC(); |
c = mygetc(); |
| if(c == '"') { |
if(c == '"') { |
| s = malloc(i+1); |
c = mygetc(); |
| buffer[i]='\0'; |
buffer[i]='\0'; |
| strcpy(s, buffer); |
return mkstr(buffer); |
| |
|
| c = GETC(); |
|
| return s; |
|
| }else if (c == '\\') { |
}else if (c == '\\') { |
| c0 = c; |
c0 = c; |
| c = GETC(); |
c = mygetc(); |
| if (c != '"') { |
if (c != '"') { |
| buffer[i++] = c0; |
buffer[i++] = c0; |
| } |
} |
| Line 707 static int lex_symbol() |
|
| Line 736 static int lex_symbol() |
|
| return token_of_symbol(buffer); |
return token_of_symbol(buffer); |
| } |
} |
| buffer[i]=c; |
buffer[i]=c; |
| c = GETC(); |
c = mygetc(); |
| } |
} |
| fprintf(stderr, "buffer overflow!\n"); |
fprintf(stderr, "buffer overflow!\n"); |
| return 0; |
return 0; |
|
|
| { |
{ |
| int c_dash = 0; |
int c_dash = 0; |
| |
|
| /* 空白をスキップする. */ |
/* white spaces are ignored. */ |
| while (isspace(c) && c != '\n') { |
while (isspace(c)) { |
| c = GETC(); |
c = mygetc(); |
| } |
} |
| |
|
| switch(c) { |
switch(c) { |
| case '(': |
case '(': |
| case ')': |
case ')': |
| case ',': |
case ',': |
| case '\n': |
case '+': |
| |
case '-': |
| c_dash = c; |
c_dash = c; |
| c = ' '; |
c = ' '; |
| return c_dash; |
return c_dash; |
| case EOF: |
case EOF: |
| c = GETC(); |
c = mygetc(); |
| return c_dash; |
return c_dash; |
| case '"': /* a quoted string! */ |
case '"': /* a quoted string! */ |
| yylval.sym = lex_quoted_string(); |
yylval.sym = lex_quoted_string(); |
|
|
| default: |
default: |
| } |
} |
| |
|
| if (isalpha(c)) { /* 識別子 */ |
if (isalpha(c)) { |
| |
/* symbols */ |
| return lex_symbol(); |
return lex_symbol(); |
| } |
} |
| |
|
| /* 32bit 整数値 */ |
/* digit */ |
| if (isdigit(c)){ |
if (isdigit(c)){ |
| yylval.d = lex_digit(); |
yylval.sym = lex_digit(); |
| return T_INTEGER; |
return T_DIGIT; |
| } |
} |
| if (c == '-') { |
c = mygetc(); |
| c = GETC(); |
|
| while (isspace(c) && c != '\n') { |
|
| c = GETC(); |
|
| } |
|
| if (isdigit(c)){ |
|
| yylval.d = - lex_digit(); |
|
| return T_INTEGER; |
|
| } |
|
| return 0; |
|
| } |
|
| |
|
| c = GETC(); |
|
| return 0; |
return 0; |
| } |
} |