#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int ch;			// caracter adelantado

int getch(void) 
{
	do {
		ch= fgetc(stdin);
	} while (isspace(ch) && (ch!=EOF));
	fprintf(stderr, "estoy leyendo %c\n", ch);
	return ch;
}

void Error(const char mensaje[])
{
	fprintf(stderr, "ERROR - %s\n", mensaje);
	exit(EXIT_FAILURE);
}

void expr(void);

void factor(void)
{
	if (ch=='x')
		getch();
	else
	if (ch=='y')
		getch();
	else
	if (ch=='(') {
		getch();
		expr();
		if (ch==')')
			getch();
		else
			Error("falta ')'");
	}
	else
		Error("falta <factor>");
}

void term(void)
{
	factor();
	while(1) {
		if (ch=='*') {
			getch();
			factor();
		}
		else
		if (ch=='/') {
			getch();
			factor();
		}
		else
			return;
	}
}

void expr(void)
{
	term();
	while(1) {
		if (ch=='+') {
			getch();
			term();
		}
		else
		if (ch=='-') {
			getch();
			term();
		}
		else
			return;
	}
}

void linea(void)
{
	expr();
	if (ch!=';')
		Error("falta ';'");
}

int main(int argc, char *args[])
{
	getch();
	linea();
	printf("la expresion es correcta\n");
	return EXIT_SUCCESS;
}




