[BACK]Return to parse.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Diff for /OpenXM/src/ox_toolkit/parse.c between version 1.3 and 1.14

version 1.3, 2000/01/17 19:55:56 version 1.14, 2003/06/02 10:25:57
Line 1 
Line 1 
 /* -*- 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.13 2003/03/30 08:05:23 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.
 */  */
   
Line 12 
Line 12 
 #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 --- */
Line 47  static union{
Line 48  static union{
 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_mpz_integer();
 static mpz_ptr parse_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 72  static int  parse_sm();
Line 73  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 95  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);      ox_printf("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()
Line 114  cmo *parse()
Line 118  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 252  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 260  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 268  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 278  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      return z;
         free(src);  
 #endif  
         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;
   
         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  
         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 335  static cmo *parse_cmo_null()
Line 357  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 374  static cmo *parse_cmo_mathcap()
Line 396  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 412  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 428  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;
   
     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 441  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 444  static cmo *parse_cmo_zz()
Line 463  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_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);
   
         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 497  static cmo *parse_cmo_ring_by_name()
Line 516  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 547  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 591  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)
 {  {
       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 */
 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 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 }
 #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 663  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 674  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 685  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 696  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 *get_symbol_by_tag(int tag)
   {
       symbol_t symp = lookup_by_tag(tag);
       return (symp != NULL)? symp->key: NULL;
   }
   
 /* 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 718  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 709  static char *lex_quoted_string()
Line 728  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; */
 }  }
   
 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;
     }      }
 #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 738  static int lex_symbol()
Line 755  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. */
 int lex()  static int lex()
 {  {
     int c_dash = 0;      int c_dash = 0;
   
Line 756  int lex()
Line 773  int lex()
     case '(':      case '(':
     case ')':      case ')':
     case ',':      case ',':
         case '+':      case '+':
         case '-':      case '-':
         c_dash = c;          c_dash = c;
         c = ' ';          c = ' ';
         return c_dash;          return c_dash;
Line 771  int lex()
Line 788  int lex()
     }      }
   
     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;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.14

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>