%{
#define YYDEBUG 1

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

extern int yylex();

int promenljive[26];
void postavi_promenljivu(char p, char val)
{
	promenljive[p-'a'] = val == 'T';
}

int vrednost_promenljive(char p)
{
	return promenljive[p-'a'];
}

%}

%left IMPL
%left DISJ
%left KONJ
%nonassoc NEG

%union
{
	string* string_a;
};

%token VAR FORMULA VAL ASSIGN KONJ DISJ IMPL NEG
%token <string_a> ID
%%

program : program linija '\n' prompt
	| prompt
	;

linija : 
		VAR niz_varijabli 
	|	FORMULA niz_formula
	|	VAL ID
	;

prompt : /* eps */	{cout<<">> ";}	
	;

niz_varijabli : niz_varijabli ',' varijabla
	| varijabla
	;
	
varijabla :
	ID ASSIGN ID		
	{
		if ($1->size() != 1 || !((*$1)[0]>='a' && (*$1)[0] <= 'z'))
			yyerror("Var name should be [a-z]");
		if ($3->size() != 1 || ((*$3)[0]!='T' && (*$3)[0] !='F'))
			yyerror("Var value should be T or F");
		postavi_promenljivu((*$1)[0], (*$3)[0]);
		cout<<"Variable "<<(*$1)[0]<<" is set to "<<(*$3)[0]<<endl;
		delete $1;
		delete $3;
	}
	;

niz_formula : niz_formula ',' formula
	| formula
	;

formula : ID ASSIGN izraz
	{}
	;

izraz : 
		ID
	{}
	|	'(' izraz ')'
	{}
	|	izraz KONJ izraz
	{}
	|	izraz DISJ izraz
	{}
	|	izraz IMPL izraz
	{}
	|	NEG izraz
	{}
	;
		

%% 
main()  
{
	yydebug = 1;
	yyparse();
}
