Read from the open file fp, and create the linked data structure
   that represents the Makefile contained in the file.
   See the top of pmake.h for the specification of Makefile contents.
Rule *parse_file(FILE *fp) {
    // Implement this function and remove the stubbed return statement below.
    Rule *head_rule, *curr_rule;
    char line[MAXLINE];

    while (fgets(line, MAXLINE, fp) != NULL) {
        // Check if a line of text is a comment or an empty line.
        if (is_comment_or_empty(line)) {
            continue;
        }

        // Parse the line.
        char *target = strtok(line, " \t\n");
        Dependency *dependencies = NULL;
        Dependency *curr_dep = NULL;
        Action *actions = NULL;
        Action *curr_act = NULL;
        char *token = strtok(NULL, " \t\n");
        
        // while ((token = strtok(NULL, " \t\n")) != NULL) {
        if (strcmp(token, ":") == 0) {
            // Parse the dependency
            while ((token = strtok(NULL, " \t\n")) != NULL) {
                Dependency *dep = malloc(sizeof(Dependency));

                if (dependencies == NULL) {
                    dependencies = dep;
                    curr_dep = dep;
                } else {
                    curr_dep->next_dep = dep;
                    curr_dep = dep;
                }
            }
        } else {
            // Parse the action
            Action *act = malloc(sizeof(Action));
            char **args = malloc(sizeof(char **));
            strcpy(args[0], target);
            int i = 1;

            while (token != NULL) {
                strcpy(args[i],token);
                token = strtok(NULL, " \t\n");
                i++;
            }

            args[i] = NULL;
            act->args = args;
            
            if (actions == NULL) {
                actions = act;
                curr_act = act;
            } else {
                curr_act->next_act = act;
                curr_act = act;
            }
        }
        
        // Create a new Rule node.
        Rule *rule = malloc(sizeof(Rule));
        strcpy(rule->target, target);
        rule->dependencies = dependencies;
        rule->actions = actions;
        if (head_rule == NULL) {
            head_rule = rule;
            curr_rule = rule;
        } else {
            curr_rule->next_rule = rule;
            curr_rule = rule;
        }
        
    }
    return head_rule;
}