int main(int argc, char* argv[]){ //test command line arguments if(argc != 2){ printf("insufficent command line arguments"); exit(0); } //open the file FILE* input = fopen(argv[1],"r"); if(input == NULL){ printf("File not found\n"); exit(0); } char equation[MAX_EQU_LEN] = {0}; // initilize the array while(fscanf(input,"%s",equation) != EOF){ printf("For equation %s; %s : %s\n",equation,paren_match(equation)?"Parensmatch":"Parens do not match",paren_and_brackets_match(equation)?"Bonus Brackets match":"Bonus Brackets do not match"); equation[0] = '\0'; //reset the array } close(input); //close the file return 0; }
/* Takes a pointer to an opening parenthetical. * Returns a pointer AFTER the matching delimiter, or NULL if not found. */ const char *paren_match (const char *open, step_t step) { const char *s = (*step)(open, 1); while (*s) { switch (*s) { case '#': s = comment_match(s, step); if (s) { break; } else return NULL; case '(': case '{': case '[': s = paren_match(s, step); if (s) { break; } else return NULL; case ']': if (*open == '[') return (*step)(s,1); else return NULL; case '}': if (*open == '{') return (*step)(s,1); else return NULL; case ')': if (*open == '(') { return (*step)(s,1); } else return NULL; case '"': case '\'': case '%': case '`': s = quoted_match(s, step); if (s) { break; } else return NULL; default: s = (*step)(s, 1); } } return NULL; }
/* Go forward one symbol, balanced subexpression, or other char. * Return NULL if imbalanced. */ const char *forward_balanced(const char *str, step_t step) { switch (*str) { case 0: return NULL; case '(': case '{': case '[': return paren_match(str, step); case ')': case '}': case ']': return NULL; case '"': case '\'': case '%': case '`': return quoted_match(str, step); case '#': return comment_match(str, step); default: return forward_word_or_char(str, step); } }