work
Rule *parse_file(FILE *fp) {
Rule *rules = makeRule();
Rule *cur = rules;
Action *cur_act = makeAction();
Dependency *cur_dep = makeDependency();
char *line = malloc(sizeof(char*) * MAXLINE);
while (fgets(line, sizeof(MAXLINE), fp)) {
if (is_comment_or_empty(line)) {
continue;
}
char** args = parseLine(line);
if (line[0] == '\t') {
Action *cur_act = cur->actions;
while (cur_act->next_act != NULL) {
cur_act = cur_act->next_act;
}
cur_act->next_act = malloc(sizeof(Action));
cur_act->next_act = makeAction();
cur_act->next_act->args = args;
}
else {
Rule *cur = rules;
while (cur->next_rule != NULL) {
cur = cur->next_rule;
}
for (int i = 0; i < sizeof(args); i++) {
Rule *searchRule = rules;
cur_dep = cur->dependencies;
while (searchRule->next_rule != NULL) {
if (strcmp(searchRule->target, args[i]) == 0) {
if (i == 0){
cur = searchRule;
break;
}
else if (i >= 2) {
while (cur_dep->next_dep != NULL) {
cur_dep = cur_dep->next_dep;
}
cur_dep->rule = searchRule;
cur_dep->next_dep = makeDependency();
cur_dep = cur_dep->next_dep;
break;
}
}
searchRule = searchRule->next_rule;
}
if (searchRule->next_rule == NULL) {
if (i == 0){
Rule *oldcur = cur;
cur->target = args[0];
cur->dependencies = makeDependency();
cur_dep = cur->dependencies;
cur->actions = makeAction();
for (int j = 2; j < sizeof(args); j++) {
cur_dep->rule = makeRule();
cur_dep->rule->target = args[j];
cur_dep->next_dep = makeDependency();
cur_dep = cur_dep->next_dep;
cur->next_rule = cur_dep->rule;
cur = cur->next_rule;
}
cur->next_rule = makeRule();
cur = oldcur;
break;
}
else if (i >= 2) {
Rule *newcur = rules;
while (newcur->next_rule != NULL) {
newcur = newcur->next_rule;
}
Rule *new_rule = makeRule();
new_rule->target = args[i];
newcur->next_rule = new_rule;
cur_dep->rule = new_rule;
cur_dep->next_dep = makeDependency();
cur_dep = cur_dep->next_dep;
break;
}
}

}
}
}
return rules;
}