Example #1
0
int main(int argc, char** argv)
{
    char operator[100];
    char * trimmed_operator = NULL;
    struct Info_list * list_first = NULL;
    /* 给链表的头结点分配内存,并初始化数据为NULL */
    struct Info_list * list_head = (struct Info_list*)malloc(sizeof(struct Info_list));
    if (NULL == list_head) {
        printf("\nError!    Cannot malloc memory\n");
        return 0;
    }
    list_head->next = NULL;
    list_head->node = NULL;
    
    /* 给链表的第一个结点分配内存,并初始化数据为NULL */
    list_first = (struct Info_list*)malloc(sizeof(struct Info_list));
    if (NULL == list_first) {
        printf("\nError!    Cannot malloc memory\n");
        return 0;
    }
    list_first->next = NULL;
    list_first->node = NULL;
    list_head->next = list_first;
    /* 从保存的文件读取数据,并将数据保存到链表当中 */
    read_from_file(list_first);
    
    /* 循环执行用户输入的指令,直到输入退出指令 "q" */
    do{
        printf("\n________________________________________________________________________\n");
        printf("Plese Select Your Operator!    ");
        printf("Type \"help\", for More Information.\n");
        printf("------------------------------------------------------------------------\n");
        printf(">>>> ");
        fgets(operator, 100, stdin);
        trimmed_operator = trimmed(operator);
        select_operator(trimmed_operator, list_head);
        
    } while (strcmp(operator, "q") != 0);
    
    /* 释放链表所申请的内存,防止内存泄露 */
    freeMem(list_head);
    return 0;
}
Example #2
0
Token lex_get_token(void){
  Token ret;
  LexerState state = INITIAL_STATE;
  char token[256];
  int ch;
    
  token[0] = '\0';
  while ((ch = getc(st_source_file)) != EOF){
    switch (state){
    case INITIAL_STATE:
      if(isdigit(ch)){  // 数字?
        add_letter(token, ch);
        state = INT_VALUE_STATE;
      }else if(isalpha(ch) || ch == '_'){ // 英文字? _?
        add_letter(token, ch);
        state = IDENTIFIER_STATE;
      }else if(ch == '\"'){ // 文字列?
        state = STRING_STATE;
      }else if(in_operator(token, ch)){ // 演算子?
        add_letter(token, ch);
        state = OPERATOR_STATE;
      }else if(isspace(ch)){ // 空白?
        if(ch == '\n'){ // 改行?
          st_current_line_number++;
        }
      }else if(ch == '#'){ // コメント?
        state = COMMENT_STATE;
      }else{
        lex_error("bad character", ch); // エラー
      }
      break;
    case INT_VALUE_STATE:
      if (isdigit(ch)) {
        add_letter(token, ch);
      } else {
        ret.kind = INT_VALUE_TOKEN;
        sscanf(token, "%d", &ret.u.int_value);
        ungetc(ch, st_source_file);
        goto LOOP_END;
      }
      break;
    case IDENTIFIER_STATE:
      if(isalpha(ch) || ch == '_' || isdigit(ch)){
        add_letter(token, ch);
      }else{
        ret.u.identifier = token;
        ungetc(ch, st_source_file);
        goto LOOP_END;
      }
      break;
    case STRING_STATE:
      if (ch == '\"') {
        ret.kind = STRING_LITERAL_TOKEN;
        ret.u.string = my_strdup(token);
        goto LOOP_END;
      } else {
        add_letter(token, ch);
      }
      break;
    case OPERATOR_STATE:
      if (in_operator(token, ch)) {
        add_letter(token, ch);
      } else {
        ungetc(ch, st_source_file);
        goto LOOP_END;
      }
      break;
    case COMMENT_STATE:
      if (ch == '\n') {
        state = INITIAL_STATE;
      }
      break;
    default:
      assert(0);
    }
  }
 LOOP_END:
  if (ch == EOF) {
    if (state == INITIAL_STATE
         || state == COMMENT_STATE) {
      ret.kind = END_OF_FILE_TOKEN;
      return ret;
    }
  }
  if (state == IDENTIFIER_STATE) {
    if (!is_keyword(token, &ret.kind)) {
      ret.kind = IDENTIFIER_TOKEN;
      ret.u.string = my_strdup(token);
    }
  } else if (state == OPERATOR_STATE) {
    ret.kind = select_operator(token);
  }
  return ret;
}