source: src/dgp/grammar.cc @ a15e59b

Last change on this file since a15e59b was a15e59b, checked in by Tomasz Obrebski <to@…>, 12 years ago

dodana opcja --time w dgp, poprawione przesy�anie b�belk�w, obsluga &LEFT, &RIGHT

  • Property mode set to 100644
File size: 14.8 KB
Line 
1
2#include <cstdio>
3
4#include "grammar.hh"
5
6//bool (*constraint[MAXCONSTRS])(int head, int dep);
7
8//====================================================================================================
9//inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10bool chk_type(const char* s) { return Role::index(s)>0; } // PRZENIEŠÆ DO Role
11//----------------------------------------------------------------------------------------------------
12//inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13bool chk_long(const char* s) { return LongRel::index(s)>0; } // jw
14//----------------------------------------------------------------------------------------------------
15//inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16bool chk_cat(const char* s) { return Cat::index(s)>0; } //jw
17//----------------------------------------------------------------------------------------------------
18//inline !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19bool chk_flag(const char* s) { return Flag::index(s)>0; } //jw
20//====================================================================================================
21
22void grammar_error(int lineno, string s="Grammar error.")
23{
24  fprintf(stderr,"%8d: %s Line ignored.\n",lineno,s.c_str());
25}
26
27//====================================================================================================
28
29void Grammar::add_category(const char* s)
30{
31  Cat::add(s);
32
33  if(connect.size() <= Cat::count())
34  {
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);
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);
60}
61
62void Grammar::add_type(const char* s)
63{ 
64  Role::add(s);
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
79bool 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
88void 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//====================================================================================================
100
101void Grammar::set_lt(Role s, Role t)
102{
103  lt[s].set(t);
104  gt[t].set(s);
105  if(s==0||(int)t==0)
106    return;
107  else
108  {
109    for(int i=0; i<Role::count(); ++i)
110      if(lt[i][s])
111        set_lt(i,t);
112    for(int i=0; i<Role::count(); ++i)
113      if(lt[t][i])
114        set_lt(s,i);
115  }
116} 
117
118
119void Grammar::compute_gt()
120{
121  for(Role s=0; s<Role::count(); ++s)
122    for(Role t=0; t<Role::count(); ++t)
123      if(lt[s][t])
124        gt[t].set(s);
125}
126
127
128void 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
147list<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
158Flag 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
170PropSet 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
182bool Grammar::read(FILE* f)
183{
184
185  //>>> TU?
186
187  Prop::add("INIT");
188  Prop::add("FIN");
189  Prop::add("LEFT");
190  Prop::add("RIGHT");
191
192  //<<< TU?
193
194
195  int lineno=0;
196  char line[MAXLINE]; // line has the structure: key [arg1 [arg2 [arg3]]]
197  char key[MAXLINE];
198  char arg1[MAXLINE];
199  char arg2[MAXLINE];
200  char arg3[MAXLINE];
201  char arg4[MAXLINE];
202
203  while(fgets(line,MAXLINE,f))
204    {
205      lineno++;
206      int fields=sscanf(line,"%s %s %s %s %s",key,arg1,arg2,arg3,arg4);
207     
208       if(fields<1 || key[0]=='#') continue; // skip empty lines and comments
209
210      if(fields>1 && arg1[0] == '#') fields=1;
211      if(fields>2 && arg2[0] == '#') fields=2;
212      if(fields>3 && arg3[0] == '#') fields=3;
213      if(fields>4 && arg4[0] == '#') fields=4;
214     
215      if     (strcmp(key,"CAT")==0 && fields==2)
216        {
217          add_category(arg1);
218        }
219      else if(strcmp(key,"ROLE")==0 && fields==2)
220        {
221          add_type(arg1);
222        }
223      else if(strcmp(key,"SGL")==0 && fields==2)
224        { 
225          if(chk_type(arg1))
226            set_sgl(arg1);
227          else
228            grammar_error(lineno);
229        }
230      else if(strcmp(key,"LEFT")==0 && fields==2)
231        { 
232          if(chk_type(arg1))
233            set_left(arg1);
234          else
235            grammar_error(lineno);
236        }
237      else if(strcmp(key,"RIGHT")==0 && fields==2)
238        {
239          if(chk_type(arg1))
240            set_right(arg1);
241          else
242            grammar_error(lineno);
243        }
244      else if(strcmp(key,"INITR")==0 && fields==2)
245        {
246          if(chk_type(arg1))
247            set_init(arg1);
248          else
249            grammar_error(lineno);
250        }
251      else if(strcmp(key,"FINR")==0 && fields==2)
252        {
253          if(chk_type(arg1))
254            set_fin(arg1);
255          else
256            grammar_error(lineno);
257        }
258      else if(strcmp(key,"INITF")==0 && fields==2)
259        {
260          if(chk_flag(arg1))
261            set_initf(arg1);
262          else
263            grammar_error(lineno);
264        }
265      else if(strcmp(key,"FINF")==0 && fields==2)
266        {
267          if(chk_flag(arg1))
268            set_finf(arg1);
269          else
270            grammar_error(lineno);
271        }
272      else if(strcmp(key,"REQ")==0 && fields==3)
273        {
274          if( chk_cat(arg1) && chk_type(arg2) )
275            set_obl(arg1,arg2);
276          else
277            grammar_error(lineno);
278        }
279      else if(strcmp(key,"CONSTRE")==0 && fields==3)
280        {
281          if( chk_type(arg1) && chk_type(arg2) )
282            set_exclude(arg1,arg2);
283          else
284            grammar_error(lineno);
285        }
286      else if(strcmp(key,"CONSTRI")==0 && fields==3)
287        {
288          if( chk_type(arg1) && chk_type(arg2) )
289            set_include(arg1,arg2);
290          else
291            grammar_error(lineno);
292        }
293      else if(strcmp(key,"LONG")==0 &&  fields ==3)
294        {
295            add_long(arg1,arg2);
296        }
297      else if(strcmp(key,"LINK")==0 && fields==4)
298        { 
299          char cat1[MAXLINE],flags1[MAXLINE],cat2[MAXLINE],flags2[MAXLINE],type[MAXLINE],props[MAXLINE];
300
301          if(sscanf(arg1,"%[^;];%s",cat1,flags1)==1) *flags1='\0'; 
302          if(sscanf(arg2,"%[^;];%s",cat2,flags2)==1) *flags2='\0'; 
303          if(sscanf(arg3,"%[^&]%s",type,props)==1) *props='\0';
304         
305          //      printf("line=%s\n\tcat1=%s flags1=%s cat2=%s flags2=%s type=%s props=%s\n",line,cat1,flags1,cat2,flags2,type,props);
306
307          if( chk_cat(cat1) && chk_cat(cat2) && chk_type(type) )
308            set_connect(cat1,parse_flags(flags1,"+"),parse_flags(flags1,"-"),cat2,parse_flags(flags2,"+"),parse_flags(flags2,"-"),type,parse_props(props));
309          else if( chk_cat(cat1) && chk_cat(cat2) && chk_long(type) )
310            {
311              set_longrel(cat1,cat2,type);
312              add_triggers(cat1,cat2,type);
313            }
314          else
315              grammar_error(lineno);
316        }
317      // else if(strcmp(key,"LINK")==0 && fields==5)
318      //        {
319      //          if( chk_cat(arg1) && chk_cat(arg2) && chk_type(arg4) )
320      //            set_connect(arg1,arg2,arg3,arg4);
321      //          else
322      //              grammar_error(lineno);
323      //        }
324      // FLAG DECLARATION
325      else if(strcmp(key,"FLAG")==0 && fields==2)
326        { 
327          add_flag(arg1);
328        }
329      else if(strcmp(key,"SET")==0 && fields==3)
330        {
331          if( chk_cat(arg1) && chk_flag(arg2) )
332            set_set(arg1,arg2);
333          else
334            grammar_error(lineno);
335        }
336      else if(strcmp(key,"PASS")==0 && fields==3)
337        {
338          if( chk_type(arg1) && chk_flag(arg2) )
339            set_pass(arg1,arg2);
340          else
341            grammar_error(lineno);
342        }
343     
344      else fprintf(stderr,"Statement not recognized in line %d. Ignored.\n", lineno);
345    }
346 
347  //   compute_gt();
348 
349  return true;
350 
351}
352
353
354void Grammar::write(ostream& os)
355{
356  for(Cat i=1; i<Cat::count(); ++i)
357    os << "CAT\t" << i.str() << endl;
358
359  for(Role i=1; i<Role::count(); ++i)
360    os << "ROLE\t" << i.str() << endl;
361
362  for(Role i=1; i<Role::count(); ++i)
363    if(sgl.test(i))
364      os << "SGL\t" << i.str() << endl;
365 
366  for(Role i=1; i<Role::count(); ++i)
367    if(left.test(i))
368      os << "LEFT\t" << i.str() << endl;
369
370  for(Role i=1; i<Role::count(); ++i)
371    if(right.test(i))
372      os << "RIGHT\t" << i.str() << endl;
373
374  for(Cat c=1; c<Cat::count(); ++c)
375    for(Role r=1; r<Role::count(); ++r)
376      if(obl[c].test(r))
377        os << "REQ\t" << c.str() << "\t" << r.str() << endl;
378 
379  for(Cat c=1; c<Cat::count(); ++c)
380    for(Cat d=1; d<Cat::count(); ++d)
381      for(Links::const_iterator l = connect1[c][d].begin(); l != connect1[c][d].end(); l++)
382        {
383          os << "LINK\t" << c.str();
384          if(l->hflagplus||l->hflagminus)
385            {
386              os << ";";
387              if(l->hflagplus) os << (l->hflagplus).str() << "+";
388              if(l->hflagminus) os << (l->hflagminus).str() << "-"; 
389            }
390          os << "\t" << d.str();
391          if(l->dflagplus||l->dflagminus)
392            {
393              os << ";";
394              if(l->dflagplus) os << (l->dflagplus).str() << "+";
395              if(l->dflagminus) os << (l->dflagminus).str() << "-"; 
396            }
397          os << "\t" << (l->role).str();
398          for(Prop p=0; p<Prop::count(); ++p)
399            if(l->props[p])
400              os << "&" << p.str();
401          os << endl;
402        }
403
404  for(LongRel i=1; i<LongRel::count(); ++i)
405    os << "LONG\t" << i.str() << endl;
406
407  for(list<Boubble*>::const_iterator b = boubbles.begin(); b != boubbles.end(); b++)
408    os << "BOUBBLE\t" << **b << endl;
409
410  for(Cat c=1; c<Cat::count(); ++c)
411    for(Cat d=1; d<Cat::count(); ++d)
412      for(LongRel l=1; l<LongRel::count(); ++l)
413        if(longrel[c][d].count(l))
414          os << "LLINK\t" << c.str() << "\t" << d.str() << "\t" << l.str() << endl;
415
416  for(Cat c=1; c<Cat::count(); ++c)
417    for(Flag f=1; f<Flag::count(); ++f)
418      if(set[c].test(f))
419        os << "SET\t" << c.str() << "\t" << f.str() << endl;
420
421  for(Role r=1; r<Role::count(); ++r)
422    for(Flag f=1; f<Flag::count(); ++f)
423      if(pass[r].test(f))
424        os << "PASS\t" << r.str() << "\t" << f.str() << endl;
425 
426  for(Role r=1; r<Role::count(); ++r)
427    for(Role t=1; t<Role::count(); ++t)
428      if(include[r].test(t))
429        os << "CONSTRI\t" << r.str() << "\t" << t.str() << endl;
430 
431  for(Role r=1; r<Role::count(); ++r)
432    for(Role t=1; t<Role::count(); ++t)
433      if(exclude[r].test(t))
434        os << "CONSTRE\t" << r.str() << "\t" << t.str() << endl;
435 
436  for(Cat c=1; c<Cat::count(); ++c)
437    for(Role r=1; r<Role::count(); ++r)
438      for(list<Boubble*>::const_iterator b=uptrigger[c][r].begin(); b!=uptrigger[c][r].end(); b++)
439        os << "TRIGGER-UP\t" << c.str() << "\t" << r.str() << "\t" << (*b)->rel().str() << endl;
440
441  for(Cat c=1; c<Cat::count(); ++c)
442    for(Role r=1; r<Role::count(); ++r)
443      for(list<Boubble*>::const_iterator b=dntrigger[c][r].begin(); b!=dntrigger[c][r].end(); b++)
444        os << "TRIGGER-DN\t" << c.str() << "\t" << r.str() << "\t" << (*b)->rel().str() << endl;
445
446  for(Flag i=1; i<Flag::count(); ++i)
447    os << "FLAG\t" << i.str() << endl;
448}
449
450
451
452
453
454
455
456
457
458//================================= OLD
459
460void Grammar::write(FILE* f)
461{
462  for(Cat i=1; i<Cat::count(); ++i)
463    fprintf(f,"CAT\t%s\n",i.str());
464
465  for(Role i=1; i<Role::count(); ++i)
466    fprintf(f,"ROLE\t%s (%d)(%d)\n",i.str(),Role::index(i.str()),chk_type(i.str()));
467
468  for(Role i=1; i<Role::count(); ++i)
469    if(sgl.test(i)) fprintf(f,"SGL\t%s\n",i.str());
470 
471  for(Role i=1; i<Role::count(); ++i)
472    if(left.test(i)) fprintf(f,"LEFT\t%s\n",i.str());
473
474  for(Role i=1; i<Role::count(); ++i)
475    if(right.test(i)) fprintf(f,"RIGHT\t%s\n",i.str());
476
477  for(Cat c=1; c<Cat::count(); ++c)
478    for(Role r=1; r<Role::count(); ++r)
479      if(obl[c].test(r)) fprintf(f,"REQ\t%s\t%s\n",c.str(),r.str());
480 
481  for(Cat c=1; c<Cat::count(); ++c)
482    for(Cat d=1; d<Cat::count(); ++d)
483      for(Role t=1; t<Role::count(); ++t)
484        if(connect[c][d].count(t))
485          fprintf(f,"LINK\t%s\t%s\t%s\n",c.str(),d.str(),t.str());
486
487  for(LongRel i=1; i<LongRel::count(); ++i)
488    fprintf(f,"LONG\t%s\n",i.str());
489
490  for(Cat c=1; c<Cat::count(); ++c)
491    for(Cat d=1; d<Cat::count(); ++d)
492      for(LongRel l=1; l<LongRel::count(); ++l)
493        if(longrel[c][d].count(l))
494          fprintf(f,"LLINK\t%s\t%s\t%s\n",c.str(),d.str(),l.str());
495
496  for(Cat c=1; c<Cat::count(); ++c)
497    for(Role r=1; r<Role::count(); ++r)
498      for(list<Boubble*>::const_iterator b=uptrigger[c][r].begin(); b!=uptrigger[c][r].end(); b++)
499        fprintf(f,"#TRIGGER\t%s\t%s\t%s\n",c.str(),r.str(),(*b)->rel().str());
500
501  for(Cat c=1; c<Cat::count(); ++c)
502    for(Role r=1; r<Role::count(); ++r)
503      for(list<Boubble*>::const_iterator b=dntrigger[c][r].begin(); b!=dntrigger[c][r].end(); b++)
504        fprintf(f,"#TRIGGER\t%s\t%s\t%s\n",c.str(),r.str(),(*b)->rel().str());
505
506  for(Flag i=1; i<Flag::count(); ++i)
507    fprintf(f,"FLAG\t%s\n",i.str());
508}
Note: See TracBrowser for help on using the repository browser.