[5f4d9c3] | 1 | #ifndef _GRAMMAR_HH |
---|
| 2 | #define _GRAMMAR_HH |
---|
| 3 | |
---|
| 4 | #include <bitset> |
---|
| 5 | #include <vector> |
---|
| 6 | #include <list> |
---|
| 7 | #include <set> |
---|
| 8 | |
---|
| 9 | #include "const.hh" |
---|
| 10 | #include "thesymbols.hh" |
---|
| 11 | #include "sgraph.hh" |
---|
[e7de6cc] | 12 | #include "boubble.hh" |
---|
[5f4d9c3] | 13 | |
---|
[e7de6cc] | 14 | using namespace std; |
---|
[5f4d9c3] | 15 | |
---|
[e7de6cc] | 16 | //enum PROP { INIT=0, FIN=1 }; |
---|
| 17 | //typedef bitset<16> PropSet; |
---|
| 18 | |
---|
| 19 | const PropSet EmptyPropSet = PropSet(); |
---|
| 20 | |
---|
| 21 | const FlagSet EmptyFlagSet = FlagSet(); |
---|
| 22 | |
---|
| 23 | //==================================================================================================== |
---|
| 24 | // class Link |
---|
| 25 | //==================================================================================================== |
---|
| 26 | |
---|
| 27 | struct Link |
---|
[5f4d9c3] | 28 | { |
---|
[d484a32] | 29 | // Link(Role r, Flag dfplus="NULL", Flag dfminus="NULL") : role(r), dflagplus(dfplus), dflagminus(dfminus) { } |
---|
| 30 | Link(Role r, PropSet ps=EmptyPropSet, Flag hfp="NULL", Flag hfm="NULL", Flag dfp="NULL", Flag dfm="NULL", int lineno=0) |
---|
[e7de6cc] | 31 | : role(r), props(ps), hflagplus(hfp), hflagminus(hfm), dflagplus(dfp), dflagminus(dfm) { } |
---|
| 32 | //Link(Role r) : role(r), dflagplus("NULL") { } |
---|
| 33 | |
---|
[5f4d9c3] | 34 | Role role; |
---|
[e7de6cc] | 35 | Flag hflagplus; |
---|
| 36 | Flag hflagminus; |
---|
| 37 | Flag dflagplus; |
---|
| 38 | Flag dflagminus; |
---|
| 39 | PropSet props; |
---|
[d484a32] | 40 | int lineno; |
---|
[e7de6cc] | 41 | |
---|
| 42 | bool operator<(const Link& l) const |
---|
| 43 | { |
---|
| 44 | if(role < l.role) return true; |
---|
| 45 | if(hflagplus < l.hflagplus) return true; |
---|
| 46 | if(hflagminus < l.hflagminus) return true; |
---|
| 47 | if(dflagplus < l.dflagplus) return true; |
---|
| 48 | if(dflagminus < l.dflagminus) return true; |
---|
| 49 | if(props.to_ulong() < l.props.to_ulong()) return true; |
---|
| 50 | return false; |
---|
| 51 | } |
---|
| 52 | |
---|
[5f4d9c3] | 53 | }; |
---|
| 54 | |
---|
[e7de6cc] | 55 | typedef set<Link> Links; |
---|
| 56 | |
---|
| 57 | //==================================================================================================== |
---|
| 58 | // class Grammar |
---|
| 59 | //==================================================================================================== |
---|
[5f4d9c3] | 60 | |
---|
| 61 | class Grammar |
---|
| 62 | { |
---|
| 63 | |
---|
| 64 | public: |
---|
| 65 | |
---|
[e7de6cc] | 66 | static const int RESIZE_DELTA=16; |
---|
[5f4d9c3] | 67 | |
---|
[e7de6cc] | 68 | Grammar() {} ; |
---|
| 69 | |
---|
| 70 | list<const Link*> connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs); |
---|
| 71 | |
---|
| 72 | bool check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role); |
---|
| 73 | bool check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link); |
---|
| 74 | |
---|
| 75 | bool check_longrel(Cat hcat, Cat dcat, LongRel rel); |
---|
| 76 | bool is_sgl(Role r); |
---|
| 77 | RoleSet is_obl(Cat c); |
---|
| 78 | |
---|
| 79 | RoleSet& constr_include(Role r) { return include[r]; }; |
---|
| 80 | RoleSet& constr_exclude(Role r) { return exclude[r]; }; |
---|
[5f4d9c3] | 81 | |
---|
[e7de6cc] | 82 | FlagSet initial_flags(Cat c) { return set[c]; } |
---|
| 83 | FlagSet pass_flags(Role r) { return pass[r]; } |
---|
| 84 | |
---|
| 85 | list<Boubble*> trigger_boubbles(Cat c, Role r, Dir d); |
---|
| 86 | |
---|
| 87 | bool read(FILE* f); |
---|
| 88 | void write(ostream& os); |
---|
| 89 | void write(FILE* f); |
---|
| 90 | |
---|
| 91 | private: |
---|
[5f4d9c3] | 92 | |
---|
| 93 | RoleSet sgl; |
---|
[e7de6cc] | 94 | vector< RoleSet > obl; //[Cat] |
---|
[5f4d9c3] | 95 | RoleSet left; |
---|
| 96 | RoleSet right; |
---|
[e7de6cc] | 97 | RoleSet init; |
---|
| 98 | RoleSet fin; |
---|
| 99 | FlagSet initf; |
---|
| 100 | FlagSet finf; |
---|
| 101 | |
---|
| 102 | vector< RoleSet > lt; //[Role] |
---|
| 103 | vector< RoleSet > gt; //[Role] |
---|
| 104 | |
---|
| 105 | vector< FlagSet > set; //[Cat] |
---|
| 106 | // vector< FlagSet > rset; //[Role] |
---|
| 107 | vector< FlagSet > pass; //[Role] |
---|
[5f4d9c3] | 108 | |
---|
[d484a32] | 109 | // vector< vector< Roles > > connect; //[Cat][Cat] |
---|
[5f4d9c3] | 110 | |
---|
[e7de6cc] | 111 | vector< vector< Links > > connect1; //[Cat][Cat] |
---|
[5f4d9c3] | 112 | |
---|
[e7de6cc] | 113 | vector< RoleSet > include; //[Role] |
---|
| 114 | vector< RoleSet > exclude; //[Role] |
---|
| 115 | |
---|
| 116 | vector< vector< LongRels > > longrel; //[Cat][Cat] |
---|
| 117 | |
---|
[519eaf5] | 118 | list< Boubble* > boubbles; |
---|
[e7de6cc] | 119 | |
---|
| 120 | vector< vector< list<Boubble*> > > uptrigger;//[Cat][Role] |
---|
| 121 | vector< vector< list<Boubble*> > > dntrigger;//[Cat][Role] |
---|
[5f4d9c3] | 122 | |
---|
| 123 | void add_category(const char* s); |
---|
| 124 | void add_type(const char* s); |
---|
[e7de6cc] | 125 | void add_flag(const char* s) { Flag::add(s); } |
---|
[519eaf5] | 126 | void add_long(const char* l, const char* p) { LongRel::add(l); boubbles.push_back( new Boubble(p,l) ); |
---|
| 127 | boubbles.push_back( (new Boubble(p,l))->reversed() ); } |
---|
[e7de6cc] | 128 | void add_triggers(Cat h, Cat d, LongRel l); |
---|
| 129 | |
---|
| 130 | void set_sgl(Role r) { sgl.set(r); } |
---|
| 131 | void set_obl(Cat c, Role r) { obl[c].set(r); } |
---|
| 132 | void set_left(Role r) { left.set(r); } |
---|
| 133 | void set_right(Role r) { right.set(r); } |
---|
| 134 | void set_init(Role r) { init.set(r); } |
---|
| 135 | void set_fin(Role r) { fin.set(r); } |
---|
| 136 | void set_initf(Flag f) { initf.set(f); } |
---|
| 137 | void set_finf(Flag f) { finf.set(f); } |
---|
| 138 | void set_order(Role r, Role s) { lt[s].set(r); } |
---|
| 139 | |
---|
| 140 | // void set_connect(Cat c, Cat d, Role r) { connect[c][d].insert(r); } |
---|
| 141 | // void set_connect(Cat c, Cat d, Flag f, Role r) { connect1[c][d].insert(Link(r,f)); } |
---|
| 142 | |
---|
[d484a32] | 143 | void set_connect(Cat h, Flag hfp, Flag hfm, Cat d, Flag dfp, Flag dfm, Role r, PropSet ps, int lineno ) { connect1[h][d].insert(Link(r,ps,hfp,hfm,dfp,dfm,lineno)); } |
---|
[e7de6cc] | 144 | |
---|
| 145 | void set_include(Role r, Role s) { include[r].set(s); } |
---|
| 146 | void set_exclude(Role r, Role s) { exclude[r].set(s); } |
---|
| 147 | void set_longrel(Cat c, Cat d, LongRel l){ longrel[c][d].insert(l); } |
---|
| 148 | void set_set(Cat c, Flag f) { set[c].set(f); } |
---|
| 149 | void set_pass(Role r, Flag f) { pass[r].set(f); } |
---|
[5f4d9c3] | 150 | void set_lt(Role r, Role s); |
---|
| 151 | void compute_gt(); |
---|
[e7de6cc] | 152 | void compute_triggers(); |
---|
| 153 | bool contains_boubble(const list<Boubble*> boubble_list, Boubble* bp) const; |
---|
[5f4d9c3] | 154 | |
---|
[e7de6cc] | 155 | }; |
---|
[5f4d9c3] | 156 | |
---|
[e7de6cc] | 157 | //---------------------------------------------------------------------------------------------------- |
---|
[5f4d9c3] | 158 | |
---|
[e7de6cc] | 159 | inline |
---|
| 160 | list<const Link*> Grammar::connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs) // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&) |
---|
| 161 | { |
---|
| 162 | list<const Link*> ret; |
---|
| 163 | for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++) |
---|
| 164 | if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) ) |
---|
| 165 | if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) ) |
---|
| 166 | ret.push_back(&(*l)); |
---|
| 167 | return ret; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | //---------------------------------------------------------------------------------------------------- |
---|
[5f4d9c3] | 171 | |
---|
[e7de6cc] | 172 | inline |
---|
| 173 | bool Grammar::check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link) // dir: 0-left 1-right |
---|
| 174 | { |
---|
| 175 | return |
---|
| 176 | !hprop.forbidden[link.role] && |
---|
[a15e59b] | 177 | ( dir==1 || (!right[link.role] && !link.props[Prop("RIGHT")]) ) && // ZREZYGNOWAÆ Z TABLICY right[<role>] |
---|
| 178 | ( dir==0 || (!left[link.role] && !link.props[Prop("LEFT")]) ) && |
---|
[e7de6cc] | 179 | ( dir!=0 || !hprop.init_attached ) && |
---|
| 180 | ( dir!=1 || !hprop.fin_attached ) |
---|
[5f4d9c3] | 181 | ; |
---|
| 182 | } |
---|
| 183 | |
---|
[e7de6cc] | 184 | //---------------------------------------------------------------------------------------------------- |
---|
| 185 | |
---|
| 186 | inline |
---|
| 187 | bool Grammar::check_longrel(Cat hcat, Cat dcat, LongRel rel) |
---|
| 188 | { |
---|
| 189 | return longrel[hcat][dcat].find(rel) != longrel[hcat][dcat].end(); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | //---------------------------------------------------------------------------------------------------- |
---|
| 193 | |
---|
| 194 | inline bool Grammar::is_sgl(Role r) |
---|
| 195 | { |
---|
| 196 | return sgl[r]; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | //---------------------------------------------------------------------------------------------------- |
---|
| 200 | |
---|
| 201 | inline RoleSet Grammar::is_obl(Cat c) |
---|
| 202 | { |
---|
| 203 | return obl[c]; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | //==================================================================================================== |
---|
[5f4d9c3] | 207 | |
---|
| 208 | #endif |
---|