static void parse_integer(struct csa *csa) { int j, binary; /* parse the keyword 'general', 'integer', or 'binary' */ if (csa->token == T_GENERAL) binary = 0, scan_token(csa); else if (csa->token == T_INTEGER) binary = 0, scan_token(csa); else if (csa->token == T_BINARY) binary = 1, scan_token(csa); else xassert(csa != csa); /* parse list of variables (may be empty) */ while (csa->token == T_NAME) { /* find the corresponding column */ j = find_col(csa, csa->image); /* change kind of the variable */ glp_set_col_kind(csa->P, j, GLP_IV); /* set bounds for the binary variable */ if (binary) #if 0 /* 07/VIII-2013 */ { set_lower_bound(csa, j, 0.0); set_upper_bound(csa, j, 1.0); } #else { set_lower_bound(csa, j, csa->lb[j] == +DBL_MAX ? 0.0 : csa->lb[j]); set_upper_bound(csa, j, csa->ub[j] == -DBL_MAX ? 1.0 : csa->ub[j]); } #endif scan_token(csa); } return; }
static void parse_objective(struct dsa *dsa) { /* parse objective sense */ int k, len; /* parse the keyword 'minimize' or 'maximize' */ if (dsa->token == T_MINIMIZE) lpx_set_obj_dir(dsa->lp, LPX_MIN); else if (dsa->token == T_MAXIMIZE) lpx_set_obj_dir(dsa->lp, LPX_MAX); else xassert(dsa != dsa); scan_token(dsa); /* parse objective name */ if (dsa->token == T_NAME && dsa->c == ':') { /* objective name is followed by a colon */ lpx_set_obj_name(dsa->lp, dsa->image); scan_token(dsa); xassert(dsa->token == T_COLON); scan_token(dsa); } else { /* objective name is not specified; use default */ lpx_set_obj_name(dsa->lp, "obj"); } /* parse linear form */ len = parse_linear_form(dsa); for (k = 1; k <= len; k++) lpx_set_obj_coef(dsa->lp, dsa->ind[k], dsa->val[k]); return; }
static void parse_integer(struct dsa *dsa) { int j, binary; /* parse the keyword 'general', 'integer', or 'binary' */ if (dsa->token == T_GENERAL) binary = 0, scan_token(dsa); else if (dsa->token == T_INTEGER) binary = 0, scan_token(dsa); else if (dsa->token == T_BINARY) binary = 1, scan_token(dsa); else xassert(dsa != dsa); /* parse list of variables (may be empty) */ while (dsa->token == T_NAME) { /* find the corresponding column */ j = find_col(dsa, dsa->image); /* change kind of the variable */ #if 0 lpx_set_class(dsa->lp, LPX_MIP); #endif lpx_set_col_kind(dsa->lp, j, LPX_IV); /* set 0-1 bounds for the binary variable */ if (binary) { set_lower_bound(dsa, j, 0.0); set_upper_bound(dsa, j, 1.0); } scan_token(dsa); } return; }
static void parse_constraints(struct csa *csa) { int i, len, type; double s; /* parse the keyword 'subject to' */ xassert(csa->token == T_SUBJECT_TO); scan_token(csa); loop: /* create new row (constraint) */ i = glp_add_rows(csa->P, 1); /* parse row name */ if (csa->token == T_NAME && csa->c == ':') { /* row name is followed by a colon */ if (glp_find_row(csa->P, csa->image) != 0) error(csa, "constraint '%s' multiply defined\n", csa->image); glp_set_row_name(csa->P, i, csa->image); scan_token(csa); xassert(csa->token == T_COLON); scan_token(csa); } else { /* row name is not specified; use default */ char name[50]; sprintf(name, "r.%d", csa->count); glp_set_row_name(csa->P, i, name); } /* parse linear form */ len = parse_linear_form(csa); glp_set_mat_row(csa->P, i, len, csa->ind, csa->val); /* parse constraint sense */ if (csa->token == T_LE) type = GLP_UP, scan_token(csa); else if (csa->token == T_GE) type = GLP_LO, scan_token(csa); else if (csa->token == T_EQ) type = GLP_FX, scan_token(csa); else error(csa, "missing constraint sense\n"); /* parse right-hand side */ if (csa->token == T_PLUS) s = +1.0, scan_token(csa); else if (csa->token == T_MINUS) s = -1.0, scan_token(csa); else s = +1.0; if (csa->token != T_NUMBER) error(csa, "missing right-hand side\n"); glp_set_row_bnds(csa->P, i, type, s * csa->value, s * csa->value); /* the rest of the current line must be empty */ if (!(csa->c == '\n' || csa->c == EOF)) error(csa, "invalid symbol(s) beyond right-hand side\n"); scan_token(csa); /* if the next token is a sign, numeric constant, or a symbolic name, here is another constraint */ if (csa->token == T_PLUS || csa->token == T_MINUS || csa->token == T_NUMBER || csa->token == T_NAME) goto loop; return; }
/* token */ int ztoken(register os_ptr op) { stream st; stream *s = &st; int code; ref token; switch ( r_type(op) ) { default: return e_typecheck; case t_file: return ztoken_file(op); case t_string: ; } check_read(*op); sread_string(s, op->value.bytes, r_size(op)); switch ( code = scan_token(s, 1, &token) ) { case 0: /* read a token */ { uint pos = stell(s); op->value.bytes += pos; r_inc_size(op, -pos); } push(2); op[-1] = token; make_bool(op, 1); return 0; case 1: /* no tokens */ make_bool(op, 0); return 0; default: /* error */ return code; } }
int main (int argc, char **argv) { program_name = basename (argv[0]); scan_options (argc, argv); stack *stack = new_stack (); token *scanner = new_token (stdin); for (;;) { int token = scan_token (scanner); if (token == EOF) break; switch (token) { case NUMBER: do_push (stack, peek_token (scanner)); break; case '+': do_binop (stack, add_bigint); break; case '-': do_binop (stack, sub_bigint); break; case '*': do_binop (stack, mul_bigint); break; case 'c': do_clear (stack); break; case 'f': do_print_all (stack); break; case 'p': do_print (stack); break; default: unimplemented (token); break; } } do_clear(stack); free_stack(stack); free_token(scanner); DEBUGF ('m', "EXIT %d\n", exit_status); return EXIT_SUCCESS; }
tokenList * scanner(const char * mplFileName) { FILE *mplFp; tokenList *tokenListHp = NULL; tokenList *tokenListIp = NULL; tokenCodeStr *retScanTokenp = NULL; if ((mplFp = fopen(mplFileName, "r")) == NULL) { fprintf(stderr, "mpl-file open is FAILED.\n"); exit(EXIT_FAILURE); } tokenListHp = malloc_tokenList(); tokenListIp = tokenListHp; if (tokenListHp != NULL) { while (((retScanTokenp = scan_token(mplFp)) != NULL) && (tokenListIp != NULL)) { if (retScanTokenp->tokenCode == T_ERROR) { break; } tokenListIp = append_tokenList(tokenListIp, retScanTokenp); free(retScanTokenp); retScanTokenp = NULL; } } fclose(mplFp); return tokenListHp; }
static int post_preproc( char * out ) /* * Convert digraphs and double '\\' of the second byte of SJIS (BIGFIVE or * ISO2022_JP). * Note: Output of -K option embeds macro informations into comments. * scan_token() does not recognize comment and parses it as '/', '*', etc. */ { #if ! HAVE_DIGRAPHS int di_count = 0; #endif int token_type; int c; char * str; char * cp = out; unget_string( out, NULL); while ((c = get_ch()) != '\n') { /* Not to read over to next line */ if (char_type[ c] & HSP) { *cp++ = c; continue; } str = cp; token_type = scan_token( c, &cp, out_wend); switch (token_type) { #if ! MBCHAR_IS_ESCAPE_FREE case WSTR : case WCHR : str++; /* Skip prefix 'L' */ /* Fall through */ case STR : case CHR : if (bsl_need_escape) cp = esc_mbchar( str, cp); break; #endif /* ! MBCHAR_IS_ESCAPE_FREE */ #if ! HAVE_DIGRAPHS case OPE : if (mcpp_mode == STD && (openum & OP_DIGRAPH)) { cp = conv_a_digraph( cp); /* Convert a digraph */ di_count++; } break; #endif } } *cp++ = '\n'; *cp = EOS; #if ! HAVE_DIGRAPHS if (mcpp_mode == STD && di_count && (warn_level & 16)) cwarn( "%.0s%ld digraph(s) converted" /* _W16_ */ , NULL, (long) di_count, NULL); #endif return 0; }
/* Scan one frame of the file to compute the frame size for each point. */ int icp_framesize(gzFile infile, int *rows, int *columns, int *values) { char line[MAX_LINE], token[MAX_LINE]; const char *pline; int ch, ncomma=0, nsemicolon=0, nvalues=0; z_off_t restore_pos; /* Save position */ restore_pos = gztell(infile); /* Get the values line */ if (gzgets(infile, line, sizeof(line)-1) == NULL) return ICP_READ_ERROR; line[sizeof(line)-1] = '\0'; /* Scan the first detector frame */ ch = gzgetc(infile); /* Skip format column character */ if (ch >= 0) ch = gzgetc(infile); /* Peek at first frame character */ if (ch == ' ' || ch == '-' || ch < 0) { /* Empty frame, so return size 0x0 */ *rows = *columns = 0; } else { /* Frame isn't empty so count number of commas and semicolons in frame. */ while (ch >= 0) { ch = gzgetc(infile); if (ch == ',') ncomma++; else if (ch == ';') nsemicolon++; else if (ch == '\r') /* ignored */ ; else if (ch == '\n') { ch = gzgetc(infile); /* Skip format column character */ if (ch >= 0) ch = gzgetc(infile); if (ch==' ' || ch=='-') break; } } /* last row does not end in semicolon so add 1 * last column does not end in comma so add 1 */ *rows = nsemicolon + 1; *columns = (ncomma / *rows) + 1; } /* Count the number of tokens on the line */ //printf("line=%s\n",line); pline = line; while (pline) { pline = scan_token(pline, sizeof(token), token); if (!*token || *token == '\n') break; nvalues++; //printf("values=%s\n",token); } *values = nvalues; /* Restore position */ gzseek(infile, restore_pos, SEEK_SET); return ICP_GOOD; }
static void devide_line( char * out /* 'out' is 'output' in actual */ ) /* * Devide a too long line into output lines shorter than NWORK. * This routine is called from putout(). */ { FILEINFO * file; char * save; char * wp; int c; file = unget_string( out, NULL); /* To re-read the line */ wp = out_ptr = out; while ((c = get_ch()), file == infile) { if (char_type[ c] & HSP) { if (keep_spaces || out == out_ptr || (char_type[ *(out_ptr - 1) & UCHARMAX] & HSP)) { *out_ptr++ = c; wp++; } continue; } scan_token( c, &wp, out_wend); /* Read a token */ if (NWORK-2 < wp - out_ptr) { /* Too long a token */ cfatal( "Too long token %s", out_ptr, 0L, NULL); /* _F_ */ } else if (out_end <= wp) { /* Too long line */ if (mcpp_debug & MACRO_CALL) { /* -K option */ /* Other than GCC or Visual C */ /* scan_token() scans a comment as sequence of some */ /* tokens such as '/', '*', ..., '*', '/', since it */ /* does not expect comment. */ save = out_ptr; while ((save = strrchr( save, '/')) != NULL) { if (*(save - 1) == '*') { /* '*' '/' sequence */ out_ptr = save + 1; /* Devide at the end*/ break; /* of a comment*/ } } } save = save_string( out_ptr); /* Save the token */ *out_ptr++ = '\n'; /* Append newline */ *out_ptr = EOS; put_a_line( out); /* Putout the former tokens */ wp = out_ptr = stpcpy( out, save); /* Restore the token */ free( save); } else { /* Still in size */ out_ptr = wp; /* Advance the pointer */ } } unget_ch(); /* Push back the source character */ put_a_line( out); /* Putout the last tokens */ sharp( NULL, 0); /* Correct line number */ }
static int parse_linear_form(struct dsa *dsa) { int j, k, len = 0, newlen; double s, coef; loop: /* parse an optional sign */ if (dsa->token == T_PLUS) s = +1.0, scan_token(dsa); else if (dsa->token == T_MINUS) s = -1.0, scan_token(dsa); else s = +1.0; /* parse an optional coefficient */ if (dsa->token == T_NUMBER) coef = dsa->value, scan_token(dsa); else coef = 1.0; /* parse a variable name */ if (dsa->token != T_NAME) fatal(dsa, "missing variable name"); /* find the corresponding column */ j = find_col(dsa, dsa->image); /* check if the variable is already used in the linear form */ if (dsa->map[j]) fatal(dsa, "multiple use of variable `%s' not allowed", dsa->image); /* mark that the variable is used in the linear form */ dsa->map[j] = 1; /* add new term to the linear form */ len++, dsa->ind[len] = j, dsa->val[len] = s * coef; scan_token(dsa); /* if the next token is a sign, there is another term */ if (dsa->token == T_PLUS || dsa->token == T_MINUS) goto loop; /* clear marks of the variables used in the linear form */ for (k = 1; k <= len; k++) dsa->map[dsa->ind[k]] = 0; /* remove zero coefficients */ newlen = 0; for (k = 1; k <= len; k++) { if (dsa->val[k] != 0.0) { newlen++; dsa->ind[newlen] = dsa->ind[k]; dsa->val[newlen] = dsa->val[k]; } } return newlen; }
void read_token() { clear_variant(&value); token = next_token; value = next_value; init_variant(&next_value); next_token = scan_token(&next_value); }
void advance(void) { parser.previous = parser.current; while (1) { parser.current = scan_token(); if (parser.current.type != TOKEN_ERROR) break; error_at_current(parser.current.start); } }
static int scan_number(struct dsa *dsa, int across, double *val) { if (scan_token(dsa, across)) return 1; if (strlen(dsa->token) == 0) { xprintf("%s:%d: missing number\n", dsa->fname, dsa->seqn); return 1; } if (str2num(dsa->token, val)) { xprintf("%s:%d: number `%s' invalid\n", dsa->fname, dsa->seqn, dsa->token); return 1; } return 0; }
static int scan_integer(struct dsa *dsa, int across, int *val) { if (scan_token(dsa, across)) return 1; if (strlen(dsa->token) == 0) { xprintf("%s:%d: missing integer\n", dsa->fname, dsa->seqn); return 1; } if (str2int(dsa->token, val)) { xprintf("%s:%d: integer `%s' invalid\n", dsa->fname, dsa->seqn , dsa->token); return 1; } return 0; }
/* Common code for token reading + execution. */ static int tokenexec_continue(i_ctx_t *i_ctx_p, scanner_state * pstate, bool save) { os_ptr op; int code; /* Note that scan_token may change osp! */ pop(1); again: check_estack(1); code = scan_token(i_ctx_p, (ref *) (esp + 1), pstate); op = osp; switch (code) { case 0: if (r_is_proc(esp + 1)) { /* Treat procedure as a literal. */ push(1); ref_assign(op, esp + 1); code = 0; break; } /* falls through */ case scan_BOS: ++esp; code = o_push_estack; break; case scan_EOF: /* no tokens */ code = 0; break; case scan_Refill: /* need more data */ code = scan_handle_refill(i_ctx_p, pstate, save, ztokenexec_continue); switch (code) { case 0: /* state is not copied to the heap */ goto again; case o_push_estack: return code; } break; /* error */ case scan_Comment: case scan_DSC_Comment: return ztoken_handle_comment(i_ctx_p, pstate, esp + 1, code, save, true, ztokenexec_continue); default: /* error */ scanner_error_object(i_ctx_p, pstate, &i_ctx_p->error_object); break; } if (!save) { /* Deallocate the scanner state record. */ ifree_object(pstate, "token_continue"); } return code; }
/* Common code for token reading. */ static int token_continue(i_ctx_t *i_ctx_p, scanner_state * pstate, bool save) { os_ptr op = osp; int code; ref token; /* Note that scan_token may change osp! */ pop(1); /* remove the file or scanner state */ again: code = scan_token(i_ctx_p, &token, pstate); op = osp; switch (code) { default: /* error */ if (code > 0) /* comment, not possible */ code = gs_note_error(e_syntaxerror); scanner_error_object(i_ctx_p, pstate, &i_ctx_p->error_object); break; case scan_BOS: code = 0; case 0: /* read a token */ push(2); ref_assign(op - 1, &token); make_true(op); break; case scan_EOF: /* no tokens */ push(1); make_false(op); code = 0; break; case scan_Refill: /* need more data */ code = scan_handle_refill(i_ctx_p, pstate, save, ztoken_continue); switch (code) { case 0: /* state is not copied to the heap */ goto again; case o_push_estack: return code; } break; /* error */ } if (code <= 0 && !save) { /* Deallocate the scanner state record. */ ifree_object(pstate, "token_continue"); } return code; }
/* Scan the header text of the icp file for the number of points */ static int numpoints(const char header[]) { char token[80]; header = scan_token(header, sizeof(token), token); /* Filename */ header = scan_token(header, sizeof(token), token); /* Date */ header = scan_token(header, sizeof(token), token); /* Scan */ header = scan_token(header, sizeof(token), token); /* Mon */ header = scan_token(header, sizeof(token), token); /* Prf */ header = scan_token(header, sizeof(token), token); /* Base */ header = scan_token(header, sizeof(token), token); /* #pts */ if (header) { return atoi(token); } else { return 0; } }
/* * get_token * wrapper function to call the main scanner routine, but use * any and all buffered tokens first (this should allow the parser * to push a set of tokens back, and allow back-tracking). * * returns type of token returned */ TokenType get_token(struct Token *inToken, struct ScanData *data) { struct StackNode *newnode; /* get top of stack */ newnode = data->tokBuf; /* check empty stack */ if (newnode == NULL) { /* empty stack, try scanning instead */ return scan_token(inToken, data); } /* otherwise pull token and copy contents */ memcpy((char*)inToken,(char*)(&(newnode->token)),sizeof(struct Token)); data->tokBuf = newnode->next; free(newnode); /* delete this node */ /* success */ return inToken->type; }
/* Read the next set of ICP columns from the file */ int icp_readmotors(gzFile infile, int nvector, Real vector[], int *linenum) { char line[MAX_LINE], token[MAX_LINE]; const char *pline; int nvalues = 0; /* Initialize to zeros */ /* TODO: should initialize to NaNs */ memset(vector, 0, sizeof(*vector)*nvector); /* Get the vector line */ if (gzeof(infile)) return ICP_EOF; if (gzgets(infile, line, sizeof(line)-1) == NULL) { #if 1 /* gets failed: can't distinguish read errors from EOF */ return ICP_EOF; #else /* gets failed: are we at the end of the file? */ if (gzeof(infile)) return ICP_EOF; else return ICP_READ_ERROR; #endif } line[sizeof(line)-1]='\0'; (*linenum)++; /* Tokenize and convert to numbers. */ pline = line; while (pline) { pline = scan_token(pline, sizeof(token), token); if (!*token || *token == '\n') break; if (nvalues >= nvector) return ICP_VECTOR_ERROR; vector[nvalues++] = atof(token); // printf("linenum:%d token %d:%s value:%g\n", *linenum, nvalues, token, atof(token)); } if (nvalues < nvector) return ICP_VECTOR_ERROR; return ICP_GOOD; }
int parse(const char* file, program_t* p) { int r = 0; FILE* fp = NULL; prog = p; prog->file = strdup(file); assert(prog != NULL); if (file != NULL) { fp = fopen(file, "r"); if (fp == NULL) { fprintf(stderr, "Failed to open %s\n", file); return -1; } } else { fp = stdin; file = "<stdin>"; } r = start_scan(fp, file); if (r < 0) { return r; } init_variant(&value); init_variant(&next_value); next_token = scan_token(&next_value); read_program(); return result; }
LPX *lpx_read_cpxlp(const char *fname) { /* read problem data in CPLEX LP format */ struct dsa _dsa, *dsa = &_dsa; if (setjmp(dsa->jump)) goto fail; dsa->lp = NULL; dsa->fname = fname; dsa->fp = NULL; dsa->count = 0; dsa->c = '\n'; dsa->token = T_EOF; dsa->image[0] = '\0'; dsa->imlen = 0; dsa->value = 0.0; dsa->n_max = 100; dsa->map = xcalloc(1+dsa->n_max, sizeof(int)); memset(&dsa->map[1], 0, dsa->n_max * sizeof(int)); dsa->ind = xcalloc(1+dsa->n_max, sizeof(int)); dsa->val = xcalloc(1+dsa->n_max, sizeof(double)); dsa->lb = xcalloc(1+dsa->n_max, sizeof(double)); dsa->ub = xcalloc(1+dsa->n_max, sizeof(double)); print("lpx_read_cpxlp: reading problem data from `%s'...", dsa->fname); dsa->fp = xfopen(dsa->fname, "r"); if (dsa->fp == NULL) { print("lpx_read_cpxlp: unable to open `%s' - %s", dsa->fname, strerror(errno)); goto fail; } dsa->lp = lpx_create_prob(); lpx_create_index(dsa->lp); #if 0 /* read very first character */ read_char(dsa); #endif /* scan very first token */ scan_token(dsa); /* parse definition of the objective function */ if (!(dsa->token == T_MINIMIZE || dsa->token == T_MAXIMIZE)) fatal(dsa, "`minimize' or `maximize' keyword missing"); parse_objective(dsa); /* parse constraints section */ if (dsa->token != T_SUBJECT_TO) fatal(dsa, "constraints section missing"); parse_constraints(dsa); /* parse optional bounds section */ if (dsa->token == T_BOUNDS) parse_bounds(dsa); /* parse optional general, integer, and binary sections */ while (dsa->token == T_GENERAL || dsa->token == T_INTEGER || dsa->token == T_BINARY) parse_integer(dsa); /* check for the keyword 'end' */ if (dsa->token == T_END) scan_token(dsa); else if (dsa->token == T_EOF) print("%s:%d: warning: keyword `end' missing", dsa->fname, dsa->count); else fatal(dsa, "symbol `%s' in wrong position", dsa->image); /* nothing must follow the keyword 'end' (except comments) */ if (dsa->token != T_EOF) fatal(dsa, "extra symbol(s) detected beyond `end'"); /* set bounds of variables */ { int j, type; double lb, ub; for (j = lpx_get_num_cols(dsa->lp); j >= 1; j--) { lb = dsa->lb[j]; ub = dsa->ub[j]; if (lb == +DBL_MAX) lb = 0.0; /* default lb */ if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */ if (lb == -DBL_MAX && ub == +DBL_MAX) type = LPX_FR; else if (ub == +DBL_MAX) type = LPX_LO; else if (lb == -DBL_MAX) type = LPX_UP; else if (lb != ub) type = LPX_DB; else type = LPX_FX; lpx_set_col_bnds(dsa->lp, j, type, lb, ub); } } /* print some statistics */ { int m = lpx_get_num_rows(dsa->lp); int n = lpx_get_num_cols(dsa->lp); int nnz = lpx_get_num_nz(dsa->lp); print("lpx_read_cpxlp: %d row%s, %d column%s, %d non-zero%s", m, m == 1 ? "" : "s", n, n == 1 ? "" : "s", nnz, nnz == 1 ? "" : "s"); } if (lpx_get_class(dsa->lp) == LPX_MIP) { int ni = lpx_get_num_int(dsa->lp); int nb = lpx_get_num_bin(dsa->lp); char s[50]; if (nb == 0) strcpy(s, "none of"); else if (ni == 1 && nb == 1) strcpy(s, ""); else if (nb == 1) strcpy(s, "one of"); else if (nb == ni) strcpy(s, "all of"); else sprintf(s, "%d of", nb); print("lpx_read_cpxlp: %d integer column%s, %s which %s binary" , ni, ni == 1 ? "" : "s", s, nb == 1 ? "is" : "are"); } print("lpx_read_cpxlp: %d lines were read", dsa->count); xfclose(dsa->fp); xfree(dsa->map); xfree(dsa->ind); xfree(dsa->val); xfree(dsa->lb); xfree(dsa->ub); lpx_delete_index(dsa->lp); lpx_order_matrix(dsa->lp); return dsa->lp; fail: if (dsa->lp != NULL) lpx_delete_prob(dsa->lp); if (dsa->fp != NULL) xfclose(dsa->fp); if (dsa->map != NULL) xfree(dsa->map); if (dsa->ind != NULL) xfree(dsa->ind); if (dsa->val != NULL) xfree(dsa->val); if (dsa->lb != NULL) xfree(dsa->lb); if (dsa->ub != NULL) xfree(dsa->ub); return NULL; }
static void parse_bounds(struct dsa *dsa) { int j, lb_flag; double lb, s; /* parse the keyword 'bounds' */ xassert(dsa->token == T_BOUNDS); scan_token(dsa); loop: /* bound definition can start with a sign, numeric constant, or a symbolic name */ if (!(dsa->token == T_PLUS || dsa->token == T_MINUS || dsa->token == T_NUMBER || dsa->token == T_NAME)) goto done; /* parse bound definition */ if (dsa->token == T_PLUS || dsa->token == T_MINUS) { /* parse signed lower bound */ lb_flag = 1; s = (dsa->token == T_PLUS ? +1.0 : -1.0); scan_token(dsa); if (dsa->token == T_NUMBER) lb = s * dsa->value, scan_token(dsa); else if (the_same(dsa->image, "infinity") || the_same(dsa->image, "inf")) { if (s > 0.0) fatal(dsa, "invalid use of `+inf' as lower bound"); lb = -DBL_MAX, scan_token(dsa); } else fatal(dsa, "missing lower bound"); } else if (dsa->token == T_NUMBER) { /* parse unsigned lower bound */ lb_flag = 1; lb = dsa->value, scan_token(dsa); } else { /* lower bound is not specified */ lb_flag = 0; } /* parse the token that should follow the lower bound */ if (lb_flag) { if (dsa->token != T_LE) fatal(dsa, "missing `<', `<=', or `=<' after lower bound"); scan_token(dsa); } /* parse variable name */ if (dsa->token != T_NAME) fatal(dsa, "missing variable name"); j = find_col(dsa, dsa->image); /* set lower bound */ if (lb_flag) set_lower_bound(dsa, j, lb); scan_token(dsa); /* parse the context that follows the variable name */ if (dsa->token == T_LE) { /* parse upper bound */ scan_token(dsa); if (dsa->token == T_PLUS || dsa->token == T_MINUS) { /* parse signed upper bound */ s = (dsa->token == T_PLUS ? +1.0 : -1.0); scan_token(dsa); if (dsa->token == T_NUMBER) { set_upper_bound(dsa, j, s * dsa->value); scan_token(dsa); } else if (the_same(dsa->image, "infinity") || the_same(dsa->image, "inf")) { if (s < 0.0) fatal(dsa, "invalid use of `-inf' as upper bound"); set_upper_bound(dsa, j, +DBL_MAX); scan_token(dsa); } else fatal(dsa, "missing upper bound"); } else if (dsa->token == T_NUMBER) { /* parse unsigned upper bound */ set_upper_bound(dsa, j, dsa->value); scan_token(dsa); } else fatal(dsa, "missing upper bound"); } else if (dsa->token == T_GE) { /* parse lower bound */ if (lb_flag) { /* the context '... <= x >= ...' is invalid */ fatal(dsa, "invalid bound definition"); } scan_token(dsa); if (dsa->token == T_PLUS || dsa->token == T_MINUS) { /* parse signed lower bound */ s = (dsa->token == T_PLUS ? +1.0 : -1.0); scan_token(dsa); if (dsa->token == T_NUMBER) { set_lower_bound(dsa, j, s * dsa->value); scan_token(dsa); } else if (the_same(dsa->image, "infinity") || the_same(dsa->image, "inf") == 0) { if (s > 0.0) fatal(dsa, "invalid use of `+inf' as lower bound"); set_lower_bound(dsa, j, -DBL_MAX); scan_token(dsa); } else fatal(dsa, "missing lower bound"); } else if (dsa->token == T_NUMBER) { /* parse unsigned lower bound */ set_lower_bound(dsa, j, dsa->value); scan_token(dsa); } else fatal(dsa, "missing lower bound"); } else if (dsa->token == T_EQ) { /* parse fixed value */ if (lb_flag) { /* the context '... <= x = ...' is invalid */ fatal(dsa, "invalid bound definition"); } scan_token(dsa); if (dsa->token == T_PLUS || dsa->token == T_MINUS) { /* parse signed fixed value */ s = (dsa->token == T_PLUS ? +1.0 : -1.0); scan_token(dsa); if (dsa->token == T_NUMBER) { set_lower_bound(dsa, j, s * dsa->value); set_upper_bound(dsa, j, s * dsa->value); scan_token(dsa); } else fatal(dsa, "missing fixed value"); } else if (dsa->token == T_NUMBER) { /* parse unsigned fixed value */ set_lower_bound(dsa, j, dsa->value); set_upper_bound(dsa, j, dsa->value); scan_token(dsa); } else fatal(dsa, "missing fixed value"); } else if (the_same(dsa->image, "free")) { /* parse the keyword 'free' */ if (lb_flag) { /* the context '... <= x free ...' is invalid */ fatal(dsa, "invalid bound definition"); } set_lower_bound(dsa, j, -DBL_MAX); set_upper_bound(dsa, j, +DBL_MAX); scan_token(dsa); } else if (!lb_flag) { /* neither lower nor upper bounds are specified */ fatal(dsa, "invalid bound definition"); } goto loop; done: return; }
static void parse_constraints(struct dsa *dsa) { int i, len, type; double s; /* parse the keyword 'subject to' */ xassert(dsa->token == T_SUBJECT_TO); scan_token(dsa); loop: /* create new row (constraint) */ i = lpx_add_rows(dsa->lp, 1); /* parse row name */ if (dsa->token == T_NAME && dsa->c == ':') { /* row name is followed by a colon */ if (lpx_find_row(dsa->lp, dsa->image) != 0) fatal(dsa, "constraint `%s' multiply defined", dsa->image); lpx_set_row_name(dsa->lp, i, dsa->image); scan_token(dsa); xassert(dsa->token == T_COLON); scan_token(dsa); } else { /* row name is not specified; use default */ char name[50]; sprintf(name, "r.%d", dsa->count); lpx_set_row_name(dsa->lp, i, name); } /* parse linear form */ len = parse_linear_form(dsa); lpx_set_mat_row(dsa->lp, i, len, dsa->ind, dsa->val); /* parse constraint sense */ if (dsa->token == T_LE) type = LPX_UP, scan_token(dsa); else if (dsa->token == T_GE) type = LPX_LO, scan_token(dsa); else if (dsa->token == T_EQ) type = LPX_FX, scan_token(dsa); else fatal(dsa, "missing constraint sense"); /* parse right-hand side */ if (dsa->token == T_PLUS) s = +1.0, scan_token(dsa); else if (dsa->token == T_MINUS) s = -1.0, scan_token(dsa); else s = +1.0; if (dsa->token != T_NUMBER) fatal(dsa, "missing right-hand side"); switch (type) { case LPX_LO: lpx_set_row_bnds(dsa->lp, i, LPX_LO, s * dsa->value, 0.0); break; case LPX_UP: lpx_set_row_bnds(dsa->lp, i, LPX_UP, 0.0, s * dsa->value); break; case LPX_FX: lpx_set_row_bnds(dsa->lp, i, LPX_FX, s * dsa->value, 0.0); break; } /* the rest of the current line must be empty */ if (!(dsa->c == '\n' || dsa->c == EOF)) fatal(dsa, "invalid symbol(s) beyond right-hand side"); scan_token(dsa); /* if the next token is a sign, numeric constant, or a symbolic name, here is another constraint */ if (dsa->token == T_PLUS || dsa->token == T_MINUS || dsa->token == T_NUMBER || dsa->token == T_NAME) goto loop; return; }
void main () { err_t err; reg_t regs [REG_MAX]; context_t * context; char_t token [TOKEN_LEN_MAX]; byte_t len; context = (context_t *) regs; // Startup banner print_char ('P'); print_char ('T'); print_char ('1'); print_char ('.'); print_char ('1'); print_char (13); // carriage return print_char (10); // new line while (1) { err = scan_token (token, &len); if (err == E_OK) { if (!len) break; switch (token [0]) { case 'R': if (len != 2) { err = E_LEN; break; } err = reg_read (regs, token [1]); print_val (context->val); break; case 'W': if (len != 2) { err = E_LEN; break; } err = reg_write (regs, token [1]); break; case 'X': if (len != 2) { err = E_LEN; break; } err = sub_exec (regs, token [1]); break; default: if (len > 4) { err = E_LEN; break; } err = val_write (regs, token, len); } } print_stat (err); } }
static void do_pragma_op( void) /* * Execute the _Pragma() operator contained in an expanded macro. * Note: _Pragma() operator is also implemented as a special macro. Therefore * it is always searched as a macro. * There might be more than one _Pragma() in a expanded macro and those may be * surrounded by other token sequences. * Since all the macros have been expanded completely, any name identical to * macro should not be re-expanded. * However, a macro in the string argument of _Pragma() may be expanded by * do_pragma() after de_stringize(), if EXPAND_PRAGMA == TRUE. */ { FILEINFO * file; DEFBUF * defp; int prev = output < out_ptr; /* There is a previous sequence */ int token_type; char * cp1, * cp2; int c; file = unget_string( out_ptr, NULL); while (c = get_ch(), file == infile) { if (char_type[ c] & HSP) { *out_ptr++ = c; continue; } if (scan_token( c, (cp1 = out_ptr, &cp1), out_wend) == NAM && (defp = is_macro( &cp1)) != NULL && defp->nargs == DEF_PRAGMA) { /* _Pragma() operator */ if (prev) { putout( output); /* Putout the previous sequence */ cp1 = stpcpy( output, "pragma "); /* From top of buffer */ } /* is_macro() already read over possible spaces after _Pragma */ *cp1++ = get_ch(); /* '(' */ while (char_type[ c = get_ch()] & HSP) *cp1++ = c; if (((token_type = scan_token( c, (cp2 = cp1, &cp1), out_wend)) != STR && token_type != WSTR)) { /* Not a string literal */ put_seq( output, cp1); return; } workp = de_stringize( cp2, work_buf); while (char_type[ c = get_ch()] & HSP) *cp1++ = c; if (c != ')') { /* More than a string literal */ unget_ch(); put_seq( output, cp1); return; } strcpy( workp, "\n"); /* Terminate with <newline> */ unget_string( work_buf, NULL); do_pragma(); /* Do the #pragma "line" */ infile->bptr += strlen( infile->bptr); /* Clear sequence */ cp1 = out_ptr = output; /* From the top of buffer */ prev = FALSE; } else { /* Not pragma sequence */ out_ptr = cp1; prev = TRUE; } } unget_ch(); if (prev) putout( output); }
static void mcpp_main( void) /* * Main process for mcpp -- copies tokens from the current input stream * (main file or included file) to the output file. */ { int c; /* Current character */ char * wp; /* Temporary pointer */ DEFBUF * defp; /* Macro definition */ int line_top; /* Is in the line top, possibly spaces */ LINE_COL line_col; /* Location of macro call in source */ keep_comments = option_flags.c && !no_output; keep_spaces = option_flags.k; /* Will be turned off if !compiling */ line_col.col = line_col.line = 0L; /* * This loop is started "from the top" at the beginning of each line. * 'wrong_line' is set TRUE in many places if it is necessary to write * a #line record. (But we don't write them when expanding macros.) * * 'newlines' variable counts the number of blank lines that have been * skipped over. These are then either output via #line records or * by outputting explicit blank lines. * 'newlines' will be cleared on end of an included file by get_ch(). */ while (1) { /* For the whole input */ newlines = 0; /* Count empty lines */ while (1) { /* For each line, ... */ out_ptr = output; /* Top of the line buf */ c = get_ch(); if (src_col) break; /* There is a residual tokens on the line */ while (char_type[ c] & HSP) { /* ' ' or '\t' */ if (c != COM_SEP) *out_ptr++ = c; /* Retain line top white spaces */ /* Else skip 0-length comment */ c = get_ch(); } if (c == '#') { /* Is 1st non-space '#' */ directive(); /* Do a #directive */ } else if (mcpp_mode == STD && option_flags.dig && c == '%') { /* In POST_STD digraphs are already converted */ if (get_ch() == ':') { /* '%:' i.e. '#' */ directive(); /* Do a #directive */ } else { unget_ch(); if (! compiling) { skip_nl(); newlines++; } else { break; } } } else if (c == CHAR_EOF) { /* End of input */ break; } else if (! compiling) { /* #ifdef false? */ skip_nl(); /* Skip to newline */ newlines++; /* Count it, too. */ } else if (in_asm && ! no_output) { /* In #asm block */ put_asm(); /* Put out as it is */ } else if (c == '\n') { /* Blank line */ if (keep_comments) mcpp_fputc( '\n', OUT); /* May flush comments */ else newlines++; /* Wait for a token */ } else { break; /* Actual token */ } } if (c == CHAR_EOF) /* Exit process at */ break; /* end of input */ /* * If the loop didn't terminate because of end of file, we * know there is a token to compile. First, clean up after * absorbing newlines. newlines has the number we skipped. */ if (no_output) { wrong_line = FALSE; } else { if (wrong_line || newlines > 10) { sharp( NULL, 0); /* Output # line number */ if (keep_spaces && src_col) { while (src_col--) /* Adjust columns */ mcpp_fputc( ' ', OUT); src_col = 0; } } else { /* If just a few, stuff */ while (newlines-- > 0) /* them out ourselves */ mcpp_fputc('\n', OUT); } } /* * Process each token on this line. */ line_top = TRUE; while (c != '\n' && c != CHAR_EOF) { /* For the whole line */ /* * has_pragma is set to TRUE so as to execute _Pragma() operator * when the psuedo macro _Pragma() is found. */ int has_pragma; if ((mcpp_debug & MACRO_CALL) && ! in_directive) { line_col.line = src_line; /* Location in source */ line_col.col = infile->bptr - infile->buffer - 1; } if (scan_token( c, (wp = out_ptr, &wp), out_wend) == NAM && (defp = is_macro( &wp)) != NULL) { /* A macro */ wp = expand_macro( defp, out_ptr, out_wend, line_col , & has_pragma); /* Expand it completely */ if (line_top) { /* The first token is a macro */ char * tp = out_ptr; while (char_type[ *tp & UCHARMAX] & HSP) tp++; /* Remove excessive spaces */ memmove( out_ptr, tp, strlen( tp) + 1); wp -= (tp - out_ptr); } if (has_pragma) { /* Found _Pramga() */ do_pragma_op(); /* Do _Pragma() operator*/ out_ptr = output; /* Do the rest of line */ wrong_line = TRUE; /* Line-num out of sync */ } else { out_ptr = wp; } if (keep_spaces && wrong_line && infile && *(infile->bptr) != '\n' && *(infile->bptr) != EOS) { src_col = infile->bptr - infile->buffer; /* Remember the current colums */ break; /* Do sharp() now */ } } else { /* Not a macro call */ out_ptr = wp; /* Advance the place */ if (wrong_line) /* is_macro() swallowed */ break; /* the newline */ } while (char_type[ c = get_ch()] & HSP) { /* Horizontal space */ if (c != COM_SEP) /* Skip 0-length comment*/ *out_ptr++ = c; } line_top = FALSE; /* Read over some token */ } /* Loop for line */ putout( output); /* Output the line */ } /* Continue until EOF */ }
TSP *tsp_read_data(char *fname) { struct dsa _dsa, *dsa = &_dsa; TSP *tsp = NULL; dsa->fname = fname; xprintf("tsp_read_data: reading TSP data from `%s'...\n", dsa->fname); dsa->fp = fopen(dsa->fname, "r"); if (dsa->fp == NULL) { xprintf("tsp_read_data: unable to open `%s' - %s\n", dsa->fname, strerror(errno)); goto fail; } tsp = xmalloc(sizeof(TSP)); tsp->name = NULL; tsp->type = TSP_UNDEF; tsp->comment = NULL; tsp->dimension = 0; tsp->edge_weight_type = TSP_UNDEF; tsp->edge_weight_format = TSP_UNDEF; tsp->display_data_type = TSP_UNDEF; tsp->node_x_coord = NULL; tsp->node_y_coord = NULL; tsp->dply_x_coord = NULL; tsp->dply_y_coord = NULL; tsp->tour = NULL; tsp->edge_weight = NULL; dsa->seqn = 1; if (get_char(dsa)) goto fail; loop: if (scan_keyword(dsa)) goto fail; if (strcmp(dsa->token, "NAME") == 0) { if (tsp->name != NULL) { xprintf("%s:%d: NAME entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_token(dsa, 0)) goto fail; if (strlen(dsa->token) == 0) { xprintf("%s:%d: NAME entry incomplete\n", dsa->fname, dsa->seqn); goto fail; } tsp->name = xmalloc(strlen(dsa->token) + 1); strcpy(tsp->name, dsa->token); xprintf("tsp_read_data: NAME: %s\n", tsp->name); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "TYPE") == 0) { if (tsp->type != TSP_UNDEF) { xprintf("%s:%d: TYPE entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_keyword(dsa)) goto fail; if (strcmp(dsa->token, "TSP") == 0) tsp->type = TSP_TSP; else if (strcmp(dsa->token, "ATSP") == 0) tsp->type = TSP_ATSP; else if (strcmp(dsa->token, "TOUR") == 0) tsp->type = TSP_TOUR; else { xprintf("%s:%d: data type `%s' not recognized\n", dsa->fname, dsa->seqn, dsa->token); goto fail; } xprintf("tsp_read_data: TYPE: %s\n", dsa->token); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "COMMENT") == 0) { if (tsp->comment != NULL) { xprintf("%s:%d: COMMENT entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_comment(dsa)) goto fail; tsp->comment = xmalloc(strlen(dsa->token) + 1); strcpy(tsp->comment, dsa->token); xprintf("tsp_read_data: COMMENT: %s\n", tsp->comment); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "DIMENSION") == 0) { if (tsp->dimension != 0) { xprintf("%s:%d: DIMENSION entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_integer(dsa, 0, &tsp->dimension)) goto fail; if (tsp->dimension < 1) { xprintf("%s:%d: invalid dimension\n", dsa->fname, dsa->seqn); goto fail; } xprintf("tsp_read_data: DIMENSION: %d\n", tsp->dimension); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "EDGE_WEIGHT_TYPE") == 0) { if (tsp->edge_weight_type != TSP_UNDEF) { xprintf("%s:%d: EDGE_WEIGHT_TYPE entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_keyword(dsa)) goto fail; if (strcmp(dsa->token, "GEO") == 0) tsp->edge_weight_type = TSP_GEO; else if (strcmp(dsa->token, "EUC_2D") == 0) tsp->edge_weight_type = TSP_EUC_2D; else if (strcmp(dsa->token, "ATT") == 0) tsp->edge_weight_type = TSP_ATT; else if (strcmp(dsa->token, "EXPLICIT") == 0) tsp->edge_weight_type = TSP_EXPLICIT; else if (strcmp(dsa->token, "CEIL_2D") == 0) tsp->edge_weight_type = TSP_CEIL_2D; else { xprintf("%s:%d: edge weight type `%s' not recognized\n", dsa->fname, dsa->seqn, dsa->token); goto fail; } xprintf("tsp_read_data: EDGE_WEIGHT_TYPE: %s\n", dsa->token); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "EDGE_WEIGHT_FORMAT") == 0) { if (tsp->edge_weight_format != TSP_UNDEF) { xprintf( "%s:%d: EDGE_WEIGHT_FORMAT entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_keyword(dsa)) goto fail; if (strcmp(dsa->token, "UPPER_ROW") == 0) tsp->edge_weight_format = TSP_UPPER_ROW; else if (strcmp(dsa->token, "FULL_MATRIX") == 0) tsp->edge_weight_format = TSP_FULL_MATRIX; else if (strcmp(dsa->token, "FUNCTION") == 0) tsp->edge_weight_format = TSP_FUNCTION; else if (strcmp(dsa->token, "LOWER_DIAG_ROW") == 0) tsp->edge_weight_format = TSP_LOWER_DIAG_ROW; else { xprintf("%s:%d: edge weight format `%s' not recognized\n", dsa->fname, dsa->seqn, dsa->token); goto fail; } xprintf("tsp_read_data: EDGE_WEIGHT_FORMAT: %s\n", dsa->token); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "DISPLAY_DATA_TYPE") == 0) { if (tsp->display_data_type != TSP_UNDEF) { xprintf("%s:%d: DISPLAY_DATA_TYPE entry multiply defined\n", dsa->fname, dsa->seqn); goto fail; } if (check_colon(dsa)) goto fail; if (scan_keyword(dsa)) goto fail; if (strcmp(dsa->token, "COORD_DISPLAY") == 0) tsp->display_data_type = TSP_COORD_DISPLAY; else if (strcmp(dsa->token, "TWOD_DISPLAY") == 0) tsp->display_data_type = TSP_TWOD_DISPLAY; else { xprintf("%s:%d: display data type `%s' not recognized\n", dsa->fname, dsa->seqn, dsa->token); goto fail; } xprintf("tsp_read_data: DISPLAY_DATA_TYPE: %s\n", dsa->token); if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "NODE_COORD_SECTION") == 0) { int n = tsp->dimension, k, node; if (n == 0) { xprintf("%s:%d: DIMENSION entry not specified\n", dsa->fname, dsa->seqn); goto fail; } if (tsp->node_x_coord != NULL) { xprintf("%s:%d: NODE_COORD_SECTION multiply specified\n", dsa->fname, dsa->seqn); goto fail; } if (check_newline(dsa)) goto fail; tsp->node_x_coord = xcalloc(1+n, sizeof(double)); tsp->node_y_coord = xcalloc(1+n, sizeof(double)); for (node = 1; node <= n; node++) tsp->node_x_coord[node] = tsp->node_y_coord[node] = DBL_MAX; for (k = 1; k <= n; k++) { if (scan_integer(dsa, 0, &node)) goto fail; if (!(1 <= node && node <= n)) { xprintf("%s:%d: invalid node number %d\n", dsa->fname, dsa->seqn, node); goto fail; } if (tsp->node_x_coord[node] != DBL_MAX) { xprintf("%s:%d: node number %d multiply specified\n", dsa->fname, dsa->seqn, node); goto fail; } if (scan_number(dsa, 0, &tsp->node_x_coord[node])) goto fail; if (scan_number(dsa, 0, &tsp->node_y_coord[node])) goto fail; if (check_newline(dsa)) goto fail; } } else if (strcmp(dsa->token, "DISPLAY_DATA_SECTION") == 0) { int n = tsp->dimension, k, node; if (n == 0) { xprintf("%s:%d: DIMENSION entry not specified\n", dsa->fname, dsa->seqn); goto fail; } if (tsp->dply_x_coord != NULL) { xprintf("%s:%d: DISPLAY_DATA_SECTION multiply specified\n", dsa->fname, dsa->seqn); goto fail; } if (check_newline(dsa)) goto fail; tsp->dply_x_coord = xcalloc(1+n, sizeof(double)); tsp->dply_y_coord = xcalloc(1+n, sizeof(double)); for (node = 1; node <= n; node++) tsp->dply_x_coord[node] = tsp->dply_y_coord[node] = DBL_MAX; for (k = 1; k <= n; k++) { if (scan_integer(dsa, 0, &node)) goto fail; if (!(1 <= node && node <= n)) { xprintf("%s:%d: invalid node number %d\n", dsa->fname, dsa->seqn, node); goto fail; } if (tsp->dply_x_coord[node] != DBL_MAX) { xprintf("%s:%d: node number %d multiply specified\n", dsa->fname, dsa->seqn, node); goto fail; } if (scan_number(dsa, 0, &tsp->dply_x_coord[node])) goto fail; if (scan_number(dsa, 0, &tsp->dply_y_coord[node])) goto fail; if (check_newline(dsa)) goto fail; } } else if (strcmp(dsa->token, "TOUR_SECTION") == 0) { int n = tsp->dimension, k, node; if (n == 0) { xprintf("%s:%d: DIMENSION entry not specified\n", dsa->fname, dsa->seqn); goto fail; } if (tsp->tour != NULL) { xprintf("%s:%d: TOUR_SECTION multiply specified\n", dsa->fname, dsa->seqn); goto fail; } if (check_newline(dsa)) goto fail; tsp->tour = xcalloc(1+n, sizeof(int)); for (k = 1; k <= n; k++) { if (scan_integer(dsa, 1, &node)) goto fail; if (!(1 <= node && node <= n)) { xprintf("%s:%d: invalid node number %d\n", dsa->fname, dsa->seqn, node); goto fail; } tsp->tour[k] = node; } if (scan_integer(dsa, 1, &node)) goto fail; if (node != -1) { xprintf("%s:%d: extra node(s) detected\n", dsa->fname, dsa->seqn); goto fail; } if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "EDGE_WEIGHT_SECTION") == 0) { int n = tsp->dimension, i, j, temp; if (n == 0) { xprintf("%s:%d: DIMENSION entry not specified\n", dsa->fname, dsa->seqn); goto fail; } if (tsp->edge_weight_format == TSP_UNDEF) { xprintf("%s:%d: EDGE_WEIGHT_FORMAT entry not specified\n", dsa->fname, dsa->seqn); goto fail; } if (tsp->edge_weight != NULL) { xprintf("%s:%d: EDGE_WEIGHT_SECTION multiply specified\n", dsa->fname, dsa->seqn); goto fail; } if (check_newline(dsa)) goto fail; tsp->edge_weight = xcalloc(1+n*n, sizeof(int)); switch (tsp->edge_weight_format) { case TSP_FULL_MATRIX: for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (scan_integer(dsa, 1, &temp)) goto fail; tsp->edge_weight[(i - 1) * n + j] = temp; } } break; case TSP_UPPER_ROW: for (i = 1; i <= n; i++) { tsp->edge_weight[(i - 1) * n + i] = 0; for (j = i + 1; j <= n; j++) { if (scan_integer(dsa, 1, &temp)) goto fail; tsp->edge_weight[(i - 1) * n + j] = temp; tsp->edge_weight[(j - 1) * n + i] = temp; } } break; case TSP_LOWER_DIAG_ROW: for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { if (scan_integer(dsa, 1, &temp)) goto fail; tsp->edge_weight[(i - 1) * n + j] = temp; tsp->edge_weight[(j - 1) * n + i] = temp; } } break; default: goto fail; } if (check_newline(dsa)) goto fail; } else if (strcmp(dsa->token, "EOF") == 0) { if (check_newline(dsa)) goto fail; goto done; } else { xprintf("%s:%d: keyword `%s' not recognized\n", dsa->fname, dsa->seqn, dsa->token); goto fail; } goto loop; done: xprintf("tsp_read_data: %d lines were read\n", dsa->seqn-1); fclose(dsa->fp); return tsp; fail: if (tsp != NULL) { if (tsp->name != NULL) xfree(tsp->name); if (tsp->comment != NULL) xfree(tsp->comment); if (tsp->node_x_coord != NULL) xfree(tsp->node_x_coord); if (tsp->node_y_coord != NULL) xfree(tsp->node_y_coord); if (tsp->dply_x_coord != NULL) xfree(tsp->dply_x_coord); if (tsp->dply_y_coord != NULL) xfree(tsp->dply_y_coord); if (tsp->tour != NULL) xfree(tsp->tour); if (tsp->edge_weight != NULL) xfree(tsp->edge_weight); xfree(tsp); } if (dsa->fp != NULL) fclose(dsa->fp); return NULL; }
static void db_reader(struct cdt_node *np) { size_t local_lnum = np->lnum; unsigned char *ftext = np->text; struct cdt_record *rp = NULL; struct cdt_database *db = NULL; db = malloc(sizeof(struct cdt_database)); db->type = np->name; db->records = list_create(LIST_SINGLE); np->children = list_create(LIST_SINGLE); list_add(np->children,db); while (*ftext) { ftext = skip_white(ftext,&local_lnum); if (*ftext == '@') { unsigned char *tok, save; tok = scan_token(ftext, &ftext, &save); if (':' == *tok) { if (rp) { struct cdt_field *fld; list_add(rp->fields, fld = malloc(sizeof(struct cdt_field))); *ftext = save; tok = ftext; fld->name = (const char *)tok; while (!isspace(*tok)) ++tok; if (*tok) *tok++ = '\0'; while (isspace(*ftext)) ++ftext; fld->text = scan_field(tok,&ftext); if (fld->text && strchr(fld->text, '@')) { /* fprintf(stderr, "found @ in %s\n", fld->text); */ struct cdt_node *sp = cdt_string_node(NULL,fld->text,np->file,local_lnum); cdt_inline(sp,NULL,NULL); fld->inp = sp; } else fld->inp = NULL; } } else if (!strcmp((const char *)tok,"record")) { list_add(db->records,rp = malloc(sizeof(struct cdt_record))); rp->fields = list_create(LIST_SINGLE); *ftext = save; } else if (!strcmp((const char *)tok,"end")) { *ftext = save; tok = ++ftext; while (' ' == *tok || '\t' == *tok) ++tok; if (!strncmp((const char *)tok,"record",6)) { if (rp) rp = NULL; else cdt_warning(np->file,np->lnum,"misplaced '@end record'"); } else if (!strncmp((const char *)tok,np->name,strlen(np->name))) { ftext = tok + strlen(np->name); *ftext = '\0'; } else cdt_warning(np->file,np->lnum,"misplaced @end %s command", tok); } else { *ftext = save; ++ftext; } } else ++ftext; } }
Token get_token() { Token token = scan_token(); while(token.type == COMMENT) token = scan_token(); return token; }