Rule *parse_file(FILE *fp) {
    Rule *rules_head = malloc(sizeof(Rule));
    Rule *curr = rules_head;
    Dependency *prev_dep = NULL;
    Rule *p = NULL;
    Action *prev_action = NULL;
    char line[MAXLINE + 1];
    char *token;
    printf("start1");

    while (fgets(line, MAXLINE, fp) != NULL) {
        printf("start");
        line[strcspn(line, "\n")] = '\0';
        line[strcspn(line, "\r")] = '\0';
        if (is_comment_or_empty(line) == 1) {
            continue;
        }
        if (line[0] == '\t') { // line is a target line
            if (curr != rules_head) {
                Rule *new_rule = malloc(sizeof(Rule));
                curr->next_rule = new_rule;
                curr = new_rule;
                prev_action = NULL;
   }
            token = strtok(line, " "); // first token is the target word
            curr->target = malloc(sizeof(char) * (strlen(token) + 1));
            strcpy(curr->target, token);
            token = strtok(line, " "); // second token is the colon
            // Loop through dependencies
            while ((token = strtok(line, " ")) != NULL) {
                Dependency *dep = malloc(sizeof(Dependency));
                if ((p = rule_in_list(rules_head, token)) != NULL) {
                    dep->rule = p;
                }
                else {
                    Rule *new_rule = malloc(sizeof(Rule));
                    new_rule->target = malloc(sizeof(char) * (strlen(token) + 1));
                    strcpy(new_rule->target, token);
                    new_rule->next_rule = NULL;
                    curr->next_rule = new_rule;
                    dep->rule = new_rule;
                }
                dep->next_dep = NULL;
                if (prev_dep != NULL) {
                    prev_dep->next_dep = dep;
                }
                prev_dep = dep;
 }
            printf("target done");
            prev_dep = NULL;
        }
        else { // line is an action line
            Action *action = malloc(sizeof(Action));
            char l[strlen(line + 1)];
            strcpy(l, line);
            int size = 0;
            while ((token = strtok(l, " ")) != NULL) {
                size++;
            }
            action->args = malloc(sizeof(char *) * (size + 1));
            action->args[size] = NULL;
            int i = 0;
            while ((token = strtok(line, " ")) != NULL) {
                action->args[i] = malloc(sizeof(char) * (strlen(token) + 1));
                strcpy(action->args[i], token);
                i++;
            }
            if (prev_action != NULL) {
                prev_action->next_act = action;
            }
            else {
                curr->actions = action;
                  }
            action->next_act = NULL;
            prev_action = action;
            printf("action done");
        }
    }
    printf("parse done!\n");
    return rules_head;
}