/*
 File: mpost_output.cpp
 Date and Time: Fri Jan 30 18:55:16 2015 
*/
#include "mpost_output.h"
using namespace NS_yacco2_T_enum;// enumerate
using namespace NS_yacco2_err_symbols;// error symbols
using namespace NS_yacco2_k_symbols;// lrk 
using namespace NS_yacco2_terminals;// terminals
using namespace NS_yacco2_characters;// rc 
using namespace yacco2;// yacco2 library
using namespace NS_mpost_output;// grammar's ns
// first set terminals
fsm_rules_reuse_table_type::fsm_rules_reuse_table_type(){
 no_rules_entries_ = 21;
 per_rule_s_table_[0] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[1] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[2] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[3] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[4] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[5] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[6] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[7] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[8] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[9] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[10] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[11] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[12] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[13] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[14] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[15] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[16] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[17] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[18] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[19] =  new Per_rule_s_reuse_table();
 per_rule_s_table_[20] =  new Per_rule_s_reuse_table();
}
  Cmpost_output::
  Cmpost_output()
    :yacco2::CAbs_fsm
      ("mpost_output.lex"
      ,"1.0"
      ,"29 Nov. 2005"
      ,false
      ,"Output grammar rules railroad diagrams for mpost that cweb program uses."
      ,"Fri Jan 30 18:55:16 2015 "
      ,S1_Cmpost_output){
    
    MPOST_CWEB_LOAD_XLATE_CHRS(this);
  
  }
 
Cmpost_output::~Cmpost_output(){

  for(int x = 0;x < 21;++x){
   ///delete fsm_rules_reuse_table.per_rule_s_table_[x];
  }
} 

  bool Cmpost_output::failed(){
      return false;
  }
  void Cmpost_output::op(){
no_subrules_per_rule_.push_back(0);
time_t theTime= time(0);
char*cTime= ctime(&theTime);
gened_date_time_+= string(cTime);
int n= gened_date_time_.find('\n');
gened_date_time_[n]= ' ';

mp_dimension_+= " abcdefghijklmnopqrstuvwxyz";
w_fig_no_= 0;
rule_def_= 0;
subrule_def_= 0;
rule_no_= 0;
subrule_no_= 0;
elem_no_= 0;
no_of_rules_= 0;
no_of_subrules_= 0;
mp_filename_+= grammar_filename_prefix_.c_str();
mp_filename_+= ".mp";
omp_file_.open(mp_filename_.c_str(),ios_base::out|ios::trunc);
if(!omp_file_){
CAbs_lr1_sym*sym= new Err_bad_filename(mp_filename_.c_str());
      sym->set_who_created(__FILE__,__LINE__);
parser__->add_token_to_error_queue(*sym);
parser__->set_stop_parse(true);
return;
}
w_filename_+= grammar_filename_prefix_.c_str();
w_filename_+= ".w";
ow_file_.open(w_filename_.c_str(),ios_base::out|ios::trunc);
if(!ow_file_){
CAbs_lr1_sym*sym= new Err_bad_filename(w_filename_.c_str());
      sym->set_who_created(__FILE__,__LINE__);
parser__->add_token_to_error_queue(*sym);
parser__->set_stop_parse(true);
return;
}
w_index_filename_+= grammar_filename_prefix_.c_str();
w_index_filename_+= "_idx.w";
ow_index_file_.open(w_index_filename_.c_str(),ios_base::out|ios::trunc);
if(!ow_index_file_){
CAbs_lr1_sym*sym= new Err_bad_filename(w_index_filename_.c_str());
      sym->set_who_created(__FILE__,__LINE__);
parser__->add_token_to_error_queue(*sym);
parser__->set_stop_parse(true);
return;
}


KCHARP t_file = "T.w";
ow_t_file_.open(t_file,ios_base::out|ios::trunc);
if(!ow_t_file_){
CAbs_lr1_sym*sym= new Err_bad_filename(t_file);
      sym->set_who_created(__FILE__,__LINE__);
parser__->add_token_to_error_queue(*sym);
parser__->set_stop_parse(true);
return;
}
KCHARP err_file = "Err.w";
ow_err_file_.open(err_file,ios_base::out|ios::trunc);
if(!ow_err_file_){
CAbs_lr1_sym*sym= new Err_bad_filename(err_file);
      sym->set_who_created(__FILE__,__LINE__);
parser__->add_token_to_error_queue(*sym);
parser__->set_stop_parse(true);
return;
}

KCHARP lrk_file = "Lrk.w";
ow_lrk_file_.open(lrk_file,ios_base::out|ios::trunc);
if(!ow_lrk_file_){
CAbs_lr1_sym*sym= new Err_bad_filename(lrk_file);
      sym->set_who_created(__FILE__,__LINE__);
parser__->add_token_to_error_queue(*sym);
parser__->set_stop_parse(true);
return;
}

	MPOST_CWEB_EMIT_PREFIX_CODE(this);
  
}
int Cmpost_output::rhs_to_rules_mapping_[33] = {
 -1
 ,0 // subrule 1 for rule 1
 ,1 // subrule 2 for rule 2
 ,2 // subrule 3 for rule 3
 ,2 // subrule 4 for rule 3
 ,3 // subrule 5 for rule 4
 ,3 // subrule 6 for rule 4
 ,4 // subrule 7 for rule 5
 ,5 // subrule 8 for rule 6
 ,6 // subrule 9 for rule 7
 ,6 // subrule 10 for rule 7
 ,7 // subrule 11 for rule 8
 ,8 // subrule 12 for rule 9
 ,9 // subrule 13 for rule 10
 ,10 // subrule 14 for rule 11
 ,11 // subrule 15 for rule 12
 ,12 // subrule 16 for rule 13
 ,13 // subrule 17 for rule 14
 ,13 // subrule 18 for rule 14
 ,14 // subrule 19 for rule 15
 ,15 // subrule 20 for rule 16
 ,16 // subrule 21 for rule 17
 ,16 // subrule 22 for rule 17
 ,17 // subrule 23 for rule 18
 ,18 // subrule 24 for rule 19
 ,19 // subrule 25 for rule 20
 ,19 // subrule 26 for rule 20
 ,20 // subrule 27 for rule 21
 ,20 // subrule 28 for rule 21
 ,20 // subrule 29 for rule 21
 ,20 // subrule 30 for rule 21
 ,20 // subrule 31 for rule 21
 ,20 // subrule 32 for rule 21
}; 
Rmpost_output::Rmpost_output(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rmpost_output",0,Cmpost_output::R_Rmpost_output_,P,false,false){
}

void Rmpost_output::sr1(){
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
	KCHARP mp_init_vars =	"init_variables;";
 	fsm->omp_file_ << mp_init_vars << endl;
	KCHARP mp_figure =	
		"beginfig(%i);\n"
		"  drw_rule(%i);\n"
		"endfig;";
	KCHARP sr_figure =	
		"beginfig(%i);\n"
		"  drw_rule_a_rhs(%i,%i);\n"
		"endfig;";

	int rule_no = 1;
	int fig_no(0);
	for(;rule_no <= fsm->no_of_rules_;++rule_no){
		++fig_no;
 	    sprintf(fsm->big_buf_,mp_figure,fig_no,rule_no);
 	    fsm->omp_file_ << fsm->big_buf_ << endl;
 	    int no_srs = fsm->no_subrules_per_rule_[rule_no];
 	    int sr_no(1);
 	    for(;sr_no <= no_srs;++sr_no){
 	      ++fig_no;
 	      sprintf(fsm->big_buf_,sr_figure,fig_no,rule_no,sr_no);
 	      fsm->omp_file_ << fsm->big_buf_ << endl;
 	    }
	}

	KCHARP mp_end =	"end;";
 	fsm->omp_file_ << mp_end << endl;
 	fsm->omp_file_.close();
	KCHARP w_fsc_file =	
    "@** First Set Language for \\Olinker.\\fbreak\n"
 	"\\openin 1 = \"%s.fsc\"\n" // macro file open
	"\\ifeof 1 true\n" // test the result
	 " \\closein 1\n"
	"\\else\n"   
	"   \\closein 1\n"
        "   \\listing{\"%s.fsc\"}"
	"\\fi";
 	sprintf(fsm->big_buf_,w_fsc_file,fsm->fq_filename_noext_.c_str()
		,fsm->fq_filename_noext_.c_str());
    fsm->ow_file_ << fsm->big_buf_ << endl;

    KCHARP w_lr1_states =	
        "@** Lr1 State Network.\\fbreak\n"
        "\\statecolumnspace %s";
    sprintf(fsm->big_buf_,w_lr1_states," ");
    fsm->ow_file_ << fsm->big_buf_ << endl;
    KCHARP w_stateno = // stateno, vectored into symbol, conflict	
        "\\stateno{%i} {%s} {%s}\n"
        "\\halign{\n"
        "\\span\\statetemplate\n"
        "\\statetitle";
  char vectored_into[Max_cweb_item_size];
  char rule_name[Max_cweb_item_size];
  char subrule_sym[Max_cweb_item_size];
  char cur_sym[Max_cweb_item_size];
  char nxtsym_1[Max_cweb_item_size];
  char nxtsym_2[Max_cweb_item_size];
  char state_type[Max_cweb_item_size];
  char la_set_no[Max_cweb_item_size];
  string w_possible_ar_code;

  KCHARP la_set_no_template = "%i";
  KCHARP state_type_template = "%s";
  KCHARP three_symbols_string_template = 
        "%s %s %s";        // 3 symbols: current and 2 la
  KCHARP w_stateno_subrule = 
        "{%s}&\n"        // closure or transitive
        "{%s}&\n"       // rule
        "{%i\\hss}&\n"   // rule no
        "{%i\\hss}&\n"   // subrule no
        "{%i\\hss}&\n"   // element pos
        "{%s}&\n"       // current symbol
        "{%i\\hss}&\n"   // born state no
        "{%i\\hss}&\n"   // goto state no
        "{%i\\hss}&\n"   // reduced state no
        "{%s\\hss}\\cr";   // la set if reduced
  int fnd_reduced_stateno(0);
  
  STATES_ITER_type si = LR1_STATES.begin();
  STATES_ITER_type sie = LR1_STATES.end();
  for(;si!=sie;++si){// walk the states
    vectored_into[0] = (char)0;
    w_possible_ar_code.clear();
    state_type[0] = (char)0;
    state* cur_state = *si;
    
    if(cur_state->state_no_ != 1){// state 1 vector into is empty
      XLATE_SYMBOLS_FOR_cweave
            (cur_state->entry_symbol_literal(),vectored_into);
    }
        switch (cur_state->state_type_){
           case 0:{//shift only
             sprintf(state_type,state_type_template,"\\Shiftonly");
             break;
           }
           case 1:{
             sprintf(state_type,state_type_template,"\\Reduceonly");
             break;
           }
           case 2:{
             sprintf(state_type,state_type_template,"\\ShiftReduce");
             break;
           }
           case 3:{
             sprintf(state_type,state_type_template,"\\MultipleReduces");
             break;
           }
           case 4:{
             sprintf(state_type,state_type_template,"\\ShiftandMultipleReduces");
             break;
           }
        }
    w_possible_ar_code.append(vectored_into);
    if (cur_state->vectored_into_by_elem_ == LR1_PARALLEL_OPERATOR){
     // add arbitration code? to |vectored_into| string
     if (cur_state->arbitrator_name_ != 0){
      w_possible_ar_code += "\\ arbitration-code: \\ |";
      w_possible_ar_code += cur_state->arbitrator_name_->c_str();
      w_possible_ar_code += "|";
     }else{
      w_possible_ar_code += "\\ arbitration-code: \\ \\epsilon";
     }
    }
    sprintf(fsm->big_buf_
            ,w_stateno
            ,cur_state->state_no_
            ,w_possible_ar_code.c_str()//|vectored_into|
            ,state_type);
    fsm->ow_file_ << fsm->big_buf_ << endl;
	S_VECTORS_ITER_type svi = cur_state->state_s_vector_.begin();
	S_VECTORS_ITER_type svie = cur_state->state_s_vector_.end();
        string rhs_syms_str;
	for(;svi!=svie;++svi){
	  S_VECTOR_ELEMS_ITER_type seli = svi->second.begin();
	  S_VECTOR_ELEMS_ITER_type selie = svi->second.end();
	  for(;seli!=selie;++seli){
	        rhs_syms_str.clear();     
		state_element* se = *seli;
                rule_def* rd = se->subrule_def_->its_rule_def();
                T_subrule_def* srd = se->subrule_def_;
                rule_name[0] = (char)0;
                subrule_sym[0] = (char)0;
                cur_sym[0] = (char)0;
                la_set_no[0] = (char)0;

                XLATE_SYMBOLS_FOR_cweave(rd->rule_name()->c_str(),rule_name);
                int elem_pos(0);
                if(se->previous_state_ == 0) elem_pos = 1;
                elem_pos = MPOST_CWEB_xlated_symbol(se->sr_element_,cur_sym);
                if(se->goto_state_==0){// reducing subrule
                 sprintf(la_set_no,la_set_no_template,se->common_la_set_idx_+1);
		}else{// no la set
 		}
   
  if (se->reduced_state_ != 0) {
    fnd_reduced_stateno = se->reduced_state_->state_no_;
    }
  else{ // find from merged into
    fnd_reduced_stateno=-1;
    if (se->next_state_element_->reduced_state_ != 0){
     fnd_reduced_stateno = se->next_state_element_->reduced_state_->state_no_;   
    }
  }
		fsm->MPOST_CWEB_crt_rhs_sym_str(se,&rhs_syms_str);                
                sprintf(fsm->big_buf_
                    ,w_stateno_subrule
                    ,(elem_pos == 1)?"c":"t"
                    ,rule_name
                    ,rd->rule_no()
                    ,srd->subrule_no_of_rule()
                    ,elem_pos
                    ,rhs_syms_str.c_str()
                    ,se->closure_state_->state_no_
                    ,(se->goto_state_ !=0)?se->goto_state_->state_no_:0
                    ,fnd_reduced_stateno
                    ,la_set_no
                    );
                fsm->ow_file_ << fsm->big_buf_ << endl;
	   }
	}
    fsm->ow_file_ << "}" << endl;
  }
	KCHARP w_end =	"@** Index.";
 	fsm->ow_file_ << w_end << endl;
 	fsm->ow_file_.close();
  
}

Rgrammar_phrase::Rgrammar_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rgrammar_phrase",0,Cmpost_output::R_Rgrammar_phrase_,P,false,false){
}

Rks_epi::Rks_epi(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rks_epi",0,Cmpost_output::R_Rks_epi_,P,false,false){
}

Rks::Rks(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rks",0,Cmpost_output::R_Rks_,P,false,false){
}

Rk::Rk(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rk",0,Cmpost_output::R_Rk_,P,false,false){
}

void Rk::sr1(){
  struct SF{
   T_cweb_marker* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
	WRT_CWEB_MARKER(&fsm->ow_file_,sf->p1__->ast());   
  
}

Rfsm_phrase::Rfsm_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rfsm_phrase",0,Cmpost_output::R_Rfsm_phrase_,P,false,false){
}

void Rfsm_phrase::sr1(){
  struct SF{
   T_fsm_phrase* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    fsm->MPOST_CWEB_wrt_fsm(fsm,sf->p1__);
  
}

Rparallel_phrase::Rparallel_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rparallel_phrase",0,Cmpost_output::R_Rparallel_phrase_,P,false,false){
}

RT_enum_phrase::RT_enum_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("RT_enum_phrase",0,Cmpost_output::R_RT_enum_phrase_,P,false,false){
}

Rerr_phrase::Rerr_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rerr_phrase",0,Cmpost_output::R_Rerr_phrase_,P,false,false){
}

void Rerr_phrase::sr1(){
  struct SF{
   T_error_symbols_phrase* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    fsm->MPOST_CWEB_wrt_Err(fsm,sf->p1__);
  
}

Rrc_phrase::Rrc_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rrc_phrase",0,Cmpost_output::R_Rrc_phrase_,P,false,false){
}

Rlr1_k_phrase::Rlr1_k_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rlr1_k_phrase",0,Cmpost_output::R_Rlr1_k_phrase_,P,false,false){
}

void Rlr1_k_phrase::sr1(){
  struct SF{
   T_lr1_k_phrase* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    fsm->MPOST_CWEB_wrt_Lrk(fsm,sf->p1__);
  
}

Rterms_phrase::Rterms_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rterms_phrase",0,Cmpost_output::R_Rterms_phrase_,P,false,false){
}

void Rterms_phrase::sr1(){
  struct SF{
   T_terminals_phrase* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    fsm->MPOST_CWEB_wrt_T(fsm,sf->p1__);
  
}

Rrule_phrase::Rrule_phrase(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rrule_phrase",0,Cmpost_output::R_Rrule_phrase_,P,false,false){
}

void Rrule_phrase::sr1(){
  struct SF{
   T_rules_phrase* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
	fsm->no_of_rules_ = sf->p1__->rules_alphabet()->size(); 
 	KCHARP mp_no_of_rules = "no_of_rules := %i;";
 	sprintf(fsm->big_buf_,mp_no_of_rules,fsm->no_of_rules_);
 	fsm->omp_file_ << fsm->big_buf_ << endl;
  
}

Rrules::Rrules(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rrules",0,Cmpost_output::R_Rrules_,P,false,false){
}

Rrule::Rrule(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rrule",0,Cmpost_output::R_Rrule_,P,false,false){
}

Rrule_def::Rrule_def(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rrule_def",0,Cmpost_output::R_Rrule_def_,P,false,false){
}

void Rrule_def::sr1(){
  struct SF{
   rule_def* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    fsm->rule_def_ = sf->p1__;
    fsm->rules_for_fs_prt_.push_back(fsm->rule_def_);
 	KCHARP rname = fsm->rule_def_->rule_name()->c_str();
    ++fsm->w_fig_no_;
    ++fsm->rule_no_;
    fsm->subrule_no_ = 0;

	KCHARP mp_rule_names = 	
		"rule_names[%i].literal := \"%s\";"
		"rule_names[%i].vname := \"%s\";";
 	  
 	sprintf(fsm->big_buf_,mp_rule_names,fsm->rule_no_,rname,fsm->rule_no_,rname);
 	fsm->omp_file_ << fsm->big_buf_ << endl;

 	T_subrules_phrase* sr_ph = fsm->rule_def_->subrules();
 	fsm->no_of_subrules_ = sr_ph->no_subrules();
	KCHARP mp_rule_s_no_rhs = "rule_s_no_rhs[%i] := %i;";
 	sprintf(fsm->big_buf_,mp_rule_s_no_rhs
		,fsm->rule_no_,fsm->no_of_subrules_);
 	fsm->omp_file_ << fsm->big_buf_ << endl;
    fsm->no_subrules_per_rule_.push_back(fsm->no_of_subrules_);//[rule \#]
    
    KCHARP rule_cweb = 
		"@*2 |@!%s| rule.\\fbreak\n";
 	sprintf(fsm->big_buf_,rule_cweb,rname);
 	fsm->ow_file_ << fsm->big_buf_;
    if(fsm->rule_def_->cweb_marker() != 0){
		WRT_CWEB_MARKER(&fsm->ow_file_,fsm->rule_def_->cweb_marker());   
    }
    KCHARP rule_cweb_diagram = 
		"\\fbreak\n"
		"\\convertMPtoPDF{%s.%i}{1}{1}\n"; 
 	sprintf(fsm->big_buf_,rule_cweb_diagram
 			,fsm->grammar_filename_prefix_.c_str()
 			,fsm->w_fig_no_);
 	fsm->ow_file_ << fsm->big_buf_;
    std::vector<T_subrule_def*>* srules = sr_ph->subrules();
    std::vector<T_subrule_def*>::iterator i =  srules->begin();
    std::vector<T_subrule_def*>::iterator ie =  srules->end();
    for(;i!=ie;++i){
      T_subrule_def* srd = *i;
      fsm->MPOST_CWEB_gen_sr_elem_xrefs(fsm,srd->subrule_s_tree());
    }
    fsm->MPOST_CWEB_wrt_rule_s_lhs_sdc(fsm,fsm->rule_def_);
   
  
}

Rsubrules::Rsubrules(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rsubrules",0,Cmpost_output::R_Rsubrules_,P,false,false){
}

Rsubrule::Rsubrule(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rsubrule",0,Cmpost_output::R_Rsubrule_,P,false,false){
}

Rsubrule_def::Rsubrule_def(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Rsubrule_def",0,Cmpost_output::R_Rsubrule_def_,P,false,false){
}

void Rsubrule_def::sr1(){
  struct SF{
   T_subrule_def* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    ++fsm->w_fig_no_;
    ++fsm->subrule_no_;
    fsm->elem_no_ = 0;
    fsm->subrule_def_ = sf->p1__;
    int no_elements = fsm->subrule_def_->no_of_elems()-1;
    if(no_elements == 0) no_elements = 1;// epsilon subrule
	KCHARP mp_rule_s_subrule_no_elems = "rule_s_subrule_no_elems[%i][%i]:= %i;";
    sprintf(fsm->big_buf_,mp_rule_s_subrule_no_elems
 			,fsm->rule_no_,fsm->subrule_no_,no_elements);
 	fsm->omp_file_ << fsm->big_buf_ << endl;

	if(fsm->MPOST_CWEB_should_subrule_be_printed(fsm,fsm->subrule_def_) 
		== false) return;

 	KCHARP rname = fsm->rule_def_->rule_name()->c_str();

    KCHARP subrule_cweb = 
		"@*3 |%s|'s subrule %i.\\fbreak\n";
	if((fsm->rule_def_->rule_lhs() != 0) 
	    ||
	   (fsm->no_of_subrules_ > 1)){
 		  sprintf(fsm->big_buf_,subrule_cweb,rname,fsm->subrule_no_);
 		  fsm->ow_file_ << fsm->big_buf_;
 	}
    if(fsm->subrule_def_->cweb_marker() != 0){
		WRT_CWEB_MARKER(&fsm->ow_file_,fsm->subrule_def_->cweb_marker());   
		cout << "subrule's cweb marker for rule: " << rname << endl;
    }else{
		cout << "no subrule's cweb marker for rule: " << rname << endl;
		}

    KCHARP subrule_cweb_diagram = 
		"\\fbreak"
		"\\convertMPtoPDF{%s.%i}{1}{1}\n"; 
	if((fsm->rule_def_->rule_lhs() != 0) 
	    ||
	   (fsm->no_of_subrules_ > 1)){
 	  sprintf(fsm->big_buf_,subrule_cweb_diagram
 			,fsm->grammar_filename_prefix_.c_str()
 			,fsm->w_fig_no_);
 	  fsm->ow_file_ << fsm->big_buf_;
 	}

    fsm->MPOST_CWEB_woutput_sr_sdcode(fsm,fsm->subrule_def_);
  
}

Relements::Relements(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Relements",0,Cmpost_output::R_Relements_,P,false,false){
}

Relement::Relement(yacco2::Parser* P)
 :CAbs_lr1_sym
  ("Relement",0,Cmpost_output::R_Relement_,P,false,false){
}

void Relement::sr1(){
  struct SF{
   refered_T* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
   CAbs_lr1_sym* p2__;
   State* s2__;
   bool abort2__;
   Rule_s_reuse_entry* rule_s_reuse_entry2__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(2);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    ++fsm->elem_no_;
    std::string mp_obj_type("Circle_solid");
    std::string mp_elem_name(sf->p1__->t_in_stbl()->t_def()->t_name()->c_str());
    string::size_type x = mp_elem_name.find("\\\"");
    if(x != string::npos){
      mp_elem_name.clear();
      mp_elem_name+= "char(34)";
    }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
  
}

void Relement::sr2(){
  struct SF{
   refered_rule* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(1);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    ++fsm->elem_no_;
    std::string mp_obj_type("Box_solid");
    std::string mp_elem_name(sf->p1__->Rule_in_stbl()->
	r_def()->rule_name()->c_str());
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
  
}

void Relement::sr3(){
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    ++fsm->elem_no_;
    if(fsm->elem_no_ == 1){// epsilon
       std::string mp_obj_type("Circle_solid");
       std::string mp_elem_name(" ");
       fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
     }
  
}

void Relement::sr4(){
  struct SF{
   refered_T* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
   refered_T* p2__;
   State* s2__;
   bool abort2__;
   Rule_s_reuse_entry* rule_s_reuse_entry2__;
   T_called_thread_eosubrule* p3__;
   State* s3__;
   bool abort3__;
   Rule_s_reuse_entry* rule_s_reuse_entry3__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(3);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
	KCHARP re_align_nos = "rule_s_subrule_no_elems[%i][%i]:= %i;";
    sprintf(fsm->big_buf_,re_align_nos
 			,fsm->rule_no_,fsm->subrule_no_,3);
 	fsm->omp_file_ << fsm->big_buf_ << "% re-align cnt" << endl;
    ++fsm->elem_no_;
    std::string mp_obj_type("Circle_dotted");
    std::string mp_elem_name;
      if(sf->p1__->its_t_def()->enum_id() == T_Enum::T_LR1_parallel_operator_){    
        mp_elem_name += "|||";
      }else{
        mp_elem_name += "|t|";
      }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
    
    ++fsm->elem_no_;
    mp_obj_type.clear();
    mp_obj_type += "Circle_dotted";
    mp_elem_name.clear();
    mp_elem_name += sf->p2__->t_in_stbl()->t_def()->t_name()->c_str();
    string::size_type x = mp_elem_name.find("\\\"");
    if(x != string::npos){
      mp_elem_name.erase();
      mp_elem_name+= "char(34)";
    }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);

    ++fsm->elem_no_;
    mp_elem_name.clear();
    mp_obj_type.clear();
    mp_obj_type += "Box_dotted"; 
	mp_elem_name += sf->p3__->ns()->identifier()->c_str();
	mp_elem_name += "::";
	mp_elem_name += sf->p3__->called_thread_name()->identifier()->c_str();
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
  
}

void Relement::sr5(){
  struct SF{
   refered_T* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
   refered_T* p2__;
   State* s2__;
   bool abort2__;
   Rule_s_reuse_entry* rule_s_reuse_entry2__;
   T_null_call_thread_eosubrule* p3__;
   State* s3__;
   bool abort3__;
   Rule_s_reuse_entry* rule_s_reuse_entry3__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(3);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
	KCHARP re_align_nos = "rule_s_subrule_no_elems[%i][%i]:= %i;";
    sprintf(fsm->big_buf_,re_align_nos,fsm->rule_no_,fsm->subrule_no_,3);
 	fsm->omp_file_ << fsm->big_buf_ << "% re-align cnt" << endl;

    ++fsm->elem_no_;
    std::string mp_obj_type("Circle_dotted");
    std::string mp_elem_name;
      if(sf->p1__->its_t_def()->enum_id() == T_Enum::T_LR1_parallel_operator_){    
        mp_elem_name += "|||";
    }else{
        mp_elem_name += "|t|";
    }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
    mp_elem_name.clear();
    
    ++fsm->elem_no_;
    mp_elem_name += sf->p2__->t_in_stbl()->t_def()->t_name()->c_str();
    string::size_type x = mp_elem_name.find("\\\"");
    if(x != string::npos){
      mp_elem_name.clear();
      mp_elem_name+= "char(34)";
    }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);

    ++fsm->elem_no_;
    mp_elem_name.clear();
    mp_obj_type.clear();
    mp_obj_type += "Box_dotted"; 
	mp_elem_name += "NULL";
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
  
}

void Relement::sr6(){
  struct SF{
   refered_T* p1__;
   State* s1__;
   bool abort1__;
   Rule_s_reuse_entry* rule_s_reuse_entry1__;
   refered_T* p2__;
   State* s2__;
   bool abort2__;
   Rule_s_reuse_entry* rule_s_reuse_entry2__;
   CAbs_lr1_sym* p3__;
   State* s3__;
   bool abort3__;
   Rule_s_reuse_entry* rule_s_reuse_entry3__;
  };
  SF* sf = (SF*)rule_info__.parser__->parse_stack__.sf_by_top(3);
  
    Cmpost_output* fsm = (Cmpost_output*)rule_info__.parser__->fsm_tbl__;
    ++fsm->elem_no_;
    std::string mp_obj_type("Circle_solid");
    std::string mp_elem_name(sf->p1__->t_in_stbl()->t_def()->t_name()->c_str());
    string::size_type x = mp_elem_name.find("\\\"");
    if(x != string::npos){
      mp_elem_name.erase();
      mp_elem_name+= "char(34)";
    }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);

    ++fsm->elem_no_;
    mp_obj_type.clear();
    mp_obj_type += "Circle_solid";
    mp_elem_name.clear();
    mp_elem_name += sf->p2__->t_in_stbl()->t_def()->t_name()->c_str();
    x = mp_elem_name.find("\\\"");
    if(x != string::npos){
      mp_elem_name.erase();
      mp_elem_name+= "char(34)";
    }
    fsm->MPOST_CWEB_wrt_mp_rhs_elem(fsm,mp_elem_name,mp_obj_type);
  
}

