- Timestamp:
- 02/21/12 20:02:51 (13 years ago)
- Branches:
- master
- Children:
- b242df2
- Parents:
- 354ba3d
- git-author:
- Tomasz Obrebski <to@…> (02/21/12 20:02:51)
- git-committer:
- Tomasz Obrebski <to@…> (02/21/12 20:02:51)
- Location:
- src/dgp
- Files:
-
- 4 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
src/dgp/Makefile
r5f4d9c3 re7de6cc 1 1 2 include ../../config.mak 3 2 4 3 5 SHELL = /bin/sh … … 6 8 CMDLINE_FILE='"../dgp/cmdline.h"' 7 9 10 8 11 #vpath %.o . 9 12 10 ifeq ($(BUILD_STATIC), yes) 11 LDFLAGS += -static 12 endif 13 CXXFLAGS = -g -static 13 14 14 CXXFLAGS += -O2 15 16 sources = main.cc grammar.cc symbol.cc mgraph.cc sgraph.cc dgp0.cc cmdline.cc \ 15 sources = main.cc grammar.cc symbol.cc mgraph.cc sgraph.cc dgp1.cc cmdline.cc \ 17 16 $(COMMON_PATH)/common.cc global.cc 18 17 … … 23 22 24 23 ${bin}: ${objs} 25 $ (CXX) $(CXXFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) -o $@ ${objs} $(LDFLAGS)24 ${CXX} ${CXXFLAGS} -D _CMDLINE_FILE=$(CMDLINE_FILE) -o $@ ${objs} 26 25 27 26 include $(sources:.cc=.d) 28 27 29 28 %.o: %.cc 30 $ (CXX)-D _CMDLINE_FILE=$(CMDLINE_FILE) -c ${CXXFLAGS} -o $@ $<29 ${CXX} -D _CMDLINE_FILE=$(CMDLINE_FILE) -c ${CXXFLAGS} -o $@ $< 31 30 32 31 %.d: %.cc … … 35 34 rm -f $@.$$$$ 36 35 37 # stare: 38 # cmdline.cc cmdline.h : cmdline.ggo 39 # gengetopt --c-extension=cc -i cmdline.ggo 40 # nowe 36 41 37 cmdline.cc cmdline.h: cmdline.ggo 42 $(GENGETOPT)-i cmdline.ggo --c-extension=cc --conf-parser38 gengetopt -i cmdline.ggo --c-extension=cc --conf-parser 43 39 44 40 cmdline.ggo: cmdline_dgp.ggo ../common/cmdline_common.ggo 45 41 cat cmdline_dgp.ggo ../common/cmdline_common.ggo > cmdline.ggo 46 # endnowe47 42 48 43 44 45 .PHONY: clean 49 46 clean: 50 rm ${bin} ${objs} cmdline.cc cmdline.h51 rm - rf *.d47 rm -f ${bin} ${objs} cmdline.* 48 rm -f *.d 52 49 50 .PHONY: prof 53 51 prof: dgp 54 52 gprof dgp ~/tmp/dgp-pl/gmon.out > dgp.prof 53 55 54 56 55 .PHONY: install … … 58 57 ifdef BIN_DIR 59 58 install -m 0755 dgp $(BIN_DIR) 60 install -m 0755 dgc $(BIN_DIR)61 install -m 0755 canonize $(BIN_DIR)62 install -m 0755 tre $(BIN_DIR)63 59 endif 64 60 … … 67 63 ifdef BIN_DIR 68 64 rm $(BIN_DIR)/dgp 69 rm $(BIN_DIR)/dgc70 rm $(BIN_DIR)/canonize71 rm $(BIN_DIR)/tre72 65 endif -
src/dgp/const.hh
r5f4d9c3 re7de6cc 3 3 4 4 #define MAXTYPES 32 5 #define MAXCATS 4096 5 6 #define MAXFLAGS 64 6 #define MAXNODES 1024 7 #define MAXPROPS 16 8 #define MAXNODES 2048 7 9 #define MAXCONSTRS 32 8 #define MAXLINE 25610 #define MAXLINE 512 9 11 #define MAXFORMLEN 64 10 12 #define MAXDESCRLEN 80 -
src/dgp/dgp0.cc
r5f4d9c3 re7de6cc 6 6 extern SGraph sgraph; 7 7 8 SNode* snodes;9 10 8 extern bool debug; 11 9 … … 16 14 void set_initial_constraints(int node) 17 15 { 18 s nodes[node].prop.forbidden.reset();19 s nodes[node].prop.required=grammar.obl[snodes[node].mnode->cat];16 sgraph[node].prop.forbidden.reset(); 17 sgraph[node].prop.required=grammar.is_obl(mgraph[sgraph[node].mnode].cat); 20 18 } 21 19 … … 23 21 bool changing_constraints(int head, Role role) 24 22 { 25 return grammar. sgl[role] || snodes[head].prop.required[role];23 return grammar.is_sgl(role) || sgraph[head].prop.required[role]; 26 24 } 27 25 28 26 void apply_constraints(int head, Role role) 29 27 { 30 if(grammar. sgl[role]) snodes[head].prop.forbidden.set(role);31 s nodes[head].prop.required.reset(role);28 if(grammar.is_sgl(role)) sgraph[head].prop.forbidden.set(role); 29 sgraph[head].prop.required.reset(role); 32 30 } 33 31 … … 35 33 { 36 34 NodeProp ret=headprop; 37 if(grammar. sgl[role]) ret.forbidden.set(role);35 if(grammar.is_sgl(role)) ret.forbidden.set(role); 38 36 ret.required.reset(role); 39 37 return ret; … … 44 42 NodeProp ret=headprop; 45 43 46 if(grammar. sgl[role]) ret.forbidden.set(role);44 if(grammar.is_sgl(role)) ret.forbidden.set(role); 47 45 ret.required.reset(role); 48 46 return ret; … … 52 50 { 53 51 for(vector<int>::iterator ps=mnode.snodes.begin(); ps!=mnode.snodes.end(); ++ps) 54 if(s nodes[*ps].prop==p && snodes[*ps].LH==newheadLH && snodes[*ps].LV==newheadLV)52 if(sgraph[*ps].prop==p && sgraph[*ps].LH==newheadLH && sgraph[*ps].LV==newheadLV) 55 53 return *ps; 56 54 return -1; … … 59 57 void connect_left(list<int>::iterator h, list<int>::iterator d, Role r) 60 58 { 61 NodeProp &oldheadprop = snodes[*h].prop; 62 NodeProp newheadprop; 63 bitset<MAXNODES> newheadLV; 64 bitset<MAXNODES> newheadLH; 65 bitset<MAXNODES> newheadLD; 66 67 newheadprop=compute_prop_left(oldheadprop,r); 59 NodeProp &oldheadprop = sgraph[*h].prop; 60 NodeProp newheadprop = compute_prop_left(oldheadprop,r); 68 61 69 62 int newheadind; … … 72 65 else 73 66 { 74 newheadLH = snodes[*h].LH;75 newheadLV = snodes[*d].LV;76 newheadLD = snodes[*h].LD;77 78 newheadind = get_node( *(snodes[*h].mnode), newheadprop, newheadLH, newheadLV);67 bitset<MAXNODES> newheadLH = sgraph[*h].LH; 68 bitset<MAXNODES> newheadLV = sgraph[*d].LV; 69 bitset<MAXNODES> newheadLD = sgraph[*h].LD; 70 71 newheadind = get_node(mgraph[sgraph[*h].mnode], newheadprop, newheadLH, newheadLV); 79 72 if( newheadind < 0 ) 80 73 { … … 82 75 list<int>::iterator nextit=h; ++nextit; 83 76 nodelist.insert(nextit,newheadind); 84 s nodes[newheadind].LH=newheadLH;85 s nodes[newheadind].in_LH=true;86 s nodes[newheadind].LV.reset();87 s nodes[newheadind].LD = newheadLD;77 sgraph[newheadind].LH=newheadLH; 78 sgraph[newheadind].LD = newheadLD; 79 sgraph[newheadind].in_LH=true; 80 sgraph[newheadind].LV.reset(); 88 81 89 82 if(debug) sgraph.print_node_debug(stderr," C ",newheadind); 90 83 } 91 84 else 92 s nodes[newheadind].LD |= newheadLD; // TYLKO DLA LD85 sgraph[newheadind].LD |= newheadLD; // TYLKO DLA LD 93 86 } 94 87 95 snodes[newheadind].deps.push_back(Arc(*d,r,*h)); 96 97 if(snodes[*d].saturated()) snodes[newheadind].LV |= snodes[*d].LV; 98 snodes[newheadind].LD.set(*d); 99 if(snodes[*d].saturated()) snodes[newheadind].LD |= snodes[*d].LD; 100 101 if(debug) 102 sgraph.print_arc(stderr,*d,newheadind,r,0), sgraph.print_node_debug(stderr," U ",newheadind); 103 } 104 88 sgraph[newheadind].deps.push_back(Arc(*d,r,*h)); 89 90 if(sgraph[*d].saturated()) sgraph[newheadind].LV |= sgraph[*d].LV; 91 92 sgraph[newheadind].LD.set(*d); 93 if(sgraph[*d].saturated()) sgraph[newheadind].LD |= sgraph[*d].LD; 94 95 if(debug) sgraph.print_arc(stderr,*d,newheadind,r,0), sgraph.print_node_debug(stderr," U ",newheadind); 96 } 105 97 106 98 void connect_right(list<int>::iterator h, list<int>::iterator d, Role r) 107 99 { 108 NodeProp &oldheadprop = snodes[*h].prop; 109 NodeProp newheadprop; 110 bitset<MAXNODES> newheadLV; 111 bitset<MAXNODES> newheadLH; 112 bitset<MAXNODES> newheadLD; 100 NodeProp &oldheadprop = sgraph[*h].prop; 101 NodeProp newheadprop = compute_prop_right(oldheadprop,r); 113 102 int newheadind; 114 103 115 newheadprop = compute_prop_right(oldheadprop,r);116 104 if(oldheadprop==newheadprop) 117 105 newheadind = *h; 118 106 else 119 107 { 120 newheadLH = snodes[*h].LH;121 newheadLV = snodes[*h].LV;122 newheadLD = snodes[*h].LD;123 124 newheadind = get_node( *(snodes[*h].mnode), newheadprop, newheadLH, newheadLV);108 bitset<MAXNODES> newheadLH = sgraph[*h].LH; 109 bitset<MAXNODES> newheadLV = sgraph[*h].LV; 110 bitset<MAXNODES> newheadLD = sgraph[*h].LD; 111 112 newheadind = get_node(mgraph[sgraph[*h].mnode], newheadprop, newheadLH, newheadLV); 125 113 if( newheadind < 0 ) 126 114 { 127 115 newheadind = sgraph.clone(*h,newheadprop); 128 snodes[newheadind].LH=newheadLH;129 snodes[newheadind].in_LH=false;130 snodes[newheadind].LV=newheadLV;131 snodes[newheadind].LD=newheadLD;132 116 list<int>::iterator nextit=h; ++nextit; 133 117 nodelist.insert(nextit,newheadind); 118 sgraph[newheadind].LH=newheadLH; 119 sgraph[newheadind].LD=newheadLD; 120 sgraph[newheadind].in_LH=false; 121 sgraph[newheadind].LV=newheadLV; 134 122 135 123 if(debug) sgraph.print_node_debug(stderr," C ",newheadind); 136 124 } 137 125 else 138 s nodes[newheadind].LD |= newheadLD; // TYLKO DLA LD126 sgraph[newheadind].LD |= newheadLD; // TYLKO DLA LD 139 127 } 140 128 141 snodes[*d].heads.push_back(Arc(newheadind,r,*h)); 142 143 snodes[*d].LH.set(newheadind); 144 145 if(snodes[newheadind].saturated()) snodes[*d].LH |= snodes[newheadind].LH; 146 147 if(debug) 148 sgraph.print_arc(stderr,newheadind,*d,r,1), sgraph.print_node_debug(stderr," U ",*d); 129 sgraph[*d].heads.push_back(Arc(newheadind,r,*h)); 130 131 sgraph[*d].LH.set(newheadind); 132 133 if(sgraph[newheadind].saturated()) sgraph[*d].LH |= sgraph[newheadind].LH; 134 135 if(debug) sgraph.print_arc(stderr,newheadind,*d,r,1), sgraph.print_node_debug(stderr," U ",*d); 149 136 150 137 } … … 156 143 if(sgraph.visible(*i,*j) && sgraph.saturated(*i)) 157 144 { 158 Roles& ji_roles = grammar.connect [snodes[*j].mnode->cat][snodes[*i].mnode->cat];145 Roles& ji_roles = grammar.connectable( mgraph[sgraph[*j].mnode].cat, mgraph[sgraph[*i].mnode].cat ); 159 146 for(RolesIter r=ji_roles.begin(); r!=ji_roles.end();++r) 160 if(grammar.check_constr(s nodes[*j].prop,snodes[*i].prop,0,*r))147 if(grammar.check_constr(sgraph[*j].prop,sgraph[*i].prop,0,*r)) 161 148 connect_left(j,i,*r); 162 149 } … … 169 156 if(sgraph.visible(*i,*j)) 170 157 { 171 Roles& ij_roles = grammar.connect [snodes[*i].mnode->cat][snodes[*j].mnode->cat];158 Roles& ij_roles = grammar.connectable( mgraph[sgraph[*i].mnode].cat, mgraph[sgraph[*j].mnode].cat ); 172 159 for(RolesIter r=ij_roles.begin(); r!=ij_roles.end();++r) 173 if(grammar.check_constr(s nodes[*i].prop,snodes[*j].prop,1,*r))160 if(grammar.check_constr(sgraph[*i].prop,sgraph[*j].prop,1,*r)) 174 161 connect_right(i,j,*r); 175 162 } … … 182 169 for(++i; i!=nodelist.end(); ++i) 183 170 { 184 for(vector<Arc>::iterator da=sgraph .nodes[*i].deps.begin()--; da!=sgraph.nodes[*i].deps.end(); ++da)185 sgraph .nodes[da->dst].heads.push_back(Arc(*i,da->role,da->anc));186 for(vector<Arc>::iterator ha=sgraph .nodes[*i].heads.begin(); ha!=sgraph.nodes[*i].heads.end(); ++ha)187 sgraph .nodes[ha->dst].deps.push_back(Arc(*i,ha->role,ha->anc));171 for(vector<Arc>::iterator da=sgraph[*i].deps.begin()--; da!=sgraph[*i].deps.end(); ++da) 172 sgraph[da->dst].heads.push_back(Arc(*i,da->role,da->anc)); 173 for(vector<Arc>::iterator ha=sgraph[*i].heads.begin(); ha!=sgraph[*i].heads.end(); ++ha) 174 sgraph[ha->dst].deps.push_back(Arc(*i,ha->role,ha->anc)); 188 175 } 189 176 } … … 192 179 void dgp0() 193 180 { 194 snodes=sgraph.nodes;195 181 196 182 nodelist.clear(); … … 198 184 processed=nodelist.begin(); 199 185 200 for(int m=0; m<mgraph. n; ++m)186 for(int m=0; m<mgraph.size() ; ++m) 201 187 { 202 int basenode = sgraph.add_base_snode(mgraph.nodes+m); // ma zwracaæ SNode* 188 int basenode = sgraph.add_base_snode(m); // ma zwracaæ SNode* 189 203 190 set_initial_constraints(basenode); 204 191 nodelist.push_back(basenode); … … 213 200 processed=cursor; 214 201 } 202 215 203 } 216 204 reverse_links(); -
src/dgp/grammar.cc
r5f4d9c3 re7de6cc 1 1 2 #include < stdio.h>2 #include <cstdio> 3 3 4 4 #include "grammar.hh" 5 5 6 bool (*constraint[MAXCONSTRS])(int head, int dep); 7 8 9 int chk_type(const char* s, int lineno) // SIDE EFECTS! 10 { 11 if(Role::index(s)>0) return 1; 12 13 fprintf(stderr,"%8d: Invalid type '%s'. Line ignored.\n",lineno,s); 14 return 0; 15 } 16 17 int chk_cat(const char* s, int lineno) 18 { 19 if(Cat::index(s)>0) return 1; 20 21 fprintf(stderr,"%8d: Invalid category '%s'. Line ignored.\n",lineno,s); 22 return 0; 23 } 6 //bool (*constraint[MAXCONSTRS])(int head, int dep); 7 8 //==================================================================================================== 9 //inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 10 bool chk_type(const char* s) { return Role::index(s)>0; } // PRZENIEŠÆ DO Role 11 //---------------------------------------------------------------------------------------------------- 12 //inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 13 bool chk_long(const char* s) { return LongRel::index(s)>0; } // jw 14 //---------------------------------------------------------------------------------------------------- 15 //inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 16 bool chk_cat(const char* s) { return Cat::index(s)>0; } //jw 17 //---------------------------------------------------------------------------------------------------- 18 //inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 19 bool chk_flag(const char* s) { return Flag::index(s)>0; } //jw 20 //==================================================================================================== 21 22 void grammar_error(int lineno, string s="Grammar error.") 23 { 24 fprintf(stderr,"%8d: %s Line ignored.\n",lineno,s.c_str()); 25 } 26 27 //==================================================================================================== 24 28 25 29 void Grammar::add_category(const char* s) 26 30 { 27 31 Cat::add(s); 28 if(Cat::count()>cats_sz) 32 33 if(connect.size() <= Cat::count()) 29 34 { 30 cats_sz += 16; 31 connect.resize(cats_sz); 32 for(int i=0; i<cats_sz; ++i) 33 connect[i].resize(cats_sz); 34 obl.resize(cats_sz); 35 connect.resize(Cat::count()+RESIZE_DELTA); 36 for(int i=0; i<connect.size(); ++i) 37 if(connect[i].size() <= Cat::count()) connect[i].resize(Cat::count()+RESIZE_DELTA); 35 38 } 39 if(connect1.size() <= Cat::count()) 40 { 41 connect1.resize(Cat::count()+RESIZE_DELTA); 42 for(int i=0; i<=connect1.size(); ++i) 43 if(connect1[i].size() <= Cat::count()) connect1[i].resize(Cat::count()+RESIZE_DELTA); 44 } 45 46 if(longrel.size() <= Cat::count()) 47 { 48 longrel.resize(Cat::count()+RESIZE_DELTA); 49 for(int i=0; i<longrel.size(); ++i) 50 if(longrel[i].size() <= Cat::count()) longrel[i].resize(Cat::count()+RESIZE_DELTA); 51 } 52 53 if(uptrigger.size() <= Cat::count()) 54 uptrigger.resize(Cat::count()+RESIZE_DELTA); 55 if(dntrigger.size() <= Cat::count()) 56 dntrigger.resize(Cat::count()+RESIZE_DELTA); 57 58 if(obl.size() <= Cat::count()) obl.resize(Cat::count()+RESIZE_DELTA); 59 if(set.size() <= Cat::count()) set.resize(Cat::count()+RESIZE_DELTA); 36 60 } 37 61 … … 39 63 { 40 64 Role::add(s); 41 if(Role::count()>types_sz) 42 { 43 types_sz += 16; 44 lt.resize(types_sz); 45 gt.resize(types_sz); 46 } 47 } 48 49 void Grammar::add_flag(const char* s) 50 { 51 Flag::add(s); 52 if(Flag::count()>flags_sz) 53 { 54 flags_sz += 16; 55 pass.resize(flags_sz); 56 } 57 } 58 65 66 if(lt.size() <= Role::count()) lt.resize(Role::count()+RESIZE_DELTA); 67 if(gt.size() <= Role::count()) gt.resize(Role::count()+RESIZE_DELTA); 68 if(pass.size() <= Role::count()) pass.resize(Role::count()+RESIZE_DELTA); 69 if(include.size() <= Role::count()) include.resize(Role::count()+RESIZE_DELTA); 70 if(exclude.size() <= Role::count()) exclude.resize(Role::count()+RESIZE_DELTA); 71 for(int i=0; i<uptrigger.size(); i++) 72 if(uptrigger[i].size() <= Role::count()) uptrigger[i].resize(Role::count()+RESIZE_DELTA); 73 for(int i=0; i<dntrigger.size(); i++) 74 if(dntrigger[i].size() <= Role::count()) dntrigger[i].resize(Role::count()+RESIZE_DELTA); 75 } 76 77 //==================================================================================================== 78 79 bool Grammar::contains_boubble(const list<Boubble*> boubble_list, Boubble* bp) const 80 { 81 for(list<Boubble*>::const_iterator bi = boubble_list.begin(); bi != boubble_list.end(); bi++ ) 82 if(**bi == *bp) return true; 83 return false; 84 } 85 86 //---------------------------------------------------------------------------------------------------- 87 88 void Grammar::add_triggers(Cat src, Cat dest, LongRel l) 89 { 90 for(list<Boubble*>::const_iterator b=boubbles.begin(); b!=boubbles.end(); b++) 91 if((*b)->rel() == l) 92 { 93 list<Boubble*>& boubble_list = ((*b)->dir()==UP) ? uptrigger[src][(*b)->next()] : dntrigger[src][(*b)->next()] ; 94 if(!contains_boubble(boubble_list,*b)) 95 boubble_list.push_back(*b); 96 } 97 } 98 99 //==================================================================================================== 59 100 60 101 void Grammar::set_lt(Role s, Role t) … … 85 126 86 127 128 void Grammar::compute_triggers() 129 { 130 //init uptrigger array 131 uptrigger.resize(Cat::count()); 132 for(int i=0; i<uptrigger.size(); i++) 133 uptrigger[i].resize(Role::count()); 134 //init dntrigger array 135 dntrigger.resize(Cat::count()); 136 for(int i=0; i<dntrigger.size(); i++) 137 dntrigger[i].resize(Role::count()); 138 139 // for(int c=0; c<Cat::count(); c++) 140 // for(int r=0; r<Role::count(); r++) 141 // for(list<Boubble>::const_iterator b=boubbles.begin(); b!=boubbles.end; b++) 142 // if(b->dir()==UP && ) 143 } 144 145 //==================================================================================================== 146 147 list<Boubble*> Grammar::trigger_boubbles(Cat c, Role r, Dir d) 148 { 149 list<Boubble*> boubble_list = (d == UP) ? uptrigger[c][r] : dntrigger[c][r]; 150 list<Boubble*> ret; 151 for(list<Boubble*>::iterator b = boubble_list.begin(); b != boubble_list.end(); ++b) 152 ret.push_back((*b)->step(r,d)); 153 return ret; 154 } 155 156 //==================================================================================================== 157 158 Flag parse_flags(const char* s, const char* v) 159 { 160 char buf[16][17]; 161 int n=sscanf(s,"%[A-Z]%[+-]%[A-Z]%[+-]%[A-Z]%[+-]%[A-Z]%[+-]%[A-Z]%[+-]%[A-Z]%[+-]%[A-Z]%[+-]%[A-Z]%[+-]", 162 buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15],buf[16]); 163 for(int i=2; i<=n; i+=2) 164 if(strcmp(buf[i-1],v)==0) 165 return Flag(buf[i-2]); 166 return Flag("NULL"); 167 } 168 169 170 PropSet parse_props(const char* s) 171 { 172 PropSet ret; 173 char buf[8][17]; 174 int n=sscanf(s,"&%[A-Z]&%[A-Z]&%[A-Z]&%[A-Z]&%[A-Z]&%[A-Z]&%[A-Z]&%[A-Z]",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]); 175 for(int i=1; i<=n; i++) 176 ret.set(Prop(buf[i-1])); 177 return ret; 178 179 } 180 181 87 182 bool Grammar::read(FILE* f) 88 183 { 184 185 //>>> TU? 186 187 Prop::add("INIT"); 188 Prop::add("FIN"); 189 190 //<<< TU? 191 192 89 193 int lineno=0; 90 194 char line[MAXLINE]; // line has the structure: key [arg1 [arg2 [arg3]]] … … 93 197 char arg2[MAXLINE]; 94 198 char arg3[MAXLINE]; 199 char arg4[MAXLINE]; 95 200 96 201 while(fgets(line,MAXLINE,f)) 97 {98 lineno++;99 int fields=sscanf(line,"%s %s %s %s",key,arg1,arg2,arg3);100 101 if(fields<1 || key[0]=='#') continue; // skip empty lines and comments102 103 if (strcmp(key,"CAT")==0 && fields>=2)104 202 { 105 add_category(arg1); 203 lineno++; 204 int fields=sscanf(line,"%s %s %s %s %s",key,arg1,arg2,arg3,arg4); 205 206 if(fields<1 || key[0]=='#') continue; // skip empty lines and comments 207 208 if(fields>1 && arg1[0] == '#') fields=1; 209 if(fields>2 && arg2[0] == '#') fields=2; 210 if(fields>3 && arg3[0] == '#') fields=3; 211 if(fields>4 && arg4[0] == '#') fields=4; 212 213 if (strcmp(key,"CAT")==0 && fields==2) 214 { 215 add_category(arg1); 216 } 217 else if(strcmp(key,"ROLE")==0 && fields==2) 218 { 219 add_type(arg1); 220 } 221 else if(strcmp(key,"SGL")==0 && fields==2) 222 { 223 if(chk_type(arg1)) 224 set_sgl(arg1); 225 else 226 grammar_error(lineno); 227 } 228 else if(strcmp(key,"LEFT")==0 && fields==2) 229 { 230 if(chk_type(arg1)) 231 set_left(arg1); 232 else 233 grammar_error(lineno); 234 } 235 else if(strcmp(key,"RIGHT")==0 && fields==2) 236 { 237 if(chk_type(arg1)) 238 set_right(arg1); 239 else 240 grammar_error(lineno); 241 } 242 else if(strcmp(key,"INITR")==0 && fields==2) 243 { 244 if(chk_type(arg1)) 245 set_init(arg1); 246 else 247 grammar_error(lineno); 248 } 249 else if(strcmp(key,"FINR")==0 && fields==2) 250 { 251 if(chk_type(arg1)) 252 set_fin(arg1); 253 else 254 grammar_error(lineno); 255 } 256 else if(strcmp(key,"INITF")==0 && fields==2) 257 { 258 if(chk_flag(arg1)) 259 set_initf(arg1); 260 else 261 grammar_error(lineno); 262 } 263 else if(strcmp(key,"FINF")==0 && fields==2) 264 { 265 if(chk_flag(arg1)) 266 set_finf(arg1); 267 else 268 grammar_error(lineno); 269 } 270 else if(strcmp(key,"REQ")==0 && fields==3) 271 { 272 if( chk_cat(arg1) && chk_type(arg2) ) 273 set_obl(arg1,arg2); 274 else 275 grammar_error(lineno); 276 } 277 else if(strcmp(key,"CONSTRE")==0 && fields==3) 278 { 279 if( chk_type(arg1) && chk_type(arg2) ) 280 set_exclude(arg1,arg2); 281 else 282 grammar_error(lineno); 283 } 284 else if(strcmp(key,"CONSTRI")==0 && fields==3) 285 { 286 if( chk_type(arg1) && chk_type(arg2) ) 287 set_include(arg1,arg2); 288 else 289 grammar_error(lineno); 290 } 291 else if(strcmp(key,"LONG")==0 && fields ==3) 292 { 293 add_long(arg1,arg2); 294 } 295 else if(strcmp(key,"LINK")==0 && fields==4) 296 { 297 char cat1[MAXLINE],flags1[MAXLINE],cat2[MAXLINE],flags2[MAXLINE],type[MAXLINE],props[MAXLINE]; 298 299 if(sscanf(arg1,"%[^;];%s",cat1,flags1)==1) *flags1='\0'; 300 if(sscanf(arg2,"%[^;];%s",cat2,flags2)==1) *flags2='\0'; 301 if(sscanf(arg3,"%[^&]%s",type,props)==1) *props='\0'; 302 303 // printf("line=%s\n\tcat1=%s flags1=%s cat2=%s flags2=%s type=%s props=%s\n",line,cat1,flags1,cat2,flags2,type,props); 304 305 if( chk_cat(cat1) && chk_cat(cat2) && chk_type(type) ) 306 set_connect(cat1,parse_flags(flags1,"+"),parse_flags(flags1,"-"),cat2,parse_flags(flags2,"+"),parse_flags(flags2,"-"),type,parse_props(props)); 307 else if( chk_cat(cat1) && chk_cat(cat2) && chk_long(type) ) 308 { 309 set_longrel(cat1,cat2,type); 310 add_triggers(cat1,cat2,type); 311 } 312 else 313 grammar_error(lineno); 314 } 315 // else if(strcmp(key,"LINK")==0 && fields==5) 316 // { 317 // if( chk_cat(arg1) && chk_cat(arg2) && chk_type(arg4) ) 318 // set_connect(arg1,arg2,arg3,arg4); 319 // else 320 // grammar_error(lineno); 321 // } 322 // FLAG DECLARATION 323 else if(strcmp(key,"FLAG")==0 && fields==2) 324 { 325 add_flag(arg1); 326 } 327 else if(strcmp(key,"SET")==0 && fields==3) 328 { 329 if( chk_cat(arg1) && chk_flag(arg2) ) 330 set_set(arg1,arg2); 331 else 332 grammar_error(lineno); 333 } 334 else if(strcmp(key,"PASS")==0 && fields==3) 335 { 336 if( chk_type(arg1) && chk_flag(arg2) ) 337 set_pass(arg1,arg2); 338 else 339 grammar_error(lineno); 340 } 341 342 else fprintf(stderr,"Statement not recognized in line %d. Ignored.\n", lineno); 106 343 } 107 else if(strcmp(key,"ROLE")==0 && fields>=2) 108 { 109 add_type(arg1); 110 } 111 else if(strcmp(key,"SGL")==0 && fields>=2) 112 { 113 if(chk_type(arg1,lineno)) 114 set_sgl(arg1); 115 } 116 else if(strcmp(key,"LEFT")==0 && fields>=2) 117 { 118 if(chk_type(arg1,lineno)) 119 set_left(arg1); 120 } 121 else if(strcmp(key,"RIGHT")==0 && fields>=2) 122 { 123 if(chk_type(arg1,lineno)) 124 set_right(arg1); 125 } 126 else if(strcmp(key,"REQ")==0 && fields>=3) 127 { 128 if(chk_cat(arg1,lineno) + chk_type(arg2,lineno) == 2) 129 set_obl(arg1,arg2); 130 } 131 else if(strcmp(key,"LINK")==0 && fields>=4) 132 { 133 if(chk_cat(arg1,lineno) + chk_cat(arg2,lineno) + chk_type(arg3,lineno) == 3) 134 set_connect(arg1,arg2,arg3); 135 } 136 // FLAG DECLARATION 137 else if(strcmp(key,"FLAG")==0 && fields>=2) 138 { 139 add_flag(arg1); 140 } 141 142 else fprintf(stderr,"Invalid line %d. Ignored.\n", lineno); 143 } 144 145 // compute_gt(); 146 344 345 // compute_gt(); 346 147 347 return true; 148 348 149 349 } 350 351 352 void Grammar::write(ostream& os) 353 { 354 for(Cat i=1; i<Cat::count(); ++i) 355 os << "CAT\t" << i.str() << endl; 356 357 for(Role i=1; i<Role::count(); ++i) 358 os << "ROLE\t" << i.str() << endl; 359 360 for(Role i=1; i<Role::count(); ++i) 361 if(sgl.test(i)) 362 os << "SGL\t" << i.str() << endl; 363 364 for(Role i=1; i<Role::count(); ++i) 365 if(left.test(i)) 366 os << "LEFT\t" << i.str() << endl; 367 368 for(Role i=1; i<Role::count(); ++i) 369 if(right.test(i)) 370 os << "RIGHT\t" << i.str() << endl; 371 372 for(Cat c=1; c<Cat::count(); ++c) 373 for(Role r=1; r<Role::count(); ++r) 374 if(obl[c].test(r)) 375 os << "REQ\t" << c.str() << "\t" << r.str() << endl; 376 377 for(Cat c=1; c<Cat::count(); ++c) 378 for(Cat d=1; d<Cat::count(); ++d) 379 for(Links::const_iterator l = connect1[c][d].begin(); l != connect1[c][d].end(); l++) 380 { 381 os << "LINK\t" << c.str(); 382 if(l->hflagplus||l->hflagminus) 383 { 384 os << ";"; 385 if(l->hflagplus) os << (l->hflagplus).str() << "+"; 386 if(l->hflagminus) os << (l->hflagminus).str() << "-"; 387 } 388 os << "\t" << d.str(); 389 if(l->dflagplus||l->dflagminus) 390 { 391 os << ";"; 392 if(l->dflagplus) os << (l->dflagplus).str() << "+"; 393 if(l->dflagminus) os << (l->dflagminus).str() << "-"; 394 } 395 os << "\t" << (l->role).str(); 396 for(Prop p=0; p<Prop::count(); ++p) 397 if(l->props[p]) 398 os << "&" << p.str(); 399 os << endl; 400 } 401 402 for(LongRel i=1; i<LongRel::count(); ++i) 403 os << "LONG\t" << i.str() << endl; 404 405 for(list<Boubble*>::const_iterator b = boubbles.begin(); b != boubbles.end(); b++) 406 os << "BOUBBLE\t" << **b << endl; 407 408 for(Cat c=1; c<Cat::count(); ++c) 409 for(Cat d=1; d<Cat::count(); ++d) 410 for(LongRel l=1; l<LongRel::count(); ++l) 411 if(longrel[c][d].count(l)) 412 os << "LLINK\t" << c.str() << "\t" << d.str() << "\t" << l.str() << endl; 413 414 for(Cat c=1; c<Cat::count(); ++c) 415 for(Flag f=1; f<Flag::count(); ++f) 416 if(set[c].test(f)) 417 os << "SET\t" << c.str() << "\t" << f.str() << endl; 418 419 for(Role r=1; r<Role::count(); ++r) 420 for(Flag f=1; f<Flag::count(); ++f) 421 if(pass[r].test(f)) 422 os << "PASS\t" << r.str() << "\t" << f.str() << endl; 423 424 for(Role r=1; r<Role::count(); ++r) 425 for(Role t=1; t<Role::count(); ++t) 426 if(include[r].test(t)) 427 os << "CONSTRI\t" << r.str() << "\t" << t.str() << endl; 428 429 for(Role r=1; r<Role::count(); ++r) 430 for(Role t=1; t<Role::count(); ++t) 431 if(exclude[r].test(t)) 432 os << "CONSTRE\t" << r.str() << "\t" << t.str() << endl; 433 434 for(Cat c=1; c<Cat::count(); ++c) 435 for(Role r=1; r<Role::count(); ++r) 436 for(list<Boubble*>::const_iterator b=uptrigger[c][r].begin(); b!=uptrigger[c][r].end(); b++) 437 os << "TRIGGER-UP\t" << c.str() << "\t" << r.str() << "\t" << (*b)->rel().str() << endl; 438 439 for(Cat c=1; c<Cat::count(); ++c) 440 for(Role r=1; r<Role::count(); ++r) 441 for(list<Boubble*>::const_iterator b=dntrigger[c][r].begin(); b!=dntrigger[c][r].end(); b++) 442 os << "TRIGGER-DN\t" << c.str() << "\t" << r.str() << "\t" << (*b)->rel().str() << endl; 443 444 for(Flag i=1; i<Flag::count(); ++i) 445 os << "FLAG\t" << i.str() << endl; 446 } 447 448 449 450 451 452 453 454 455 456 //================================= OLD 150 457 151 458 void Grammar::write(FILE* f) … … 155 462 156 463 for(Role i=1; i<Role::count(); ++i) 157 fprintf(f,"ROLE\t%s \n",i.str());464 fprintf(f,"ROLE\t%s (%d)(%d)\n",i.str(),Role::index(i.str()),chk_type(i.str())); 158 465 159 466 for(Role i=1; i<Role::count(); ++i) … … 176 483 fprintf(f,"LINK\t%s\t%s\t%s\n",c.str(),d.str(),t.str()); 177 484 485 for(LongRel i=1; i<LongRel::count(); ++i) 486 fprintf(f,"LONG\t%s\n",i.str()); 487 488 for(Cat c=1; c<Cat::count(); ++c) 489 for(Cat d=1; d<Cat::count(); ++d) 490 for(LongRel l=1; l<LongRel::count(); ++l) 491 if(longrel[c][d].count(l)) 492 fprintf(f,"LLINK\t%s\t%s\t%s\n",c.str(),d.str(),l.str()); 493 494 for(Cat c=1; c<Cat::count(); ++c) 495 for(Role r=1; r<Role::count(); ++r) 496 for(list<Boubble*>::const_iterator b=uptrigger[c][r].begin(); b!=uptrigger[c][r].end(); b++) 497 fprintf(f,"#TRIGGER\t%s\t%s\t%s\n",c.str(),r.str(),(*b)->rel().str()); 498 499 for(Cat c=1; c<Cat::count(); ++c) 500 for(Role r=1; r<Role::count(); ++r) 501 for(list<Boubble*>::const_iterator b=dntrigger[c][r].begin(); b!=dntrigger[c][r].end(); b++) 502 fprintf(f,"#TRIGGER\t%s\t%s\t%s\n",c.str(),r.str(),(*b)->rel().str()); 503 178 504 for(Flag i=1; i<Flag::count(); ++i) 179 505 fprintf(f,"FLAG\t%s\n",i.str()); 180 506 } 181 -
src/dgp/grammar.hh
r5f4d9c3 re7de6cc 10 10 #include "thesymbols.hh" 11 11 #include "sgraph.hh" 12 13 14 class Link 15 { 12 #include "boubble.hh" 13 14 using namespace std; 15 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 28 { 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") 31 : role(r), props(ps), hflagplus(hfp), hflagminus(hfm), dflagplus(dfp), dflagminus(dfm) { } 32 //Link(Role r) : role(r), dflagplus("NULL") { } 33 16 34 Role role; 17 FlagSet hflags; 18 FlagSet dflags; 35 Flag hflagplus; 36 Flag hflagminus; 37 Flag dflagplus; 38 Flag dflagminus; 39 PropSet props; 40 41 bool operator<(const Link& l) const 42 { 43 if(role < l.role) return true; 44 if(hflagplus < l.hflagplus) return true; 45 if(hflagminus < l.hflagminus) return true; 46 if(dflagplus < l.dflagplus) return true; 47 if(dflagminus < l.dflagminus) return true; 48 if(props.to_ulong() < l.props.to_ulong()) return true; 49 return false; 50 } 51 19 52 }; 20 53 54 typedef set<Link> Links; 55 56 //==================================================================================================== 57 // class Grammar 58 //==================================================================================================== 21 59 22 60 class Grammar … … 25 63 public: 26 64 27 // enum CONSTR { SGL, OBL, LEFT, RIGHT, INIT, NONINIT, FIN, NONFIN }; 28 29 Grammar() : types_sz(0), cats_sz(0), flags_sz(0) {} ; 65 static const int RESIZE_DELTA=16; 66 67 Grammar() {} ; 68 69 Roles& connectable(Cat h, Cat d); 70 Roles connectable(Cat h, Cat d, FlagSet f, FlagSet df); 71 72 list<const Link*> connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs); 73 74 bool check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role); 75 bool check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link); 76 77 bool check_longrel(Cat hcat, Cat dcat, LongRel rel); 78 bool is_sgl(Role r); 79 RoleSet is_obl(Cat c); 80 81 RoleSet& constr_include(Role r) { return include[r]; }; 82 RoleSet& constr_exclude(Role r) { return exclude[r]; }; 30 83 31 int types_sz; 32 int cats_sz; 33 int flags_sz; 34 35 vector< vector< Roles > > connect; 84 FlagSet initial_flags(Cat c) { return set[c]; } 85 FlagSet pass_flags(Role r) { return pass[r]; } 86 87 list<Boubble*> trigger_boubbles(Cat c, Role r, Dir d); 88 89 bool read(FILE* f); 90 void write(ostream& os); 91 void write(FILE* f); 92 93 private: 94 36 95 RoleSet sgl; 37 vector< RoleSet > obl; 96 vector< RoleSet > obl; //[Cat] 38 97 RoleSet left; 39 98 RoleSet right; 40 vector< RoleSet > lt; 41 vector< RoleSet > gt; 42 43 44 // vector< vector< vector< 45 vector< FlagSet > set; 46 vector< FlagSet > pass; 47 48 bool read(FILE* f); 49 void write(FILE* f); 99 RoleSet init; 100 RoleSet fin; 101 FlagSet initf; 102 FlagSet finf; 103 104 vector< RoleSet > lt; //[Role] 105 vector< RoleSet > gt; //[Role] 106 107 vector< FlagSet > set; //[Cat] 108 // vector< FlagSet > rset; //[Role] 109 vector< FlagSet > pass; //[Role] 110 111 vector< vector< Roles > > connect; //[Cat][Cat] 112 113 vector< vector< Links > > connect1; //[Cat][Cat] 114 115 vector< RoleSet > include; //[Role] 116 vector< RoleSet > exclude; //[Role] 117 118 vector< vector< LongRels > > longrel; //[Cat][Cat] 119 120 list< Boubble* > boubbles; 121 122 vector< vector< list<Boubble*> > > uptrigger;//[Cat][Role] 123 vector< vector< list<Boubble*> > > dntrigger;//[Cat][Role] 50 124 51 125 void add_category(const char* s); 52 126 void add_type(const char* s); 53 void add_flag(const char* s); 54 55 void set_sgl(Role r) { sgl.set(r); } 56 void set_obl(Cat c, Role r) { obl[c].set(r); } 57 void set_left(Role r) { left.set(r); } 58 void set_right(Role r) { right.set(r); } 59 void set_order(Role r, Role s) { lt[s].set(r); } 60 void set_connect(Cat c, Cat d, Role r) { connect[c][d].insert(r); } 127 void add_flag(const char* s) { Flag::add(s); } 128 void add_long(const char* l, const char* p) { LongRel::add(l); boubbles.push_back( new Boubble(p,l) ); } 129 void add_triggers(Cat h, Cat d, LongRel l); 130 131 void set_sgl(Role r) { sgl.set(r); } 132 void set_obl(Cat c, Role r) { obl[c].set(r); } 133 void set_left(Role r) { left.set(r); } 134 void set_right(Role r) { right.set(r); } 135 void set_init(Role r) { init.set(r); } 136 void set_fin(Role r) { fin.set(r); } 137 void set_initf(Flag f) { initf.set(f); } 138 void set_finf(Flag f) { finf.set(f); } 139 void set_order(Role r, Role s) { lt[s].set(r); } 140 141 // void set_connect(Cat c, Cat d, Role r) { connect[c][d].insert(r); } 142 // void set_connect(Cat c, Cat d, Flag f, Role r) { connect1[c][d].insert(Link(r,f)); } 143 144 void set_connect(Cat h, Flag hfp, Flag hfm, Cat d, Flag dfp, Flag dfm, Role r, PropSet ps ) { connect1[h][d].insert(Link(r,ps,hfp,hfm,dfp,dfm)); } 145 146 void set_include(Role r, Role s) { include[r].set(s); } 147 void set_exclude(Role r, Role s) { exclude[r].set(s); } 148 void set_longrel(Cat c, Cat d, LongRel l){ longrel[c][d].insert(l); } 149 void set_set(Cat c, Flag f) { set[c].set(f); } 150 void set_pass(Role r, Flag f) { pass[r].set(f); } 61 151 void set_lt(Role r, Role s); 62 152 void compute_gt(); 63 64 65 bool check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role); 153 void compute_triggers(); 154 bool contains_boubble(const list<Boubble*> boubble_list, Boubble* bp) const; 66 155 67 156 }; 68 157 69 inline bool Grammar::check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role) 158 //---------------------------------------------------------------------------------------------------- 159 160 inline 161 Roles& Grammar::connectable(Cat h, Cat d) 162 { 163 return connect[h][d]; 164 } 165 166 //---------------------------------------------------------------------------------------------------- 167 168 inline 169 Roles Grammar::connectable(Cat h, Cat d, FlagSet hfs, FlagSet dfs) // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&) 170 { 171 Roles ret; 172 for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++) 173 if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) ) 174 if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) ) 175 ret.insert(l->role); 176 return ret; 177 } 178 179 //---------------------------------------------------------------------------------------------------- 180 181 inline 182 list<const Link*> Grammar::connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs) // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&) 183 { 184 list<const Link*> ret; 185 for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++) 186 if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) ) 187 if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) ) 188 ret.push_back(&(*l)); 189 return ret; 190 } 191 192 //---------------------------------------------------------------------------------------------------- 193 194 inline 195 bool Grammar::check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role) // dir: 0-left 1-right 70 196 { 71 197 return 72 198 !hprop.forbidden[role] && 73 ( !right[role] || dir==1 ) && 74 ( !left[role] || dir==0 ) 199 ( dir==1 || !right[role] ) && 200 ( dir==0 || !left[role] ) && 201 ( dir==1 || (hprop.attached&init).none() ) && 202 ( dir==0 || (hprop.attached&fin).none() ) 75 203 ; 76 204 } 77 205 206 //---------------------------------------------------------------------------------------------------- 207 208 inline 209 bool Grammar::check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link) // dir: 0-left 1-right 210 { 211 return 212 !hprop.forbidden[link.role] && 213 ( dir==1 || !right[link.role] ) && 214 ( dir==0 || !left[link.role] ) && 215 ( dir!=0 || !hprop.init_attached ) && 216 ( dir!=1 || !hprop.fin_attached ) 217 ; 218 } 219 220 //---------------------------------------------------------------------------------------------------- 221 222 inline 223 bool Grammar::check_longrel(Cat hcat, Cat dcat, LongRel rel) 224 { 225 return longrel[hcat][dcat].find(rel) != longrel[hcat][dcat].end(); 226 } 227 228 //---------------------------------------------------------------------------------------------------- 229 230 inline bool Grammar::is_sgl(Role r) 231 { 232 return sgl[r]; 233 } 234 235 //---------------------------------------------------------------------------------------------------- 236 237 inline RoleSet Grammar::is_obl(Cat c) 238 { 239 return obl[c]; 240 } 241 242 //==================================================================================================== 78 243 79 244 #endif -
src/dgp/main.cc
r5f4d9c3 re7de6cc 11 11 #include "sgraph.hh" 12 12 #include "grammar.hh" 13 #include "dgp 0.hh"13 #include "dgp1.hh" 14 14 #include "../common/common.h" 15 15 #include "cmdline.h" … … 25 25 Grammar grammar; 26 26 MGraph mgraph; 27 SGraph sgraph ;27 SGraph sgraph(mgraph); 28 28 29 29 FILE* grammarf; … … 65 65 fclose(grammarf); 66 66 67 68 69 // grammar.write(cout); 70 // exit(0); 71 72 73 67 74 mgraph.clear(); 68 75 sgraph.clear(); … … 83 90 if(strcmp(segtype,"EOS")==0) 84 91 { 85 dgp 0(); // parametry!!! MGraph, SGraph, Grammar92 dgp1(); // parametry!!! MGraph, SGraph, Grammar 86 93 output(); 87 94 … … 105 112 if(seg_mnode[si]>=0) 106 113 { 107 MNode& m=mgraph .nodes[seg_mnode[si]];114 MNode& m=mgraph[seg_mnode[si]]; 108 115 for(vector<int>::iterator s=m.snodes.begin(); s!=m.snodes.end(); ++s) 109 116 { -
src/dgp/mgraph.cc
r5f4d9c3 re7de6cc 4 4 #include "const.hh" 5 5 6 #include < stdio.h>6 #include <cstdio> 7 7 8 8 int MGraph::add_node(char* seg) 9 9 { 10 nodes[n].clear(); 10 MNode newnode; 11 newnode.clear(); 11 12 12 char field1[80], field3[80], descr[256], gph[256];13 char field1[80], field3[80], field4[256], descr[256], gph[256]; 13 14 char* cat; 14 15 15 16 getfield(seg,"1",field1); 16 n odes[n].pos=atoi(field1);17 newnode.pos=atoi(field1); 17 18 18 19 getfield(seg,"3",field3); 20 21 getfield(seg,"4",field4); 22 strcpy(newnode.form,field4); 23 19 24 if(!getfield(seg,"lem",descr)) strcpy(descr,"?,?"); 20 25 … … 23 28 if(*cat) ++cat; 24 29 25 // Cat::add(cat);26 30 if(Cat::index(cat)>0) 27 n odes[n].cat=cat;31 newnode.cat=cat; 28 32 else 29 n odes[n].cat="NULL";33 newnode.cat="NULL"; 30 34 31 n odes[n].pred.clear();35 newnode.pred.clear(); 32 36 33 37 char* tok; … … 41 45 42 46 char* ids=strtok(gph,":"); 43 if( n!=atoi(ids)){fprintf(stderr,"Invalid node id in line ?. Program aborted.\n"); exit(1); }47 if(size() != atoi(ids)) {fprintf(stderr,"Invalid node id in line ?. Program aborted.\n"); exit(1); } 44 48 45 49 char *preds; … … 47 51 { 48 52 previd=atoi(preds); 49 n odes[n].pred.push_back(&nodes[previd]);53 newnode.pred.push_back(previd); 50 54 } 51 55 52 return n++; 56 nodes.push_back(newnode); 57 return nodes.size()-1; 53 58 } 54 59 -
src/dgp/mgraph.hh
r5f4d9c3 re7de6cc 8 8 #include "../common/common.h" 9 9 10 11 using namespace std; 12 10 13 class MNode 11 14 { 12 15 public: 13 16 14 char type[MAXFORMLEN]; 17 int pos; 18 char form[256]; 15 19 Cat cat; 16 int pos; 17 vector<MNode*> pred; 20 vector<int> pred; 18 21 vector<int> snodes; 19 22 20 23 void clear() { snodes.clear(); }; 21 24 }; 22 25 23 26 class MGraph 24 27 { 25 28 public: 26 29 27 MNode nodes[MAXNODES]; 28 int n; 30 void clear() { nodes.clear(); } 31 int size() { return nodes.size(); } 32 int add_node(char* seg); 33 MNode& operator[](int i) { return nodes[i]; } 29 34 30 void clear() { n=0; }; 31 int add_node(char* seg); 35 private: 36 37 vector<MNode> nodes; 38 32 39 }; 33 40 -
src/dgp/sgraph.cc
r5f4d9c3 re7de6cc 1 1 #include "global.hh" 2 2 #include "sgraph.hh" 3 #include "mgraph.hh"4 3 #include "grammar.hh" 5 4 #include "const.hh" 6 #include <stdio.h> 5 #include <cstdio> 6 #include <sstream> 7 7 8 extern MGraph mgraph; 8 9 9 int SGraph::add_base_snode(MNode* mn) 10 //==================================================================================================== 11 12 int SGraph::add_base_snode(int mnodeind) 10 13 { 11 int nodeind=n; 12 SNode &node=nodes[n]; 14 SNode& newnode = makenewnode(); 13 15 14 n ode.clear();16 newnode.mnode=mnodeind; 15 17 16 node.mnode=mn; 17 18 for(vector<MNode*>::iterator pm=node.mnode->pred.begin(); pm!=node.mnode->pred.end(); ++pm) 19 for(vector<int>::iterator ps=(*pm)->snodes.begin(); ps!=(*pm)->snodes.end(); ++ps) 18 for(vector<int>::iterator pm=mgraph[newnode.mnode].pred.begin(); pm!=mgraph[newnode.mnode].pred.end(); ++pm) 19 for(vector<int>::iterator ps=mgraph[*pm].snodes.begin(); ps!=mgraph[*pm].snodes.end(); ++ps) 20 20 if(nodes[*ps].in_LH) 21 21 { 22 n ode.LV.set(*ps);23 if(nodes[*ps].saturated()) n ode.LV |= nodes[*ps].LH;22 newnode.LV.set(*ps); 23 if(nodes[*ps].saturated()) newnode.LV |= nodes[*ps].LH; 24 24 } 25 25 26 mn->snodes.push_back(nodeind); 27 ++n; 26 mgraph[newnode.mnode].snodes.push_back(lastnodeind()); 28 27 29 n ode.in_LH=true;28 newnode.in_LH=true; 30 29 31 return nodeind;30 return lastnodeind(); 32 31 } 33 32 33 //==================================================================================================== 34 34 35 35 void SGraph::update_left(int headind, int depind) … … 37 37 SNode &head=nodes[headind], &dep=nodes[depind]; 38 38 39 if(dep.saturated()) head.LV |= dep.LV,head.LD |= dep.LD;39 if(dep.saturated()) head.LV |= dep.LV, head.LD |= dep.LD; 40 40 } 41 41 … … 46 46 47 47 dep.LH.set(headind); 48 if(head.saturated()) 49 dep.LH |= head.LH; 50 } 48 if(head.saturated()) dep.LH |= head.LH; 49 } 51 50 51 //==================================================================================================== 52 52 53 53 int SGraph::clone(int ancind, NodeProp newprop) 54 54 { 55 int newind = n++; 56 SNode &newnode=nodes[newind]; 55 SNode &newnode=makenewnode(); 57 56 SNode &ancnode = nodes[ancind]; 58 57 59 newnode.clear(); 58 59 60 60 newnode.prop=newprop; 61 61 newnode.mnode=ancnode.mnode; 62 newnode.mnode->snodes.push_back(newind); 63 return newind; 62 mgraph[newnode.mnode].snodes.push_back(lastnodeind()); 63 64 return lastnodeind(); 64 65 } 65 66 66 67 //------------------------------------------------------------------------- 68 //------------------------------------------------------------------------- 69 67 //==================================================================================================== 70 68 71 69 int SGraph::print_node(FILE* f, int n, unsigned int info) 72 70 { 73 char buf[ 1000];74 sprint_node(buf,n, info);71 char buf[50000]; 72 sprint_node(buf,n,-1,info); 75 73 fputs(buf,f); 76 74 } 77 75 78 int SGraph::sprint_node(char* buf, int nodeind, unsigned int info) 76 //---------------------------------------------------------------------------------------------------- 77 78 int SGraph::print_node_debug(FILE* f, const char* pref, int n, int anc) 79 { 80 char buf[50000]; 81 sprint_node_debug(buf,pref,n,anc); 82 fputs(buf,f); 83 } 84 85 //---------------------------------------------------------------------------------------------------- 86 87 void SGraph::print_arc(FILE* f, int head, int dep, Role role, int dir) // 0 - left, 1 - right 88 { 89 if(dir==0) 90 fprintf(f,"#A %s:%d <-- %d\n", role.str(), dep, head); 91 else 92 fprintf(f,"#A %s:%d --> %d\n", role.str(), head, dep); 93 } 94 95 //==================================================================================================== 96 97 int SGraph::sprint_node(char* buf, int nodeind, int anc, unsigned int info) 79 98 { 80 99 char* buf0=buf; 81 char descr[256];82 char nodeinfo[16];83 100 84 101 SNode &node=nodes[nodeind]; 85 102 86 103 buf+=sprintf(buf," dgp:%d",nodeind); 104 if(anc>=0) buf+=sprintf(buf,"(%d)",anc); 87 105 buf+=sprintf(buf, saturated(nodeind) ? ";s" : ";u"); 88 106 107 if (info&HEADS || info&DEPS) 108 buf+=sprintf(buf,";"); 109 89 110 bool cont=false; 111 90 112 if (info&HEADS) 91 {92 buf+=sprintf(buf,";");93 113 for(vector<Arc>::iterator h=node.heads.begin(); h!=node.heads.end(); ++h) 94 114 { 95 115 if(cont) buf+=sprintf(buf,","); else cont=true; 96 buf+=sprintf(buf,"++%s-%d /%d",h->role.str(),h->dst,h->anc);116 buf+=sprintf(buf,"++%s-%d(%d~%d)",h->role.str(),h->dst,h->headanc,h->depanc); 97 117 } 98 } 99 118 100 119 if (info&DEPS) 101 {102 buf+=sprintf(buf,";");103 120 for(vector<Arc>::iterator d=node.deps.begin(); d!=node.deps.end(); ++d) 104 121 { 105 122 // if(! nodes[d->dst].saturated()) continue; // NIE DRUKUJ NIENASYCONYCH PODRZEDNIKOW 106 123 if(cont) buf+=sprintf(buf,","); else cont=true; 107 buf+=sprintf(buf,"--%s-%d /%d",d->role.str(),d->dst,d->anc);124 buf+=sprintf(buf,"--%s-%d(%d~%d)",d->role.str(),d->dst,d->headanc,d->depanc); 108 125 } 109 }110 126 111 127 if (info&SETS) … … 113 129 int ord=0; 114 130 buf+=sprintf(buf,";{"); 115 for(vector< MNode*>::iterator pm=node.mnode->pred.begin(); pm!=node.mnode->pred.end(); ++pm)116 for(vector<int>::iterator ps= (*pm)->snodes.begin(); ps!=(*pm)->snodes.end(); ++ps)131 for(vector<int>::iterator pm=mgraph[node.mnode].pred.begin(); pm!=mgraph[node.mnode].pred.end(); ++pm) 132 for(vector<int>::iterator ps=mgraph[*pm].snodes.begin(); ps!=mgraph[*pm].snodes.end(); ++ps) 117 133 buf+=sprintf(buf, ord++ ? ",%d" : "%d", *ps); 118 134 buf+=sprintf(buf,"};{"); 119 ord=0;for(int j=0; j< =n; ++j) if(node.LV[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j);135 ord=0;for(int j=0; j<size(); ++j) if(node.LV[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); 120 136 buf+=sprintf(buf,"};{"); 121 ord=0;for(int j=0; j< =n; ++j) if(node.LH[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j);137 ord=0;for(int j=0; j<size(); ++j) if(node.LH[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); 122 138 buf+=sprintf(buf,"};{"); 123 ord=0;for(int j=0; j< =n; ++j) if(node.LD[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j);139 ord=0;for(int j=0; j<size(); ++j) if(node.LD[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); 124 140 buf+=sprintf(buf,"}"); 125 141 } … … 133 149 for(Role i=1; i<=Role::count(); ++i) 134 150 if(node.prop.required[i]) buf+=sprintf(buf,"%s&%s",(cont++)?",":"",i.str()); 151 for(Role i=1; i<=Role::count(); ++i) 152 if(node.prop.attached[i]) buf+=sprintf(buf,"%s+%s",(cont++)?",":"",i.str()); 153 for(Flag i=1; i<=Flag::count(); ++i) 154 if(node.prop.flags [i]) buf+=sprintf(buf,"%s<%s>",(cont++)?",":"",i.str()); 155 if(node.prop.init_attached) 156 buf+=sprintf(buf,"<init>"); 157 if(node.prop.fin_attached) 158 buf+=sprintf(buf,"<fin>"); 159 160 stringstream oss; 161 for(list<Boubble*>::iterator b = node.prop.boubbles.begin(); b != node.prop.boubbles.end(); b++) 162 oss << (cont++ ? "," : "") << **b; 163 buf+=sprintf(buf,oss.str().c_str()); 135 164 } 136 165 … … 141 170 142 171 143 int SGraph::sprint_node_debug(char* buf, const char* pref, int n )172 int SGraph::sprint_node_debug(char* buf, const char* pref, int n, int anc) 144 173 { 145 174 char *buf0 = buf; 146 175 buf+=sprintf(buf,"#%s",pref); 147 buf+=sprint_node(buf,n,HEADS|DEPS|SETS|CONSTRAINTS); 176 177 buf+=sprintf(buf,"%-16s",form(n)); 178 179 buf+=sprint_node(buf,n,anc,HEADS|DEPS|SETS|CONSTRAINTS); 148 180 buf+=sprintf(buf,"\n"); 149 181 return buf-buf0; 150 182 } 151 183 152 int SGraph::print_node_debug(FILE* f, const char* pref, int n)153 {154 char buf[1000];155 sprint_node_debug(buf,pref,n);156 fputs(buf,f);157 }158 159 void SGraph::print_arc(FILE* f, int left, int right, Role role, int dir) // 0 - left, 1 - right160 {161 fprintf(f,"# %s:%s.%02d %s %s.%02d\n",162 role.str(),nodes[left].mnode->type,left,163 dir ? "-->" : "<--",164 nodes[right].mnode->type,right);165 } -
src/dgp/sgraph.hh
r5f4d9c3 re7de6cc 9 9 10 10 #include "const.hh" 11 #include "mgraph.hh" 11 12 #include "thesymbols.hh" 12 13 14 class MNode; 15 16 13 #include "boubble.hh" 14 15 16 using namespace std; 17 18 //==================================================================================================== 19 // CLASS Arc 20 //==================================================================================================== 17 21 struct Arc 18 22 { 19 23 int dst; 20 24 Role role; 21 int anc; 25 int headanc; 26 int depanc; 22 27 23 Arc(int d, Role r, int a) : dst(d), role(r), anc(a) {}; 24 }; 25 28 Arc(int d, Role r, int ha, int da) : dst(d), role(r), headanc(ha), depanc(da) {}; 29 }; 30 31 //==================================================================================================== 32 // CLASS NodeProp 33 //==================================================================================================== 26 34 27 35 struct NodeProp 28 36 { 29 bitset<MAXTYPES> required; 30 bitset<MAXTYPES> forbidden; 31 32 bool operator==(const NodeProp& p) 33 { return required==p.required && forbidden==p.forbidden; } 34 35 void clear() 36 { required.reset(), forbidden.reset(); } 37 38 }; 39 37 NodeProp(); 38 NodeProp(const NodeProp& p); 39 ~NodeProp(); 40 41 bool operator==(const NodeProp& p); 42 NodeProp& operator=(const NodeProp& p); 43 44 void clear_boubbles(); 45 void merge_boubbles(list<Boubble*> new_boubbles); 46 47 void copy(const NodeProp& p); 48 void clear(); 49 50 RoleSet required; 51 RoleSet forbidden; 52 RoleSet attached; 53 54 bool init_attached; 55 bool fin_attached; 56 57 FlagSet flags; 58 59 list<Boubble*> boubbles; 60 }; 61 62 //---------------------------------------------------------------------------------------------------- 63 64 inline 65 bool NodeProp::operator==(const NodeProp& p) 66 { 67 if(required != p.required) return false; 68 if(forbidden != p.forbidden) return false; 69 if(attached != p.attached) return false; 70 if(flags != p.flags) return false; 71 if(init_attached != p.init_attached) return false; 72 if(fin_attached != p.fin_attached) return false; 73 74 list<Boubble*>::const_iterator b1 = p.boubbles.begin(); 75 for(list<Boubble*>::const_iterator b = boubbles.begin(); b != boubbles.end(); b++) 76 { 77 if(b1 == p.boubbles.end()) 78 return false; 79 if(!(**b == **b1)) 80 return false; 81 } 82 if(b1 != p.boubbles.end()) 83 return false; 84 85 return true; 86 } 87 88 //---------------------------------------------------------------------------------------------------- 89 90 inline 91 void NodeProp::clear_boubbles() 92 { 93 for(list<Boubble*>::iterator b = boubbles.begin(); b!=boubbles.end(); b++) 94 delete *b; 95 boubbles.clear(); 96 } 97 98 //---------------------------------------------------------------------------------------------------- 99 100 inline 101 void NodeProp::merge_boubbles(list<Boubble*> new_boubbles) 102 { 103 boubbles.merge(new_boubbles); 104 } 105 106 //---------------------------------------------------------------------------------------------------- 107 108 inline 109 void NodeProp::copy(const NodeProp& p) 110 { 111 required=p.required; 112 forbidden=p.forbidden; 113 attached=p.attached; 114 flags=p.flags; 115 init_attached=p.init_attached; 116 fin_attached=p.fin_attached; 117 for(list<Boubble*>::const_iterator b = p.boubbles.begin(); b!=p.boubbles.end(); b++) 118 boubbles.push_back(new Boubble(**b)); 119 } 120 121 //---------------------------------------------------------------------------------------------------- 122 123 inline 124 NodeProp::~NodeProp() 125 { 126 clear_boubbles(); 127 } 128 //---------------------------------------------------------------------------------------------------- 129 130 inline 131 NodeProp::NodeProp() 132 { 133 clear(); 134 } 135 136 //---------------------------------------------------------------------------------------------------- 137 138 inline 139 NodeProp::NodeProp(const NodeProp& p) 140 { 141 copy(p); 142 } 143 144 //---------------------------------------------------------------------------------------------------- 145 146 inline 147 NodeProp& NodeProp::operator=(const NodeProp& p) 148 { 149 clear(); 150 copy(p); 151 return *this; 152 } 153 154 //---------------------------------------------------------------------------------------------------- 155 156 inline 157 void NodeProp::clear() 158 { 159 required.reset(); 160 forbidden.reset(); 161 attached.reset(); 162 init_attached=false; 163 fin_attached=false; 164 clear_boubbles(); 165 } 166 167 //==================================================================================================== 168 // CLASS SNode 169 //==================================================================================================== 40 170 41 171 struct SNode 42 172 { 43 173 44 MNode*mnode;174 int mnode; 45 175 46 176 NodeProp prop; … … 54 184 vector<Arc> deps; 55 185 56 void clear() { prop.clear(), LV.reset(), LD.reset(), LH.reset(), heads.clear(), deps.clear(); } 57 bool saturated() { return prop.required.none(); } 58 }; 59 60 186 void clear(); 187 bool saturated(); 188 }; 189 190 //---------------------------------------------------------------------------------------------------- 191 inline 192 void SNode::clear() 193 { prop.clear(), LV.reset(), LD.reset(), LH.reset(), heads.clear(), deps.clear(); } 194 //---------------------------------------------------------------------------------------------------- 195 inline 196 bool SNode::saturated() 197 { return prop.required.none(); } 198 199 //==================================================================================================== 200 // SGraph CLASS 201 //==================================================================================================== 61 202 62 203 class SGraph … … 64 205 public: 65 206 66 SNode nodes[MAXNODES]; 67 int n; // number of nodes 68 69 enum Output { HEADS=1, DEPS=2, SETS=4, CONSTRAINTS=8 }; 70 71 SGraph() : n(0) {} 72 73 void clear() { n=0; } 74 75 int add_base_snode(MNode* mn); 76 int clone(int ancind, NodeProp newprop); 207 enum Output { HEADS=1, DEPS=2, SETS=4, CONSTRAINTS=8, BOUBBLES=16 }; 208 209 SGraph(MGraph& mg) : mgraph(mg) { clear(); } 210 211 SNode& operator[](const int i) { return nodes[i]; } 212 213 void clear() { nodes.clear(); } 214 int add_base_snode(int mnodeind); 215 int clone(int ancind, NodeProp newprop); 77 216 void update_left(int headind, int depind); 78 217 void update_right(int headind, int depind); 79 80 218 bool visible(int left, int right); 81 219 bool saturated(int node); 82 220 83 //-------------------------------------------------------------------- 84 85 void read(FILE* f); 86 void write(FILE* f, list<int> nodelist, unsigned int info); 87 88 int sprint_node(char* buf, int n, unsigned int info); 221 Cat cat(int i) const { return mgraph[nodes[i].mnode].cat; } 222 char* form(int i) const { return mgraph[nodes[i].mnode].form; } 223 89 224 int print_node(FILE* f, int n, unsigned int info); 90 int sprint_node_debug(char* buf, const char* pref, int n); 91 int print_node_debug(FILE* f, const char* pref, int n); 225 int print_node_debug(FILE* f, const char* pref, int n, int anc); 92 226 93 227 void print_arc(FILE* f, int left, int right, Role role, int dir); // 0 - left, 1 - right 94 228 95 }; 96 229 //private: 230 231 int size() {return nodes.size(); } 232 233 private: 234 235 MGraph& mgraph; 236 237 vector<SNode> nodes; 238 239 int lastnodeind() { return nodes.size()-1; } 240 SNode& makenewnode() { nodes.push_back(SNode()); nodes.back().clear(); return nodes.back(); } 241 242 int sprint_node(char* buf, int n, int anc, unsigned int info); 243 int sprint_node_debug(char* buf, const char* pref, int n, int anc); 244 }; 245 246 //---------------------------------------------------------------------------------------------------- 97 247 98 248 inline bool SGraph::visible(int left, int right) … … 101 251 } 102 252 253 //---------------------------------------------------------------------------------------------------- 254 103 255 inline bool SGraph::saturated(int node) 104 256 { … … 106 258 } 107 259 260 //---------------------------------------------------------------------------------------------------- 261 108 262 #endif -
src/dgp/symbol.hh
r5f4d9c3 re7de6cc 3 3 4 4 #include <ext/hash_map> 5 //#include <ext/hash_fun.h>6 5 #include <string> 7 #include < string.h>6 #include <cstring> 8 7 #include <fstream> 9 8 #include <vector> … … 58 57 /// Symbol class template. 59 58 /** The template argument determines the symbol space. 60 Each space is created with symbol "NULL" with index ed0 already in.59 Each space is created with symbol "NULL" with index 0 already in. 61 60 */ 62 61 … … 104 103 105 104 Symbol(const char * s) : val(defs[s]) {}; 106 105 106 Symbol(string s) : val(defs[(char*)s]) {}; 107 108 109 bool empty() const { return val==0; } 110 107 111 /// Symbol to char* conversion. If symbol is invalid, NULL is returned. 108 112 const char* str() const { return (val>=0 && val<count())?defs[val]:NULL; }; … … 113 117 s=0; while(++s) ... 114 118 */ 119 115 120 (operator int)() const { return val; }; 116 121 -
src/dgp/thesymbols.hh
r5f4d9c3 re7de6cc 9 9 #include <bitset> 10 10 11 typedef Symbol<1> Cat;11 using namespace std; 12 12 13 typedef Symbol<2> Role; 14 typedef list<Role> RoleList; 15 typedef list<Role>::iterator RoleListIter; 16 typedef bitset<MAXTYPES> RoleSet; 17 typedef set<Role> Roles; 18 typedef Roles::iterator RolesIter; 13 typedef Symbol<1> Cat; 14 typedef bitset<MAXCATS> CatSet; 19 15 20 typedef Symbol<3> Constr; 21 typedef list<Constr> ConstrList; 16 typedef Symbol<2> Role; 17 typedef list<Role> RoleList; 18 typedef list<Role>::iterator RoleListIter; 19 typedef bitset<MAXTYPES> RoleSet; 20 typedef set<Role> Roles; 21 typedef Roles::iterator RolesIter; 22 23 typedef Symbol<3> Constr; 24 typedef list<Constr> ConstrList; 22 25 typedef list<Constr>::iterator ConstrListIter; 23 26 24 typedef Symbol<4> Rel; 27 typedef Symbol<4> LongRel; 28 typedef set<LongRel> LongRels; 25 29 26 typedef Symbol<5> Flag; 27 typedef bitset<MAXFLAGS> FlagSet; 30 typedef Symbol<5> Flag; 31 typedef bitset<MAXFLAGS> FlagSet; 32 33 typedef Symbol<6> Prop; 34 typedef bitset<MAXPROPS> PropSet; 28 35 29 36 #endif
Note: See TracChangeset
for help on using the changeset viewer.