%{
#include <string.h>
#include <stdio.h>
struct base
{
	int entero;
	double real;
	char tipo[32];
};
#define YYSTYPE struct base
#undef yyerror
void yyerror(char *s);
%}

%error-verbose
%token CAR
%token <entero> INT
%token <real> REAL


%%
lista	: 
	|
        lista e ';'	{
			printf("\nTIPO resultado: %s\n", $2.tipo);
			if (!strcmp($2.tipo, "INT"))
				printf("%i\n",$2.entero);
			else
				printf("%f\n",$2.real);
			}
	| 
	lista error ';' {	
				yyerrok;
			  }
	;
e	:	e '+' t	
			{
			if (!strcmp($1.tipo,$3.tipo)) 
				{				
				 if (!strcmp($1.tipo, "INT"))
					{
					strcpy($$.tipo, "INT");
					$$.entero = $1.entero + $3.entero;			
					printf("%d + ", $3.entero);
					}
				 else
					{
					strcpy($$.tipo, "REAL");
		 		 	$$.real = $1.real + $3.real;
					printf("%f + ", $3.real);
					}

				}
			else
				{
				strcpy($$.tipo, "REAL");
				if (!strcmp($1.tipo, "INT"))
					{
					printf("int_to_real %f + ", $3.real);
					$$.real = $1.entero + $3.real;
					}
				else
					{
					printf("%i int_to_real + ", $3.entero);
					$$.real = $1.real + $3.entero;
					}
				}
			}
	| 	t	{
			strcpy($$.tipo, $1.tipo);
			if (!strcmp($1.tipo, "INT"))
			{
				printf("%d ", $1.entero);
				$$.entero =$1.entero;
			}
			else
			{
				printf("%f ", $1.real);
				$$.real = $1.real;			
			}
			}
	;
t	: 	REAL		{
			strcpy($$.tipo,"REAL");
			$$.real = $1;}
	| 	INT		{
			strcpy($$.tipo,"INT");
			$$.entero = $1;}
	|	CAR		{
				strcpy($$.tipo,"CAR");				
				}	
	;
	
%%

#include "Ej3lb.c"


int main ()
{

	yyparse();
}
void yyerror(char *s)
{
   printf("linea %i: %s\n",yylineno,s);
}

