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

Annotation of OpenXM/src/ox_toolkit/parse.c, Revision 1.14

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.14    ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/parse.c,v 1.13 2003/03/30 08:05:23 ohara Exp $ */
1.1       ohara       3:
1.3       ohara       4: /*
                      5:    This module is a parser for OX/CMO expressions.
1.6       ohara       6:    Some commnets are written in Japanese by using the EUC-JP coded
1.3       ohara       7:    character set.
                      8: */
1.1       ohara       9:
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
                     12: #include <string.h>
                     13: #include <sys/param.h>
                     14: #include <setjmp.h>
1.5       ohara      15: #include <ctype.h>
1.4       ohara      16:
                     17: #include "ox_toolkit.h"
1.1       ohara      18: #include "parse.h"
                     19:
1.3       ohara      20: /* --- Parser --- */
                     21: /* Remarks for semantics.
1.1       ohara      22:    CMO_LIST, CMO_STRING は、あらかじめ与えられた要素の個数を無視する.
                     23:    CMO_MONOMIAL32 は無視しない. (つまりおかしいときは構文エラーになる)
                     24: */
                     25:
1.3       ohara      26: /*
                     27:    parse.c では, Lisp 表現された CMO 文字列を読み込み,
1.1       ohara      28:    バイト列を出力する.  中間表現として、cmo 構造体を利用する.
                     29:    parse() はトークンの列から cmo 構造体を生成し、そのポインタを返す.
1.3       ohara      30:    重要なことはパーサ(の各サブルーチン)は
1.1       ohara      31:    常にトークンをひとつ先読みしていると言うことである.
                     32: */
                     33:
                     34: /* 現在読み込み中のトークンを表す. */
                     35: static int token = 0;
                     36:
                     37: /* トークンの属性値. yylval は lex() によってセットされる. */
                     38: static union{
                     39:     int  d;
                     40:     char *sym;
                     41: } yylval;
                     42:
1.3       ohara      43: /*
                     44:    If `pflag_cmo_addrev' sets, then we admit extended CMO expressions.
                     45:    For example, (CMO_STRING, "hello") is not a real CMO expression
                     46:    but it is admitted.
                     47: */
                     48: static int pflag_cmo_addrev = 1;
1.1       ohara      49:
1.3       ohara      50: /* definitions of local functions */
1.5       ohara      51: static void parse_error(char *s);
                     52: static void parse_right_parenthesis();
                     53: static void parse_left_parenthesis();
                     54: static void parse_comma();
1.11      ohara      55: static mpz_ptr parse_mpz_integer();
                     56: static int   parse_integer();
1.1       ohara      57: static char *parse_string();
                     58: static cmo  *parse_cmo_null();
                     59: static cmo  *parse_cmo_int32();
                     60: static cmo  *parse_cmo_string();
                     61: static cmo  *parse_cmo_mathcap();
                     62: static cmo  *parse_cmo_list();
                     63: static cmo  *parse_cmo_monomial32();
                     64: static cmo  *parse_cmo_zz();
                     65: static cmo  *parse_cmo_zero();
                     66: static cmo  *parse_cmo_dms_generic();
                     67: static cmo  *parse_cmo_ring_by_name();
                     68: static cmo  *parse_cmo_distributed_polynomial();
                     69: static cmo  *parse_cmo_indeterminate();
                     70: static cmo  *parse_cmo_error2();
                     71: static cmo  *parse_cmo();
                     72: static int  parse_sm();
                     73: static ox   *parse_ox();
                     74: static ox   *parse_ox_command();
                     75: static ox   *parse_ox_data();
1.5       ohara      76: static void init_lex(char *s);
                     77: static int  lex();
                     78:
1.1       ohara      79:
                     80: static int is_token_cmo(int token)
                     81: {
                     82:     return (token >= MIN_T_CMO && token < MAX_T_CMO) || token == TOKEN(CMO_ERROR2);
                     83: }
                     84:
                     85: static int is_token_sm(int token)
                     86: {
                     87:     return token == TOKEN(SM);
                     88: }
                     89:
                     90: static int is_token_ox(int token)
                     91: {
                     92:     return token >= MIN_T_OX && token < MAX_T_OX;
                     93: }
                     94:
                     95: static jmp_buf env_parse;
                     96:
1.3       ohara      97: /* This is a parsing fault. */
1.5       ohara      98: static void parse_error(char *s)
1.1       ohara      99: {
1.9       ohara     100:     ox_printf("syntax error: %s\n", s);
1.1       ohara     101:     longjmp(env_parse, 1);
                    102: }
                    103:
1.5       ohara     104: void setflag_parse(int flag)
1.2       ohara     105: {
                    106:     pflag_cmo_addrev = flag;
                    107: }
                    108:
1.5       ohara     109: void init_parser(char *s)
1.2       ohara     110: {
1.5       ohara     111:     setflag_parse(PFLAG_ADDREV);
                    112:     init_lex(s);
1.2       ohara     113: }
                    114:
1.1       ohara     115: cmo *parse()
                    116: {
                    117:     cmo *m;
                    118:
                    119:     if (setjmp(env_parse) != 0) {
1.3       ohara     120:         return NULL;
1.5       ohara     121:         /* This is an error. */
1.1       ohara     122:     }
                    123:
1.5       ohara     124:     token = lex();
1.1       ohara     125:     if (token == '(') {
                    126:         token = lex();
                    127:         if (is_token_cmo(token)) {
                    128:             m = parse_cmo();
                    129:         }else if(is_token_ox(token)) {
                    130:             m = parse_ox();
                    131:         }else {
                    132:             parse_error("invalid symbol.");
                    133:         }
                    134:         return m;
                    135:     }
                    136:     return NULL;
                    137: }
                    138:
                    139: static ox *parse_ox()
                    140: {
                    141:     ox *m = NULL;
                    142:
                    143:     switch(token) {
                    144:     case TOKEN(OX_COMMAND):
                    145:         token = lex();
                    146:         m = parse_ox_command();
                    147:         break;
                    148:     case TOKEN(OX_DATA):
                    149:         token = lex();
                    150:         m = parse_ox_data();
                    151:         break;
                    152:     default:
                    153:         parse_error("invalid ox.");
                    154:     }
                    155:     return m;
                    156: }
                    157:
                    158: static ox *parse_ox_data()
                    159: {
                    160:     ox *m;
                    161:
                    162:     parse_comma();
                    163:     parse_left_parenthesis();
                    164:     m = (ox *)new_ox_data(parse_cmo());
                    165:     parse_right_parenthesis();
                    166:     return m;
                    167: }
                    168:
                    169: static ox *parse_ox_command()
                    170: {
                    171:     ox *m;
                    172:
                    173:     parse_comma();
                    174:     parse_left_parenthesis();
                    175:     m = (ox *)new_ox_command(parse_sm());
                    176:     parse_right_parenthesis();
                    177:     return m;
                    178: }
                    179:
                    180: static int parse_sm()
                    181: {
                    182:     int sm_code;
                    183:     if (token != TOKEN(SM)) {
                    184:         parse_error("no opecode.");
                    185:     }
                    186:     sm_code = yylval.d;
                    187:     token = lex();
                    188:     parse_right_parenthesis();
                    189:     return sm_code;
                    190: }
                    191:
                    192: static cmo *parse_cmo()
                    193: {
                    194:     cmo *m = NULL;
                    195:
                    196:     switch(token) {
                    197:     case TOKEN(CMO_NULL):
                    198:         token = lex();
                    199:         m = parse_cmo_null();
                    200:         break;
                    201:     case TOKEN(CMO_INT32):
                    202:         token = lex();
                    203:         m = parse_cmo_int32();
                    204:         break;
                    205:     case TOKEN(CMO_STRING):
                    206:         token = lex();
                    207:         m = parse_cmo_string();
                    208:         break;
                    209:     case TOKEN(CMO_MATHCAP):
                    210:         token = lex();
                    211:         m = parse_cmo_mathcap();
                    212:         break;
                    213:     case TOKEN(CMO_LIST):
                    214:         token = lex();
                    215:         m = parse_cmo_list();
                    216:         break;
                    217:     case TOKEN(CMO_MONOMIAL32):
                    218:         token = lex();
                    219:         m = parse_cmo_monomial32();
                    220:         break;
                    221:     case TOKEN(CMO_ZZ):
                    222:         token = lex();
                    223:         m = parse_cmo_zz();
                    224:         break;
                    225:     case TOKEN(CMO_ZERO):
                    226:         token = lex();
                    227:         m = parse_cmo_zero();
                    228:         break;
                    229:     case TOKEN(CMO_DMS_GENERIC):
                    230:         token = lex();
                    231:         m = parse_cmo_dms_generic();
                    232:         break;
                    233:     case TOKEN(CMO_RING_BY_NAME):
                    234:         token = lex();
                    235:         m = parse_cmo_ring_by_name();
                    236:         break;
                    237:     case TOKEN(CMO_DISTRIBUTED_POLYNOMIAL):
                    238:         token = lex();
                    239:         m = parse_cmo_distributed_polynomial();
                    240:         break;
                    241:     case TOKEN(CMO_INDETERMINATE):
                    242:         token = lex();
                    243:         m = parse_cmo_indeterminate();
                    244:         break;
                    245:     case TOKEN(CMO_ERROR2):
                    246:         token = lex();
                    247:         m = parse_cmo_error2();
                    248:         break;
                    249:     default:
                    250:         parse_error("invalid cmo.");
                    251:     }
                    252:     return m;
                    253: }
                    254:
1.5       ohara     255: static void parse_left_parenthesis()
1.1       ohara     256: {
                    257:     if (token != '(') {
                    258:         parse_error("no left parenthesis.");
                    259:     }
                    260:     token = lex();
                    261: }
                    262:
1.5       ohara     263: static void parse_right_parenthesis()
1.1       ohara     264: {
                    265:     if (token != ')') {
                    266:         parse_error("no right parenthesis.");
                    267:     }
                    268:     token = lex();
                    269: }
                    270:
1.5       ohara     271: static void parse_comma()
1.1       ohara     272: {
                    273:     if (token != ',') {
                    274:         parse_error("no comma.");
                    275:     }
                    276:     token = lex();
                    277: }
                    278:
1.3       ohara     279: static mpz_ptr new_mpz_set_str(char *s)
                    280: {
1.14    ! ohara     281:     mpz_ptr z = MALLOC(sizeof(mpz_t));
1.5       ohara     282:     mpz_init_set_str(z, s, 10);
                    283:     return z;
1.3       ohara     284: }
                    285:
                    286: static mpz_ptr my_mpz_neg(mpz_ptr src)
                    287: {
1.14    ! ohara     288:     mpz_ptr z = MALLOC(sizeof(mpz_t));
1.5       ohara     289:     mpz_init(z);
                    290:     mpz_neg(z, src);
                    291:     return z;
1.3       ohara     292: }
                    293:
1.11      ohara     294: static mpz_ptr parse_mpz_integer()
1.1       ohara     295: {
1.5       ohara     296:     int sign = 1;
                    297:     mpz_ptr val;
1.3       ohara     298:
1.5       ohara     299:     if (token == '+') {
                    300:         token = lex();
                    301:     }else if (token == '-') {
                    302:         sign = -1;
                    303:         token = lex();
                    304:     }
1.3       ohara     305:
                    306:     if (token != T_DIGIT) {
1.1       ohara     307:         parse_error("no integer.");
                    308:     }
1.5       ohara     309:     val = new_mpz_set_str(yylval.sym);
                    310:     if (sign == -1) {
                    311:         val = my_mpz_neg(val);
                    312:     }
1.1       ohara     313:     token = lex();
                    314:     return val;
                    315: }
1.11      ohara     316:
                    317: static int parse_integer()
                    318: {
1.13      ohara     319: #if 0
1.11      ohara     320:     return mpz_get_si(parse_mpz_integer());
                    321: #else
1.12      ohara     322:     int sign = 1;
                    323:     int val;
                    324:
                    325:     if (token == '+') {
                    326:         token = lex();
                    327:     }else if (token == '-') {
                    328:         sign = -1;
                    329:         token = lex();
                    330:     }
                    331:
                    332:     if (token != T_DIGIT) {
                    333:         parse_error("no integer.");
                    334:     }
                    335:     val = sign*atoi(yylval.sym);
                    336:     token = lex();
                    337:     return val;
1.11      ohara     338: #endif
                    339: }
1.1       ohara     340:
                    341: static char *parse_string()
                    342: {
                    343:     char *s;
                    344:     if (token != T_STRING) {
                    345:         parse_error("no string.");
                    346:     }
                    347:     s = yylval.sym;
                    348:     token = lex();
                    349:     return s;
                    350: }
                    351:
                    352: static cmo *parse_cmo_null()
                    353: {
                    354:     parse_right_parenthesis();
                    355:     return (cmo *)new_cmo_null();
                    356: }
                    357:
                    358: static cmo *parse_cmo_int32()
                    359: {
1.11      ohara     360:     int z;
1.1       ohara     361:
                    362:     parse_comma();
1.3       ohara     363:     z = parse_integer();
1.1       ohara     364:     parse_right_parenthesis();
1.11      ohara     365:     return (cmo *)new_cmo_int32(z);
1.1       ohara     366: }
                    367:
                    368: static cmo *parse_cmo_string()
                    369: {
                    370:     cmo_string *m;
                    371:     char *s;
                    372:
                    373:     parse_comma();
1.3       ohara     374:     if (token == T_DIGIT) {
1.1       ohara     375:         parse_integer();
                    376:         parse_comma();
                    377:     }else if (!pflag_cmo_addrev) {
                    378:         parse_error("invalid cmo string.");
                    379:     }
                    380:     s = parse_string();
                    381:     m = new_cmo_string(s);
                    382:     parse_right_parenthesis();
                    383:     return (cmo *)m;
                    384: }
                    385:
                    386: static cmo *parse_cmo_mathcap()
                    387: {
                    388:     cmo *ob;
                    389:
                    390:     parse_comma();
                    391:     parse_left_parenthesis();
                    392:     ob = parse_cmo();
                    393:     parse_right_parenthesis();
                    394:     return (cmo *)new_cmo_mathcap(ob);
                    395: }
                    396:
                    397: static cmo *parse_cmo_list()
                    398: {
                    399:     cmo_list *m = new_cmo_list();
                    400:     cmo *newcmo;
                    401:
                    402:     if (token == ',') {
                    403:         parse_comma();
                    404:
1.3       ohara     405:         if (token == T_DIGIT) {
1.1       ohara     406:             parse_integer();
                    407:             parse_comma();
                    408:         }else if (!pflag_cmo_addrev) {
                    409:             parse_error("invalid cmo_list.");
                    410:         }
                    411:
                    412:         while(token == '(') {
                    413:             parse_left_parenthesis();
                    414:             newcmo = parse_cmo();
1.5       ohara     415:             list_append(m, newcmo);
1.1       ohara     416:             if (token != ',') {
                    417:                 break;
                    418:             }
                    419:             parse_comma();
                    420:         }
                    421:     }else if (!pflag_cmo_addrev) {
                    422:         parse_error("invalid cmo_list.");
                    423:     }
                    424:     parse_right_parenthesis();
                    425:     return (cmo *)m;
                    426: }
                    427:
                    428: static cmo *parse_cmo_monomial32()
                    429: {
                    430:     int size;
                    431:     int i;
                    432:     cmo_monomial32 *m;
                    433:     int tag;
                    434:
                    435:     parse_comma();
1.11      ohara     436:     size = parse_integer();
1.1       ohara     437:     if (size < 0) {
                    438:         parse_error("invalid value.");
                    439:     }
                    440:     m = new_cmo_monomial32_size(size);
                    441:
                    442:     for(i=0; i<size; i++) {
                    443:         parse_comma();
1.11      ohara     444:         m->exps[i] = parse_integer();
1.1       ohara     445:     }
                    446:     parse_comma();
                    447:     parse_left_parenthesis();
                    448:     m->coef = parse_cmo();
                    449:     tag = m->coef->tag;
                    450:
1.3       ohara     451:     /* semantics:
                    452:        The tag of m->coef must be CMO_ZZ or CMO_INT32. */
1.1       ohara     453:     if (tag != CMO_ZZ && tag != CMO_INT32) {
                    454:         parse_error("invalid cmo.");
                    455:     }
                    456:     parse_right_parenthesis();
                    457:     return (cmo *)m;
                    458: }
                    459:
1.3       ohara     460: /* the following function rewrite internal data of mpz/cmo_zz. */
1.1       ohara     461: static cmo *parse_cmo_zz()
                    462: {
                    463:     int length;
                    464:     int i=0;
                    465:     cmo_zz *m= NULL;
1.5       ohara     466:     mpz_ptr z;
1.1       ohara     467:
                    468:     parse_comma();
1.11      ohara     469:     z = parse_mpz_integer();
1.1       ohara     470:     if (token == ',') {
1.5       ohara     471:         length = mpz_get_si(z);
1.1       ohara     472:         m = new_cmo_zz_size(length);
                    473:
                    474:         length = abs(length);
                    475:         for(i=0; i<length; i++) {
                    476:             parse_comma();
1.11      ohara     477:             m->mpz->_mp_d[i] = parse_integer();
1.1       ohara     478:         }
                    479:     }else if (pflag_cmo_addrev) {
1.3       ohara     480:         m = new_cmo_zz_set_mpz(z);
1.1       ohara     481:     }else {
                    482:         parse_error("no comma.");
                    483:     }
                    484:
                    485:     parse_right_parenthesis();
                    486:     return (cmo *)m;
                    487: }
                    488:
                    489: static cmo *parse_cmo_zero()
                    490: {
                    491:     parse_right_parenthesis();
                    492:     return (cmo *)new_cmo_zero();
                    493: }
                    494:
                    495: static cmo *parse_cmo_dms_generic()
                    496: {
                    497:     parse_right_parenthesis();
                    498:     return (cmo *)new_cmo_dms_generic();
                    499: }
                    500:
                    501: static cmo *parse_cmo_ring_by_name()
                    502: {
                    503:     cmo *ob;
                    504:
                    505:     parse_comma();
                    506:     parse_left_parenthesis();
                    507:     ob = parse_cmo();
                    508:
1.3       ohara     509:     /* The ob has a type of CMO_STRING. */
1.1       ohara     510:     if (ob->tag != CMO_STRING) {
                    511:         parse_error("invalid cmo.");
                    512:     }
                    513:     parse_right_parenthesis();
                    514:     return (cmo *)new_cmo_ring_by_name(ob);
                    515: }
                    516:
                    517: static cmo *parse_cmo_distributed_polynomial()
                    518: {
                    519:     cmo_distributed_polynomial *m = new_cmo_distributed_polynomial();
                    520:     cmo *ob;
                    521:     int tag;
                    522:
                    523:     if (token == ',') {
                    524:         parse_comma();
                    525:
1.3       ohara     526:         if (token == T_DIGIT) {
1.1       ohara     527:             parse_integer();
                    528:             parse_comma();
                    529:         }else if (!pflag_cmo_addrev) {
                    530:             parse_error("invalid d-polynomial.");
                    531:         }
                    532:
                    533:         parse_left_parenthesis();
                    534:         m->ringdef = parse_cmo();
                    535:         tag = m->ringdef->tag;
1.3       ohara     536:         /* m->ringdef needs to be a DringDefinition. */
1.1       ohara     537:         if (tag != CMO_RING_BY_NAME && tag != CMO_DMS_GENERIC
                    538:             && tag != CMO_DMS_OF_N_VARIABLES) {
                    539:             parse_error("invalid cmo.");
                    540:         }
                    541:
                    542:         parse_comma();
                    543:
                    544:         while(token == '(') {
                    545:             parse_left_parenthesis();
                    546:             ob = parse_cmo();
                    547:             if (ob->tag != CMO_MONOMIAL32 && ob->tag != CMO_ZERO) {
                    548:                 parse_error("invalid cmo.");
                    549:             }
1.5       ohara     550:             list_append((cmo_list *)m, ob);
1.1       ohara     551:             if (token != ',') {
                    552:                 break;
                    553:             }
                    554:             parse_comma();
                    555:         }
                    556:     }else if (!pflag_cmo_addrev) {
                    557:         parse_error("invalid d-polynomial.");
                    558:     }
                    559:     parse_right_parenthesis();
                    560:     return (cmo *)m;
                    561: }
                    562:
                    563: static cmo *parse_cmo_indeterminate()
                    564: {
                    565:     cmo *ob;
                    566:
                    567:     parse_comma();
                    568:     parse_left_parenthesis();
                    569:     ob = parse_cmo();
                    570:     parse_right_parenthesis();
                    571:     return (cmo *)new_cmo_indeterminate(ob);
                    572: }
                    573:
                    574: static cmo *parse_cmo_error2()
                    575: {
                    576:     cmo *ob;
                    577:
                    578:     parse_comma();
                    579:     parse_left_parenthesis();
                    580:     ob = parse_cmo();
                    581:     parse_right_parenthesis();
                    582:     return (cmo *)new_cmo_error2(ob);
                    583: }
                    584:
1.3       ohara     585: /* --- lexical analyzer --- */
1.1       ohara     586:
1.3       ohara     587: /* A white space is ignored by lexical analyzer. */
1.1       ohara     588: static int c = ' ';
                    589:
1.3       ohara     590: /* getting a character from string. */
1.2       ohara     591: static char *mygetc_ptr;
                    592: static int mygetc()
                    593: {
1.5       ohara     594:     return *mygetc_ptr++;
1.2       ohara     595: }
1.1       ohara     596:
1.5       ohara     597: static void init_lex(char *s)
1.1       ohara     598: {
1.7       ohara     599:     c=' ';
1.2       ohara     600:     mygetc_ptr=s;
1.1       ohara     601: }
                    602:
                    603: #define SIZE_BUFFER  8192
                    604: static char buffer[SIZE_BUFFER];
                    605:
1.14    ! ohara     606: static char *new_string(char *s)
1.1       ohara     607: {
1.14    ! ohara     608:     char *t = MALLOC(strlen(s)+1);
        !           609:     strcpy(t, s);
        !           610:     return t;
1.3       ohara     611: }
                    612:
                    613: /* no measure for buffer overflow */
                    614: static char *lex_digit()
                    615: {
1.5       ohara     616:     static char buff[SIZE_BUFFER];
                    617:     int i;
                    618:
                    619:     for(i=0; i<SIZE_BUFFER-1; i++) {
                    620:         if(isdigit(c)) {
                    621:             buff[i] = c;
                    622:         }else {
                    623:             buff[i] = '\0';
1.14    ! ohara     624:             return new_string(buff);
1.5       ohara     625:         }
1.3       ohara     626:         c = mygetc();
1.5       ohara     627:     }
                    628:     buff[SIZE_BUFFER-1] = '\0';
1.14    ! ohara     629:     return new_string(buff);
1.1       ohara     630: }
                    631:
                    632: #define MK_KEY_CMO(x)  { #x , x  , TOKEN(x)  , IS_CMO }
                    633: #define MK_KEY_SM(x)   { #x , x  , TOKEN(SM) , IS_SM  }
                    634: #define MK_KEY_OX(x)   { #x , x  , TOKEN(x)  , IS_OX  }
                    635:
1.4       ohara     636: static struct symbol symbol_list[] = {
1.1       ohara     637:     MK_KEY_CMO(CMO_NULL),
                    638:     MK_KEY_CMO(CMO_INT32),
                    639:     MK_KEY_CMO(CMO_DATUM),
                    640:     MK_KEY_CMO(CMO_STRING),
                    641:     MK_KEY_CMO(CMO_MATHCAP),
                    642:     MK_KEY_CMO(CMO_LIST),
                    643:     MK_KEY_CMO(CMO_MONOMIAL32),
                    644:     MK_KEY_CMO(CMO_ZZ),
                    645:     MK_KEY_CMO(CMO_ZERO),
                    646:     MK_KEY_CMO(CMO_DMS_GENERIC),
                    647:     MK_KEY_CMO(CMO_RING_BY_NAME),
                    648:     MK_KEY_CMO(CMO_INDETERMINATE),
                    649:     MK_KEY_CMO(CMO_DISTRIBUTED_POLYNOMIAL),
                    650:     MK_KEY_CMO(CMO_ERROR2),
                    651:     MK_KEY_SM(SM_popCMO),
                    652:     MK_KEY_SM(SM_popString),
                    653:     MK_KEY_SM(SM_mathcap),
                    654:     MK_KEY_SM(SM_pops),
                    655:     MK_KEY_SM(SM_executeStringByLocalParser),
                    656:     MK_KEY_SM(SM_executeFunction),
                    657:     MK_KEY_SM(SM_setMathCap),
                    658:     MK_KEY_SM(SM_shutdown),
                    659:     MK_KEY_SM(SM_control_kill),
                    660:     MK_KEY_SM(SM_control_reset_connection),
                    661:     MK_KEY_OX(OX_COMMAND),
                    662:     MK_KEY_OX(OX_DATA),
                    663:     {NULL, 0, 0, 0}        /* a gate keeper */
                    664: };
                    665:
1.4       ohara     666: symbol_t lookup_by_symbol(char *key)
1.1       ohara     667: {
1.4       ohara     668:     symbol_t symp;
1.1       ohara     669:     for(symp = symbol_list; symp->key != NULL; symp++) {
                    670:         if (strcmp(key, symp->key)==0) {
                    671:             return symp;
                    672:         }
                    673:     }
                    674:     return NULL;
                    675: }
                    676:
1.4       ohara     677: symbol_t lookup_by_token(int tok)
1.1       ohara     678: {
1.4       ohara     679:     symbol_t symp;
1.1       ohara     680:     for(symp = symbol_list; symp->key != NULL; symp++) {
                    681:         if (tok == symp->token) {
                    682:             return symp;
                    683:         }
                    684:     }
                    685:     return NULL;
                    686: }
                    687:
1.4       ohara     688: symbol_t lookup_by_tag(int tag)
1.1       ohara     689: {
1.4       ohara     690:     symbol_t symp;
1.1       ohara     691:     for(symp = symbol_list; symp->key != NULL; symp++) {
                    692:         if (tag == symp->tag) {
                    693:             return symp;
                    694:         }
                    695:     }
                    696:     return NULL;
                    697: }
                    698:
1.4       ohara     699: symbol_t lookup(int i)
1.1       ohara     700: {
                    701:     return &symbol_list[i];
                    702: }
                    703:
1.10      ohara     704: char *get_symbol_by_tag(int tag)
1.4       ohara     705: {
1.10      ohara     706:     symbol_t symp = lookup_by_tag(tag);
                    707:     return (symp != NULL)? symp->key: NULL;
1.4       ohara     708: }
                    709:
1.3       ohara     710: /* no measure for buffer overflow */
1.1       ohara     711: static char *lex_quoted_string()
                    712: {
                    713:     int i;
                    714:     char c0 = ' ';
1.3       ohara     715:
1.1       ohara     716:     for (i=0; i<SIZE_BUFFER; i++) {
1.3       ohara     717:         c = mygetc();
1.1       ohara     718:         if(c == '"') {
1.3       ohara     719:             c = mygetc();
1.1       ohara     720:             buffer[i]='\0';
1.14    ! ohara     721:             return new_string(buffer);
1.1       ohara     722:         }else if (c == '\\') {
                    723:             c0 = c;
1.3       ohara     724:             c = mygetc();
1.1       ohara     725:             if (c != '"') {
                    726:                 buffer[i++] = c0;
                    727:             }
                    728:         }
                    729:         buffer[i]=c;
                    730:     }
1.9       ohara     731:     ox_printf("buffer overflow!\n");
1.1       ohara     732:     exit(1);
                    733:     /* return NULL; */
                    734: }
                    735:
                    736: static int token_of_symbol(char *key)
                    737: {
1.4       ohara     738:     symbol_t symp = lookup_by_symbol(key);
1.1       ohara     739:     if (symp != NULL) {
                    740:         yylval.d = symp->tag;
                    741:         return symp->token;
                    742:     }
1.9       ohara     743:     ox_printf("lex error:: \"%s\" is unknown symbol.\n", key);
1.1       ohara     744:     return 0;
                    745: }
                    746:
                    747: static int lex_symbol()
                    748: {
                    749:     int i;
                    750:     for (i=0; i<SIZE_BUFFER; i++) {
                    751:         if (!isalnum(c) && c != '_') {
                    752:             buffer[i]='\0';
                    753:             return token_of_symbol(buffer);
                    754:         }
                    755:         buffer[i]=c;
1.3       ohara     756:         c = mygetc();
1.1       ohara     757:     }
1.9       ohara     758:     ox_printf("buffer overflow!\n");
1.1       ohara     759:     return 0;
                    760: }
                    761:
1.6       ohara     762: /* Remark: prefetching a character before return. */
1.5       ohara     763: static int lex()
1.1       ohara     764: {
                    765:     int c_dash = 0;
                    766:
1.3       ohara     767:     /* white spaces are ignored. */
                    768:     while (isspace(c)) {
                    769:         c = mygetc();
1.1       ohara     770:     }
                    771:
                    772:     switch(c) {
                    773:     case '(':
                    774:     case ')':
                    775:     case ',':
1.5       ohara     776:     case '+':
                    777:     case '-':
1.1       ohara     778:         c_dash = c;
                    779:         c = ' ';
                    780:         return c_dash;
                    781:     case EOF:
1.3       ohara     782:         c = mygetc();
1.1       ohara     783:         return c_dash;
                    784:     case '"':      /* a quoted string! */
                    785:         yylval.sym = lex_quoted_string();
                    786:         return T_STRING;
                    787:     default:
                    788:     }
                    789:
1.3       ohara     790:     if (isalpha(c)) {
1.5       ohara     791:         /* symbols */
1.1       ohara     792:         return lex_symbol();
                    793:     }
                    794:
1.5       ohara     795:     /* digit */
1.1       ohara     796:     if (isdigit(c)){
1.3       ohara     797:         yylval.sym = lex_digit();
                    798:         return T_DIGIT;
1.1       ohara     799:     }
1.3       ohara     800:     c = mygetc();
1.1       ohara     801:     return 0;
                    802: }

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