version 1.1, 1999/10/08 02:12:02 |
version 1.13, 2020/10/06 11:33:46 |
|
|
|
/* $OpenXM: OpenXM/src/kan96xx/Kan/parser.c,v 1.12 2015/10/08 11:49:37 takayama Exp $ */ |
/* |
/* |
parser.c parser for poly.c |
parser.c parser for poly.c |
*/ |
*/ |
|
|
#include <stdio.h> |
#include <stdio.h> |
|
#include <string.h> |
#include "datatype.h" |
#include "datatype.h" |
#include "setjmp.h" |
#include "setjmp.h" |
#include "stackm.h" |
#include "stackm.h" |
#include "extern.h" |
#include "extern.h" |
#include "extern2.h" |
#include "extern2.h" |
|
|
|
|
#define NUM 1 /* NUM means struct Bignum */ |
#define NUM 1 /* NUM means struct Bignum */ |
#define POL 0 |
#define POL 0 |
|
|
Line 21 union valObject { |
|
Line 24 union valObject { |
|
POLY p; |
POLY p; |
}; |
}; |
|
|
|
#if defined(__CYGWIN__) |
|
static sigjmp_buf EnvOfParser; |
|
#else |
static jmp_buf EnvOfParser; |
static jmp_buf EnvOfParser; |
|
#endif |
|
|
static char *String; |
static char *String; |
static int StrPtr = 0; /* String and StrPtr are used in getcharFromStr() */ |
static int StrPtr = 0; /* String and StrPtr are used in getcharFromStr() */ |
Line 34 static int Spv = 0; /* stack pointer */ |
|
Line 40 static int Spv = 0; /* stack pointer */ |
|
#define SSIZE 20000 |
#define SSIZE 20000 |
static int TagStack[SSIZE]; |
static int TagStack[SSIZE]; |
static union valObject ValStack[SSIZE]; |
static union valObject ValStack[SSIZE]; |
#define NAME_MAX 2000 |
#define MY_NAME_MAX 2000 |
static char Name[NAME_MAX]; |
static char Name[MY_NAME_MAX]; |
|
|
static union valObject ValTmp; |
static union valObject ValTmp; |
|
|
Line 100 static union valObject vpop() { |
|
Line 106 static union valObject vpop() { |
|
|
|
|
|
POLY stringToPOLY(s,ringp) |
POLY stringToPOLY(s,ringp) |
char *s; |
char *s; |
struct ring *ringp; |
struct ring *ringp; |
{ |
{ |
/* call init() [ poly.c ] before you use strToPoly(). */ |
/* call init() [ poly.c ] before you use strToPoly(). */ |
POLY ppp; |
POLY ppp; |
Line 118 struct ring *ringp; |
|
Line 124 struct ring *ringp; |
|
Ring0 = *ringp; |
Ring0 = *ringp; |
Ring0.p = 0; |
Ring0.p = 0; |
Ring0.next = (struct ring *)NULL; |
Ring0.next = (struct ring *)NULL; |
|
#if defined(__CYGWIN__) |
if (setjmp(EnvOfParser)) { |
if (MYSIGSETJMP(EnvOfParser,1)) { |
|
#else |
|
if (MYSETJMP(EnvOfParser)) { |
|
#endif |
fprintf(stderr,"\nERROR: You have syntax errors in the expression: %s\n",s); |
fprintf(stderr,"\nERROR: You have syntax errors in the expression: %s\n",s); |
errorKan1("%s\n"," parser.c : Syntax error in the input polynomial."); |
errorKan1("%s\n"," parser.c : Syntax error in the input polynomial."); |
return( POLYNULL ); /* error */ |
return( POLYNULL ); /* error */ |
} else { } |
} else { } |
|
|
ss = (char *)sGC_malloc( strlen(s)+6 ); /* This parser think that an expression |
ss = (char *)sGC_malloc( strlen(s)+6 ); /* This parser think that an expression |
-1+.. is error. ----> |
-1+.. is error. ----> |
Improved at 1992/03/04.*/ |
Improved at 1992/03/04.*/ |
if (ss == (char *)NULL) |
if (ss == (char *)NULL) |
errorParser("No more space."); |
errorParser("No more space."); |
k=0; while (s[k] == ' ') k++; |
k=0; while (s[k] == ' ') k++; |
/* This method is quite adhoc. */ |
/* This method is quite adhoc. */ |
/* |
/* |
if (s[k] == '+' || s[k] == '-') { |
if (s[k] == '+' || s[k] == '-') { |
strcpy(ss,"0"); strcat(ss,&(s[k])); |
strcpy(ss,"0"); strcat(ss,&(s[k])); |
}else strcpy(ss,&(s[k])); |
}else strcpy(ss,&(s[k])); |
*/ |
*/ |
strcpy(ss,&(s[k])); /* we introduce new parser which recognize unary - */ |
strcpy(ss,&(s[k])); /* we introduce new parser which recognize unary - */ |
k = strlen(ss); |
k = strlen(ss); |
Line 168 struct ring *ringp; |
|
Line 177 struct ring *ringp; |
|
|
|
static int mytolower(c) int c; { |
static int mytolower(c) int c; { |
/*if (c>='A' && c<= 'Z') return( c + 0x20 ); |
/*if (c>='A' && c<= 'Z') return( c + 0x20 ); |
else return( c ); */ |
else return( c ); */ |
return(c); /* 1992/06/27 : do nothing now. ***/ |
return(c); /* 1992/06/27 : do nothing now. ***/ |
} |
} |
|
|
Line 184 static int getoken() { |
|
Line 193 static int getoken() { |
|
int i; |
int i; |
if (Ch == '\0') return( -1 ); |
if (Ch == '\0') return( -1 ); |
while (isSpace(Ch)) Ch = getcharFromStr(); |
while (isSpace(Ch)) Ch = getcharFromStr(); |
if (isDigit(Ch)) { |
if (isDigit(Ch)) { |
Symbol = NUM; Bp = 0; |
Symbol = NUM; Bp = 0; |
do { |
do { |
Bwork[Bp] = Ch; Bwork[Bp+1] = '\0'; |
Bwork[Bp] = Ch; Bwork[Bp+1] = '\0'; |
Ch = getcharFromStr(); |
Ch = getcharFromStr(); |
Bp++; |
Bp++; |
if (Bp >= BIGNUM_ILIMIT-1) { |
if (Bp >= BIGNUM_ILIMIT-1) { |
errorParser(" Too big big-num. "); |
errorParser(" Too big big-num. "); |
} |
} |
} while (isDigit(Ch)); |
} while (isDigit(Ch)); |
BigValue = newMP_INT(); |
BigValue = newMP_INT(); |
Line 204 static int getoken() { |
|
Line 213 static int getoken() { |
|
/* Symbol = NUM; Don't do that. */ |
/* Symbol = NUM; Don't do that. */ |
Value = 0; |
Value = 0; |
do { |
do { |
Value = Value*10+(Ch-'0'); |
Value = Value*10+(Ch-'0'); |
Ch = getcharFromStr(); |
Ch = getcharFromStr(); |
} while (isDigit(Ch)); |
} while (isDigit(Ch)); |
}else errorParser(" Number is expected after x or d. "); |
}else errorParser(" Number is expected after x or d. "); |
} else if (Ch == '@') { |
} else if (Ch == '@') { |
Line 213 static int getoken() { |
|
Line 222 static int getoken() { |
|
i = 0; |
i = 0; |
do { |
do { |
Name[i] = Ch; Name[i+1] = '\0'; i++; |
Name[i] = Ch; Name[i+1] = '\0'; i++; |
if (i+2 >= NAME_MAX) { |
if (i+2 >= MY_NAME_MAX) { |
errorParser("Too long name begining with @."); |
errorParser("Too long name begining with @."); |
} |
} |
Ch = getcharFromStr(); |
Ch = getcharFromStr(); |
} while (isAlphabetNum(Ch)); |
} while (isAlphabetNum(Ch)); |
Line 224 static int getoken() { |
|
Line 233 static int getoken() { |
|
} |
} |
|
|
/***/ /* |
/***/ /* |
if (Symbol == NUM) { |
if (Symbol == NUM) { |
fprintf(stderr,"\nToken type = number"); |
fprintf(stderr,"\nToken type = number"); |
} else { |
} else { |
fprintf(stderr,"\nToken type = %c",Symbol); |
fprintf(stderr,"\nToken type = %c",Symbol); |
} |
} |
fprintf(stderr," value is %d ",Value); |
fprintf(stderr," value is %d ",Value); |
*/ |
*/ |
|
|
} |
} |
|
|
Line 247 static void expr() { |
|
Line 256 static void expr() { |
|
|
|
int gtype, ftype; /* data type. NUM or POL */ |
int gtype, ftype; /* data type. NUM or POL */ |
POLY gp; POLY fp; POLY rp; /* g -> gp, f->fp, r -> rp , if they are |
POLY gp; POLY fp; POLY rp; /* g -> gp, f->fp, r -> rp , if they are |
polynomials */ |
polynomials */ |
POLY tmpp; |
POLY tmpp; |
|
|
term(); |
term(); |
Line 273 static void expr() { |
|
Line 282 static void expr() { |
|
fp= (ValTmp).p; |
fp= (ValTmp).p; |
}else ; |
}else ; |
|
|
/* debug */ |
/* debug */ |
/* pWritef(gp,3); printf("\n\n"); sleep(5); */ |
/* pWritef(gp,3); printf("\n\n"); sleep(5); */ |
|
|
if (op == '+') { |
if (op == '+') { |
if (ftype == NUM && gtype == NUM) { |
if (ftype == NUM && gtype == NUM) { |
mpz_add(f,f,g); |
mpz_add(f,f,g); |
utmp.ival = f; |
utmp.ival = f; |
push(NUM,utmp); |
push(NUM,utmp); |
}else if (ftype== POL && gtype == NUM) { |
}else if (ftype== POL && gtype == NUM) { |
rp = ppAdd(fp,bxx(g,0,0,&Ring0)); |
rp = ppAdd(fp,bxx(g,0,0,&Ring0)); |
utmp.p = rp; |
utmp.p = rp; |
push(POL, utmp); |
push(POL, utmp); |
}else if (ftype==NUM && gtype == POL) { |
}else if (ftype==NUM && gtype == POL) { |
rp = ppAdd(bxx(f,0,0,&Ring0),gp); |
rp = ppAdd(bxx(f,0,0,&Ring0),gp); |
utmp.p = rp; |
utmp.p = rp; |
push(POL,utmp); |
push(POL,utmp); |
}else if (ftype==POL && gtype== POL) { |
}else if (ftype==POL && gtype== POL) { |
rp = ppAdd(fp,gp); |
rp = ppAdd(fp,gp); |
utmp.p = rp; |
utmp.p = rp; |
push(POL,utmp); |
push(POL,utmp); |
}else ; |
}else ; |
|
|
}else { |
}else { |
if (ftype == NUM && gtype == NUM) { |
if (ftype == NUM && gtype == NUM) { |
mpz_sub(f,f,g); |
mpz_sub(f,f,g); |
utmp.ival = f; |
utmp.ival = f; |
push(NUM,utmp); |
push(NUM,utmp); |
}else if (ftype== POL && gtype == NUM) { |
}else if (ftype== POL && gtype == NUM) { |
rp = ppSub(fp,bxx(g,0,0,&Ring0)); |
rp = ppSub(fp,bxx(g,0,0,&Ring0)); |
utmp.p = rp; |
utmp.p = rp; |
push(POL, utmp); |
push(POL, utmp); |
}else if (ftype==NUM && gtype == POL) { |
}else if (ftype==NUM && gtype == POL) { |
rp = ppSub(bxx(f,0,0,&Ring0),gp); |
rp = ppSub(bxx(f,0,0,&Ring0),gp); |
utmp.p = rp; |
utmp.p = rp; |
push(POL,utmp); |
push(POL,utmp); |
}else if (ftype==POL && gtype== POL) { |
}else if (ftype==POL && gtype== POL) { |
rp = ppSub(fp,gp); |
rp = ppSub(fp,gp); |
utmp.p = rp; |
utmp.p = rp; |
push(POL,utmp); |
push(POL,utmp); |
}else ; |
}else ; |
|
|
} |
} |
Line 327 static void term() { |
|
Line 336 static void term() { |
|
|
|
int gtype, ftype; /* data type. NUM or POL */ |
int gtype, ftype; /* data type. NUM or POL */ |
POLY gp; POLY fp; POLY rp; /* g -> gp, f->fp, r -> rp , if they are |
POLY gp; POLY fp; POLY rp; /* g -> gp, f->fp, r -> rp , if they are |
polynomials */ |
polynomials */ |
POLY tmpp; |
POLY tmpp; |
|
|
|
|
/* Unary + and -. For example -(1+6), -5*3, so on and so forth. |
/* Unary + and -. For example -(1+6), -5*3, so on and so forth. |
term : factor | |
term : factor | |
( + | - )factor | |
( + | - )factor | |
factor ( * | / ) factor ... | |
factor ( * | / ) factor ... | |
( + | - )factor ( * | / ) factor ... |
( + | - )factor ( * | / ) factor ... |
*/ |
*/ |
if (Symbol == '+') { |
if (Symbol == '+') { |
getoken(); |
getoken(); |
Line 384 static void term() { |
|
Line 393 static void term() { |
|
|
|
if (op == '*') { |
if (op == '*') { |
if (ftype == NUM && gtype == NUM) { |
if (ftype == NUM && gtype == NUM) { |
mpz_mul(f,f,g); |
mpz_mul(f,f,g); |
utmp.ival = f; |
utmp.ival = f; |
push(NUM,utmp); |
push(NUM,utmp); |
}else if (ftype== POL && gtype == NUM) { |
}else if (ftype== POL && gtype == NUM) { |
fp = ppMult(bxx(g,0,0,&Ring0),fp); |
fp = ppMult(bxx(g,0,0,&Ring0),fp); |
utmp.p = fp; |
utmp.p = fp; |
push(POL, utmp); |
push(POL, utmp); |
}else if (ftype==NUM && gtype == POL) { |
}else if (ftype==NUM && gtype == POL) { |
gp = ppMult(bxx(f,0,0,&Ring0),gp); |
gp = ppMult(bxx(f,0,0,&Ring0),gp); |
utmp.p = gp; |
utmp.p = gp; |
push(POL,utmp); |
push(POL,utmp); |
}else if (ftype==POL && gtype== POL) { |
}else if (ftype==POL && gtype== POL) { |
rp = ppMult(fp,gp); |
rp = ppMult(fp,gp); |
utmp.p = rp; |
utmp.p = rp; |
push(POL,utmp); |
push(POL,utmp); |
}else ; |
}else ; |
|
|
}else { |
}else { |
if (ftype == NUM && gtype == NUM) { |
if (ftype == NUM && gtype == NUM) { |
mpz_div(f,f,g); |
errorParser("num/num is not supported yet.\n"); |
utmp.ival = f; |
mpz_div(f,f,g); |
push(NUM,utmp); |
utmp.ival = f; |
|
push(NUM,utmp); |
}else if (ftype== POL && gtype == NUM) { |
}else if (ftype== POL && gtype == NUM) { |
errorParser("POLY / num is not supported yet.\n"); |
errorParser("POLY / num is not supported yet.\n"); |
/*pvCoeffDiv(BbToCoeff(g),fp); |
/*pvCoeffDiv(BbToCoeff(g),fp); |
utmp.p = fp; |
utmp.p = fp; |
push(POL, utmp);*/ |
push(POL, utmp);*/ |
}else if (ftype==NUM && gtype == POL) { |
}else if (ftype==NUM && gtype == POL) { |
errorParser(" / POLY is not supported yet.\n"); |
errorParser(" / POLY is not supported yet.\n"); |
}else if (ftype==POL && gtype== POL) { |
}else if (ftype==POL && gtype== POL) { |
errorParser(" / POLY is not supported yet.\n"); |
errorParser(" / POLY is not supported yet.\n"); |
}else ; |
}else ; |
|
|
} |
} |
Line 428 static void factor() { |
|
Line 438 static void factor() { |
|
|
|
int gtype, ftype; /* data type. NUM or POL */ |
int gtype, ftype; /* data type. NUM or POL */ |
POLY gp; POLY fp; POLY rp; /* g -> gp, f->fp, r -> rp , if they are |
POLY gp; POLY fp; POLY rp; /* g -> gp, f->fp, r -> rp , if they are |
polynomials */ |
polynomials */ |
POLY tmpp; |
POLY tmpp; |
union valObject utmp; |
union valObject utmp; |
|
|
Line 455 static void factor() { |
|
Line 465 static void factor() { |
|
|
|
if (1) { |
if (1) { |
if (ftype == NUM && gtype == NUM) { |
if (ftype == NUM && gtype == NUM) { |
/* printf("\nf=");mpz_out_str(stdout,10,f); |
/* printf("\nf=");mpz_out_str(stdout,10,f); |
printf("\ng=");mpz_out_str(stdout,10,g); */ |
printf("\ng=");mpz_out_str(stdout,10,g); */ |
mpz_pow_ui(f,f,(unsigned long int) mpz_get_si(g)); |
mpz_pow_ui(f,f,(unsigned long int) mpz_get_si(g)); |
utmp.ival = f; |
utmp.ival = f; |
push(NUM,utmp); |
push(NUM,utmp); |
}else if (ftype== POL && gtype == NUM) { |
}else if (ftype== POL && gtype == NUM) { |
rp = pPower(fp,(int)mpz_get_si(g)); |
rp = pPower(fp,(int)mpz_get_si(g)); |
utmp.p = rp; |
utmp.p = rp; |
push(POL,utmp); |
push(POL,utmp); |
}else if (ftype==NUM && gtype == POL) { |
}else if (ftype==NUM && gtype == POL) { |
errorParser("( ) ^ Polynomial is not supported yet.\n"); |
errorParser("( ) ^ Polynomial is not supported yet.\n"); |
}else if (ftype==POL && gtype== POL) { |
}else if (ftype==POL && gtype== POL) { |
errorParser("( ) ^ Polynomial is not supported yet.\n"); |
errorParser("( ) ^ Polynomial is not supported yet.\n"); |
}else ; |
}else ; |
|
|
} |
} |
Line 478 static void factor() { |
|
Line 488 static void factor() { |
|
|
|
static void monom() { |
static void monom() { |
union valObject utmp; |
union valObject utmp; |
struct object obj; |
struct object obj = OINIT; |
POLY f; |
POLY f; |
extern struct context *CurrentContextp; |
extern struct context *CurrentContextp; |
if (Symbol == 'x' || Symbol == 'd') { |
if (Symbol == 'x' || Symbol == 'd') { |
Line 494 static void monom() { |
|
Line 504 static void monom() { |
|
|
|
}else if (Symbol == '@') { |
}else if (Symbol == '@') { |
obj = findUserDictionary(&(Name[1]),hash0(&(Name[1])),hash1(&(Name[1])), |
obj = findUserDictionary(&(Name[1]),hash0(&(Name[1])),hash1(&(Name[1])), |
CurrentContextp); |
CurrentContextp); |
if (isNullObject(obj)) { |
if (isNullObject(obj)) { |
fprintf(stderr,"%s",Name); |
fprintf(stderr,"%s",Name); |
errorParser(" cannot be found in the user dictionary."); |
errorParser(" cannot be found in the user dictionary."); |
Line 509 static void monom() { |
|
Line 519 static void monom() { |
|
push(POL,utmp); |
push(POL,utmp); |
}else{ |
}else{ |
if (f->m->ringp->n != Ring0.n) { |
if (f->m->ringp->n != Ring0.n) { |
fprintf(stderr,"%s ",Name); |
fprintf(stderr,"%s ",Name); |
errorParser("should be a polynomial in the current ring.\n"); |
errorParser("should be a polynomial in the current ring.\n"); |
} |
} |
utmp.p = modulo0(f,&Ring0); |
utmp.p = modulo0(f,&Ring0); |
push(POL,utmp); |
push(POL,utmp); |
Line 559 static void errorParser(s) char s[]; { |
|
Line 569 static void errorParser(s) char s[]; { |
|
extern char *GotoLabel; |
extern char *GotoLabel; |
extern int GotoP; |
extern int GotoP; |
extern int ErrorMessageMode; |
extern int ErrorMessageMode; |
|
extern int RestrictedMode, RestrictedMode_saved; |
int j; |
int j; |
char tmpc[1024]; |
char tmpc[1024]; |
|
RestrictedMode = RestrictedMode_saved; |
|
cancelAlarm(); |
if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { |
if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { |
sprintf(tmpc,"\nError(parser.c): "); |
sprintf(tmpc,"\nError(parser.c): "); |
if (strlen(s) < 1000-strlen(tmpc)) { |
if (strlen(s) < 1000-strlen(tmpc)) { |
Line 569 static void errorParser(s) char s[]; { |
|
Line 582 static void errorParser(s) char s[]; { |
|
strcat(tmpc," The error occured around: "); |
strcat(tmpc," The error occured around: "); |
j = strlen(tmpc); |
j = strlen(tmpc); |
for (i=(((StrPtr-5) >= 0)?StrPtr-5:0) ; |
for (i=(((StrPtr-5) >= 0)?StrPtr-5:0) ; |
(i<StrPtr+10) && (String[i] != '\0'); i++) { |
(i<StrPtr+10) && (String[i] != '\0'); i++) { |
tmpc[j] = String[i]; j++; |
tmpc[j] = String[i]; j++; |
tmpc[j] = '\0'; |
tmpc[j] = '\0'; |
if (j >= 1023) break; |
if (j >= 1023) break; |
Line 580 static void errorParser(s) char s[]; { |
|
Line 593 static void errorParser(s) char s[]; { |
|
fprintf(stderr,"%s\n",s); |
fprintf(stderr,"%s\n",s); |
fprintf(stderr,"The error occured around: "); |
fprintf(stderr,"The error occured around: "); |
for (i=(((StrPtr-5) >= 0)?StrPtr-5:0) ; |
for (i=(((StrPtr-5) >= 0)?StrPtr-5:0) ; |
(i<StrPtr+10) && (String[i] != '\0'); i++) |
(i<StrPtr+10) && (String[i] != '\0'); i++) |
fprintf(stderr,"%c",String[i]); |
fprintf(stderr,"%c",String[i]); |
fprintf(stderr," ..... \n"); |
fprintf(stderr," ..... \n"); |
} |
} |
Line 588 static void errorParser(s) char s[]; { |
|
Line 601 static void errorParser(s) char s[]; { |
|
fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel); |
fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel); |
GotoP = 0; |
GotoP = 0; |
} |
} |
longjmp(EnvOfParser,1); |
#if defined(__CYGWIN__) |
|
MYSIGLONGJMP(EnvOfParser,1); |
|
#else |
|
MYLONGJMP(EnvOfParser,1); |
|
#endif |
} |
} |
|
|