%{
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;

void yyerror(char* message) {
	cerr << message << endl;
	exit(EXIT_FAILURE);
}

extern int yylex();
%}

%union {
       string*   str_attr;
       int       int_attr;
       set<int>* set_attr;
}

%token TYPE RELATION QUESTION ANSWER PP
%token <str_attr> ID
%token <int_attr> NUM
%type  <set_attr> type_def num_list
%%

file : file line
     | 
     ;

line : TYPE ID '=' type_def ';' {
       cout << "Type " << (*$2) << " defined as {";

       set<int>::const_iterator it;
       for (it = $4->begin();  it != $4->end(); ) {
	   cout << *it;
	   if (++it != $4->end())
	      cout << ", ";
       }
       cout << "}" << endl;
       delete $2;
     }
     | RELATION ID '=' rel_def ';'
     | QUESTION ID '=' quest_def ';'
     | ANSWER ID ';'
     ;

type_def : NUM PP NUM {
	$$ = new set<int>();
	for (int i = $1; i <= $3; i++)
	    $$->insert(i);
     }
     | '{' num_list '}' {
       $$ = $2;
     }
     ;

num_list : num_list ',' NUM {
       $$ = $1;
       $$->insert($3);
     }
     | NUM {
       $$ = new set<int>();
       $$->insert($1);
     }
     ;

rel_def :
     ;

quest_def :
     ;

%%

main() {
       yyparse();
}
