// main.cpp  -- Driver for lifo of characters.
//              Verify that a formula consisting of appropriately 
//              nested strings formed with the parentheses 
//              {,},[,],(,)  is well-formed.

#include <iostream>
#include "lifo.h"
using namespace std;

// Return true iff c1 and c2 are matching parentheses
int matching(char c1, char c2){
	if (((c1 == '(') && (c2 == ')')) ||
		((c1 == '[') && (c2 == ']')) ||
		((c1 == '{') && (c2 == '}')))
		return 1;
	else
		return 0;
}

enum {MAXLINE=256};

void main(void)
{
	char line[MAXLINE];
	int lcv;
	lifo stack;
	char c1;

	createLifo(stack);
	cout << "Enter a well formed string formed with {,},[,],(,): ";
	cin.getline(line, MAXLINE);
	lcv = 0;
	while((c1 = line[lcv++]) != '\0'){
		if (isspace(c1))
			;
		else if ((c1 == '{') || 
			(c1 == '[') ||
			(c1 == '('))
			push(stack, c1);
		else if ((c1 == '}') ||
			(c1 == ']') ||
			(c1 == ')')) {
			if (isempty(stack)) {
				cout << "Too many right parentheses" << endl;
				return;
			} else {
				if (!matching(pop(stack), c1)){
					cout << "The string has non-matching parentheses" << endl;
					return;
				}
			}
		}else{
			cout << "The string contains an illegal character" << endl;
			return;
		}
	}
	if (isempty(stack))
		cout << "The string is well formed" << endl;
	else
		cout << "Too many left parentheses" << endl;
}
