コード例 #1
0
ファイル: 172_cal_language.c プロジェクト: chenrushan/uva
int
expression(void)
{
    int value = 0;
    char op = 0, ch = 0;

    skip_space();
    ch = str[idx];
    if (ch == '\0') {
        return 0;
    }

    if (ch == '(') {
        idx += 1; /* read '(' */
        value = expression();
        skip_space();
        idx += 1; /* read ')' */

        skip_space();
        if (is_operator()) {
            op = operator();
            /*printf("%d|%c ", order++, op);*/
            int v = expression();
            return do_num(op, value, v);
        } else {
            return value;
        }

    } else if (ch == '_' || isdigit(ch)) {
        int num = number();
        /*printf("%d|%d ", order++, num);*/
        skip_space();
        if (is_operator()) {
            op = operator();
            /*printf("%d|%c ", order++, op);*/
            value = expression();
            return do_num(op, num, value);
        } else {
            return num;
        }

    } else {
        char var = variable();
        /*printf("%d|%c ", order++, var);*/
        skip_space();
        if (is_operator()) {
            op = operator();
            /*printf("%d|%c ", order++, op);*/
            value = expression();
            return do_var(op, var, value);
        } else {
            return vars[var - 'A'];
        }
    }
}
コード例 #2
0
ファイル: 172_cal_language.c プロジェクト: chenrushan/uva
int
do_var(char op, char var, int num)
{
    int i = var - 'A';
    switch (op) {
    case '=':
        vars[i] = num;
        return num;

    default:
        return do_num(op, vars[i], num);
    }
}
コード例 #3
0
ファイル: parserslow.c プロジェクト: tommie/xppaut
static int make_toks(const char *source, int *my_token) {
  int old_tok = STARTTOK, tok_in = 0;
  int index = 0, nparen = 0, lastindex = 0;

  while (source[index] != '\0') {
    int token = find_tok(&my_symb, source + index);

    lastindex = index;
    if (token != my_symb.len)
      index += my_symb.elems[token].len;

    if ((token == MINUS) &&
        ((old_tok == STARTTOK) || (old_tok == COMMA) || (old_tok == LPAREN)))
      token = NEGATE;
    else if (token == LPAREN)
      ++nparen;
    else if (token == RPAREN)
      --nparen;

    if (token == my_symb.len) {
      char num[40];
      double value;
      /*  WARNING  -- ASSUMES 32 bit int  and 64 bit double  */
      union {
        struct {
          int int1;
          int int2;
        } pieces;
        struct {
          double z;
        } num;
      } encoder;

      if (do_num(source, num, &value, &index)) {
        plintf("illegal expression: %s\n", num);
        show_where(source, index);
        return (1);
      }
      /*    new code        3/95      */
      encoder.num.z = value;
      my_token[tok_in++] = NUMTOK;
      my_token[tok_in++] = encoder.pieces.int1;
      my_token[tok_in++] = encoder.pieces.int2;
      if (check_syntax(old_tok, NUMTOK) == 1) {
        plintf("Illegal syntax \n");
        show_where(source, lastindex);
        return (1);
      }
      old_tok = NUMTOK;
    } else {
      my_token[tok_in++] = token;
      if (check_syntax(old_tok, token) == 1) {
        plintf("Illegal syntax (Ref:%d %d) \n", old_tok, token);
        show_where(source, lastindex);
        tokeninfo(old_tok);
        tokeninfo(token);
        return (1);
      }

      old_tok = token;
    }
  }

  my_token[tok_in++] = ENDTOK;
  if (check_syntax(old_tok, ENDTOK) == 1) {
    plintf("Premature end of expression \n");
    show_where(source, lastindex);
    return (1);
  }
  if (nparen != 0) {
    plintf("parentheses don't match\n");
    return (1);
  }
  return (0);
}