extern int rmldb_load_db(char* programdb_file)
{
	char line[RML_DEBUG_MAX_STRING];  /* declare a char array */
	struct rml_var_db *rml_var_db_node;
	struct rml_type_db *rml_type_db_node;
	struct rml_con_db *rml_con_db_node;
	struct rml_relation_db *rml_relation_db_node;

	fprintf(stderr, "loading program database from: %s\n", programdb_file);

	aarmldbin = fopen(programdb_file, "r"); 
	/* open a text file for reading */

	if(aarmldbin==NULL) 
	{
		fprintf(stderr, "Error: can't open file %s.\n", programdb_file);
		/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
		rml_exit(2);
	}
	else 
	{
		if (!aarmldbparse())
		  fprintf(stderr, "parsing of program database for file %s failed!\n", programdb_file); 
		fclose(aarmldbin);
		return 0;
	}
}


%{

#include "rml.h"
#include <string.h>
#include <stdlib.h>

extern int aalex(void);
#define FALSE 0
#define TRUE  1

%}


/* The different semantic values that can be returned within the AST. */
%union
{
  rmldb_command_type* rmldb_parsed_command;
}

/* uncomment this shit on linux and comment it on solaris damn incompatibilities */
/* %name-prefix="aa" */

%token T_BREAKPOINT T_DELETE_BREAKPOINT T_CLEAR_BREAKPOINTS
%token T_STEP T_STEP_OVER T_RUN T_SUSPEND	
%token T_STACK, T_VARIABLE_VALUE, T_LAZY_VARIABLE_VALUE 
%token T_QUIT	
%token <char *>T_ID

%type <rmldb_parsed_command> command

%start command
%%

command:  
T_BREAKPOINT T_ID T_EOL
{
#ifdef RML_DEBUG
  /* set On the filter on this relation */
  rmldb_add_relation_filter(aalval.id);
  rmldb_socket_outln(rmldb_communicator.reply_sock, "breakpoint set");
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DELETE_BREAKPOINT T_OFF  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  /* set Off the filter on this relation */
  rmldb_del_relation_filter(aalval.id);
  rmldb_socket_outln(rmldb_communicator.reply_sock, "breakpoint cleared");  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SET T_STEP T_ON T_EOL
{
#ifdef RML_DEBUG
  rmldb_execution_type = RMLDB_STEP;
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SET T_STEP T_OFF T_EOL
{
#ifdef RML_DEBUG
  rmldb_execution_type = RMLDB_RUN;
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_STEP T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_BREAK_PARSE;
  rmldb_execution_type = RMLDB_STEP;
    rmldb_socket_outln(rmldb_communicator.reply_sock, "performing one step");
#endif /* RML_DEBUG */
  YYACCEPT;
}
/*
| T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_BREAK_PARSE;
  rmldb_execution_type = RMLDB_STEP;
#endif 
  YYACCEPT;
}
*/
| T_NEXT T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_BREAK_PARSE;
  rmldb_execution_type = RMLDB_NEXT;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_RUN T_EOL
{
#ifdef RML_DEBUG
  rmldb_execution_type = RMLDB_RUN;
  rmldb_last_command = RMLDB_BREAK_PARSE;
  rmldb_show = RMLDB_HIDE;
  rmldb_socket_outln(rmldb_communicator.reply_sock, "running");
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_BACKTRACE T_EOL
{
#ifdef RML_DEBUG
  rmldb_backtrace_print(NULL);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_FILTERED_BACKTRACE  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_backtrace_print(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;   
}
| T_GRAPH_BACKTRACE T_EOL
{
#ifdef RML_DEBUG
  rmldb_backtrace_send(NULL);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_GRAPH_FILTERED_BACKTRACE  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_backtrace_send(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;   
}
| T_SET T_MAXBT  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_max_backtrace(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_MAXBT  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_max_backtrace(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_CALLCHAIN T_EOL
{
#ifdef RML_DEBUG
  rmldb_callchain_print(NULL);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_FILTERED_CALLCHAIN  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_callchain_print(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;   
}
| T_GRAPH_CALLCHAIN T_EOL
{
#ifdef RML_DEBUG
  rmldb_callchain_send(NULL);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_GRAPH_FILTERED_CALLCHAIN  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_callchain_send(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;   
}
| T_SET T_MAXCALLCHAIN  T_ID T_EOL
{
#ifdef RML_DEBUG
   
  rmldb_set_max_callchain(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_MAXCALLCHAIN  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_max_callchain(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_HELP T_EOL
{
#ifdef RML_DEBUG
  rmldb_show_help(); 
  rmldb_last_command = RMLDB_REPEAT_PARSE;
    rmldb_socket_outln(rmldb_communicator.reply_sock, "printing help");
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_QUIT T_EOL
{
#ifdef RML_DEBUG
  printf("end the program\n");
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_socket_outln(rmldb_communicator.reply_sock, "exiting");
#endif /* RML_DEBUG */
  rml_exit(2);
}
| T_SET T_DEPTH  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_depth(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DEPTH  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_depth(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SET T_MAXSTR  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_maxstr(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_MAXSTR  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_maxstr(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SETTINGS T_EOL
{
#ifdef RML_DEBUG
  rmldb_print_settings();
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SHOW T_EOL
{
#ifdef RML_DEBUG
  rmldb_show_breakpoints();
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_socket_outln(rmldb_communicator.reply_sock, "showing breakpoints");
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_CLEAR T_EOL
{
#ifdef RML_DEBUG
  rmldb_clear_breakpoints();
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_socket_outln(rmldb_communicator.reply_sock, "breakpoints cleared");
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_STATUS T_EOL
{
#ifdef RML_DEBUG
  rmldb_show_status();
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_OUTPUT  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_set_output(aalval.id);
  rmldb_last_command = RMLDB_REPEAT_PARSE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_PRINT  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_print_variable(aalval.id);
  rmldb_socket_outln(rmldb_communicator.reply_sock, "variable Value");  
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_PRINT_TYPE  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_print_type_info_id(aalval.id, stdout);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SIZEOF  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_print_sizeof_variable(aalval.id);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_LIVEVARS T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_print_vars();
  rmldb_socket_outln(rmldb_communicator.reply_sock, "v1");
  rmldb_socket_outln(rmldb_communicator.reply_sock, "v2");
  rmldb_socket_outln(rmldb_communicator.reply_sock, "v3");    
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SET T_LIVEVARS T_ON T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_set_print_vars(RMLDB_LIVEVARS_ON);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_LIVEVARS T_ON T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_set_print_vars(RMLDB_LIVEVARS_ON);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_SET T_LIVEVARS T_OFF T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_set_print_vars(RMLDB_LIVEVARS_OFF);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_LIVEVARS T_OFF T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_set_print_vars(RMLDB_LIVEVARS_OFF);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DISPLAY T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_show_displayvars();
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DISPLAY  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_display_variable(aalval.id);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_UNDISPLAY T_EOL
{
#ifdef RML_DEBUG
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_clear_displayvars();
#endif /* RML_DEBUG */
  YYACCEPT;	
}
| T_UNDISPLAY  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_undisplay_variable(aalval.id);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_FAST T_EOL
{
#ifdef RML_DEBUG
  printf("Entering in fast debug mode, no backtrace generation, no live variables.\n");
  printf("Only verifying breakpoints, if they exist. \n");
  printf("Hit CTRL+C to enter again in Step execution mode when you want.\n");
  rmldb_last_command = RMLDB_BREAK_PARSE;
  rmldb_execution_type = RMLDB_FAST;
  rmldb_show = RMLDB_HIDE;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_GRAPH  T_ID T_EOL
{
#ifdef RML_DEBUG
  
  rmldb_last_command = RMLDB_REPEAT_PARSE;
  rmldb_view_variable(aalval.id);
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DEBUGPARSER T_ON T_EOL
{
#ifdef RML_DEBUG
  aadebug = 1;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DEBUGPARSER T_OFF T_EOL
{
#ifdef RML_DEBUG
  aadebug = 0;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_RDBDEBUGPARSER T_ON T_EOL
{
#ifdef RML_DEBUG
  aarmldbdebug = 1;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_RDBDEBUGPARSER T_OFF T_EOL
{
#ifdef RML_DEBUG
  aarmldbdebug = 0;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DEBUGSOCKET T_ON T_EOL
{
#ifdef RML_DEBUG
  rmldb_socket_debug = 1;
#endif /* RML_DEBUG */
  YYACCEPT;
}
| T_DEBUGSOCKET T_OFF T_EOL
{
#ifdef RML_DEBUG
  rmldb_socket_debug = 0;
#endif /* RML_DEBUG */
  YYACCEPT;
}
;

%%

int aaerror(char *s) 
{
  fprintf(stderr, "%s or command not recognized\n", s);
  return -1;
}


#if defined(IGNORE_ME_NOW)	
	/* search for a stack frame within the same file, if it is, then pop everything until then! */
	for (i = 0; i < rmldb_stack_point; i++)
	{
		if (strcmp(rmldb_stack[i]->file, file) == 0 && /* same file */
			strcmp(rmldb_stack[i]->function, function) == 0) /* same function */
		{
			break;
		}
	}
		
	if (i < rmldb_stack_point)
	{
		/* pop stack until nsp = rmldb_stack_point */
		while (i < rmldb_stack_point && rmldb_stack_point != 0)
			rmldb_pop_stack_frame();
	}
				
	/* if (i == rmldb_stack_point) */
#endif /* IGNORED */
