src
Public Member Functions | Public Attributes | List of all members
re2c::DFA Class Reference

#include <adfa.h>

Collaboration diagram for re2c::DFA:
Collaboration graph
[legend]

Public Member Functions

 DFA (const dfa_t &dfa, const std::vector< size_t > &fill, Skeleton *skel, const charset_t &charset, const std::string &n, const std::string &c, uint32_t l)
 
 ~DFA ()
 
void reorder ()
 
void prepare ()
 
void calc_stats ()
 
void emit (Output &, uint32_t &, bool, bool &)
 

Public Attributes

const std::string name
 
const std::string cond
 
const uint32_t line
 
uint32_t lbChar
 
uint32_t ubChar
 
uint32_t nStates
 
Statehead
 
size_t max_fill
 
bool need_backup
 
bool need_backupctx
 
bool need_accept
 

Detailed Description

Definition at line 53 of file adfa.h.

Constructor & Destructor Documentation

re2c::DFA::DFA ( const dfa_t dfa,
const std::vector< size_t > &  fill,
Skeleton skel,
const charset_t charset,
const std::string &  n,
const std::string &  c,
uint32_t  l 
)

Definition at line 17 of file adfa.cc.

25  : accepts ()
26  , skeleton (skel)
27  , name (n)
28  , cond (c)
29  , line (l)
30  , lbChar(0)
31  , ubChar(charset.back())
32  , nStates(0)
33  , head(NULL)
34 
35  // statistics
36  , max_fill (0)
37  , need_backup (false)
38  , need_backupctx (false)
39  , need_accept (false)
40 {
41  const size_t nstates = dfa.states.size();
42  const size_t nchars = dfa.nchars;
43 
44  State **i2s = new State*[nstates];
45  for (size_t i = 0; i < nstates; ++i)
46  {
47  i2s[i] = new State;
48  }
49 
50  State **p = &head;
51  for (size_t i = 0; i < nstates; ++i)
52  {
53  dfa_state_t *t = dfa.states[i];
54  State *s = i2s[i];
55 
56  ++nStates;
57  *p = s;
58  p = &s->next;
59 
60  s->isPreCtxt = t->ctx;
61  s->rule = t->rule;
62  s->fill = fill[i];
63  s->go.span = allocate<Span>(nchars);
64  uint32_t j = 0;
65  for (uint32_t c = 0; c < nchars; ++j)
66  {
67  const size_t to = t->arcs[c];
68  for (;++c < nchars && t->arcs[c] == to;);
69  s->go.span[j].to = to == dfa_t::NIL ? NULL : i2s[to];
70  s->go.span[j].ub = charset[c];
71  }
72  s->go.nSpans = j;
73  }
74  *p = NULL;
75 
76  delete[] i2s;
77 }
const uint32_t line
Definition: adfa.h:61
size_t max_fill
Definition: adfa.h:69
const std::string cond
Definition: adfa.h:60
uint32_t lbChar
Definition: adfa.h:63
bool need_backup
Definition: adfa.h:70
bool need_backupctx
Definition: adfa.h:71
uint32_t ubChar
Definition: adfa.h:64
State * head
Definition: adfa.h:66
static const size_t NIL
Definition: dfa.h:38
uint32_t nStates
Definition: adfa.h:65
bool need_accept
Definition: adfa.h:72
const std::string name
Definition: adfa.h:59
re2c::DFA::~DFA ( )

Definition at line 79 of file adfa.cc.

80 {
81  State *s;
82 
83  while ((s = head))
84  {
85  head = s->next;
86  delete s;
87  }
88 
89  delete skeleton;
90 }
State * head
Definition: adfa.h:66
State * next
Definition: adfa.h:27

Member Function Documentation

void re2c::DFA::calc_stats ( )

Definition at line 240 of file prepare.cc.

241 {
242  // calculate 'YYMAXFILL'
243  max_fill = 0;
244  for (State * s = head; s; s = s->next)
245  {
246  if (max_fill < s->fill)
247  {
248  max_fill = s->fill;
249  }
250  }
251 
252  // determine if 'YYMARKER' or 'YYBACKUP'/'YYRESTORE' pair is used
253  need_backup = accepts.size () > 0;
254 
255  // determine if 'YYCTXMARKER' or 'YYBACKUPCTX'/'YYRESTORECTX' pair is used
256  for (State * s = head; s; s = s->next)
257  {
258  if (s->isPreCtxt)
259  {
260  need_backupctx = true;
261  }
262  }
263 
264  // determine if 'yyaccept' variable is used
265  need_accept = accepts.size () > 1;
266 }
size_t max_fill
Definition: adfa.h:69
bool need_backup
Definition: adfa.h:70
bool need_backupctx
Definition: adfa.h:71
State * head
Definition: adfa.h:66
size_t size() const
Definition: uniq_vector.h:21
bool need_accept
Definition: adfa.h:72
State * next
Definition: adfa.h:27

Here is the call graph for this function:

Here is the caller graph for this function:

void re2c::DFA::emit ( Output output,
uint32_t &  ind,
bool  isLastCond,
bool &  bPrologBrace 
)

Definition at line 115 of file emit_dfa.cc.

116 {
117  OutputFile & o = output.source;
118 
119  bool bProlog = (!opts->cFlag || !bWroteCondCheck);
120 
121  // start_label points to the beginning of current re2c block
122  // (prior to condition dispatch in '-c' mode)
123  // it can forced by configuration 're2c:startlabel = <integer>;'
124  label_t start_label = o.label_counter.next ();
125  // initial_label points to the beginning of DFA
126  // in '-c' mode this is NOT equal to start_label
127  label_t initial_label = bProlog && opts->cFlag
128  ? o.label_counter.next ()
129  : start_label;
130  for (State * s = head; s; s = s->next)
131  {
132  s->label = o.label_counter.next ();
133  }
134  std::set<label_t> used_labels;
135  count_used_labels (used_labels, start_label, initial_label, o.get_force_start_label ());
136 
137  head->action.set_initial (initial_label, head->action.type == Action::SAVE);
138 
139  skeleton->warn_undefined_control_flow ();
140  skeleton->warn_unreachable_rules ();
141  skeleton->warn_match_empty ();
142 
143  if (opts->target == opt_t::SKELETON)
144  {
145  if (output.skeletons.insert (name).second)
146  {
147  skeleton->emit_data (o.file_name);
149  uint32_t i = 2;
150  emit_body (o, i, used_labels, initial_label);
151  skeleton->emit_end (o, need_backup, need_backupctx);
152  }
153  }
154  else
155  {
156  // Generate prolog
157  if (bProlog)
158  {
159  o.ws("\n").wdelay_line_info ();
160  if (opts->target == opt_t::DOT)
161  {
162  bPrologBrace = true;
163  o.ws("digraph re2c {\n");
164  }
165  else if ((!opts->fFlag && o.get_used_yyaccept ())
166  || (!opts->fFlag && opts->bEmitYYCh)
167  || (opts->bFlag && !opts->cFlag && BitMap::first)
168  || (opts->cFlag && !bWroteCondCheck && opts->gFlag)
169  || (opts->fFlag && !bWroteGetState && opts->gFlag)
170  )
171  {
172  bPrologBrace = true;
173  o.wind(ind++).ws("{\n");
174  }
175  else if (ind == 0)
176  {
177  ind = 1;
178  }
179  if (!opts->fFlag && opts->target != opt_t::DOT)
180  {
181  if (opts->bEmitYYCh)
182  {
183  o.wind(ind).wstring(opts->yyctype).ws(" ").wstring(opts->yych).ws(";\n");
184  }
185  o.wdelay_yyaccept_init (ind);
186  }
187  else
188  {
189  o.ws("\n");
190  }
191  }
192  if (opts->bFlag && !opts->cFlag && BitMap::first)
193  {
194  BitMap::gen(o, ind, lbChar, ubChar <= 256 ? ubChar : 256);
195  }
196  if (bProlog)
197  {
198  if (opts->cFlag && !bWroteCondCheck && opts->gFlag)
199  {
200  genCondTable(o, ind, output.types);
201  }
202  o.wdelay_state_goto (ind);
203  if (opts->cFlag && opts->target != opt_t::DOT)
204  {
205  if (used_labels.count(start_label))
206  {
207  o.wstring(opts->labelPrefix).wlabel(start_label).ws(":\n");
208  }
209  }
210  o.wuser_start_label ();
211  if (opts->cFlag && !bWroteCondCheck)
212  {
213  genCondGoto(o, ind, output.types);
214  }
215  }
216  if (opts->cFlag && !cond.empty())
217  {
218  if (opts->condDivider.length())
219  {
220  o.wstring(replaceParam(opts->condDivider, opts->condDividerParam, cond)).ws("\n");
221  }
222  if (opts->target == opt_t::DOT)
223  {
224  o.wstring(cond).ws(" -> ").wlabel(head->label).ws("\n");
225  }
226  else
227  {
228  o.wstring(opts->condPrefix).wstring(cond).ws(":\n");
229  }
230  }
231  if (opts->cFlag && opts->bFlag && BitMap::first)
232  {
233  o.wind(ind++).ws("{\n");
234  BitMap::gen(o, ind, lbChar, ubChar <= 256 ? ubChar : 256);
235  }
236  // Generate code
237  emit_body (o, ind, used_labels, initial_label);
238  if (opts->cFlag && opts->bFlag && BitMap::first)
239  {
240  o.wind(--ind).ws("}\n");
241  }
242  // Generate epilog
243  if ((!opts->cFlag || isLastCond) && bPrologBrace)
244  {
245  o.wind(--ind).ws("}\n");
246  }
247  }
248 
249  // Cleanup
250  if (BitMap::first)
251  {
252  delete BitMap::first;
253  BitMap::first = NULL;
254  }
255 }
bool bFlag
Definition: opt.h:118
bool bWroteGetState
Definition: main.cc:17
size_t max_fill
Definition: adfa.h:69
std::string replaceParam(std::string str, const std::string &param, const _Ty &value)
Definition: emit.h:26
std::string condPrefix
Definition: opt.h:118
static void genCondTable(OutputFile &o, uint32_t ind, const std::vector< std::string > &condnames)
Definition: emit_dfa.cc:257
const std::string cond
Definition: adfa.h:60
std::string yych
Definition: opt.h:118
static void genCondGoto(OutputFile &o, uint32_t ind, const std::vector< std::string > &condnames)
Definition: emit_dfa.cc:309
static void gen(OutputFile &, uint32_t ind, uint32_t, uint32_t)
Definition: bitmap.cc:75
std::string condDivider
Definition: opt.h:118
void emit_end(OutputFile &o, bool backup, bool backupctx) const
void emit_start(OutputFile &o, size_t maxfill, bool backup, bool backupctx, bool accept) const
bool bWroteCondCheck
Definition: main.cc:18
bool cFlag
Definition: opt.h:118
uint32_t lbChar
Definition: adfa.h:63
void warn_match_empty()
Definition: match_empty.cc:14
opt_t::target_t target
Definition: opt.h:118
bool gFlag
Definition: opt.h:118
Action action
Definition: adfa.h:33
bool need_backup
Definition: adfa.h:70
std::string labelPrefix
Definition: opt.h:118
bool need_backupctx
Definition: adfa.h:71
uint32_t ubChar
Definition: adfa.h:64
bool fFlag
Definition: opt.h:118
label_t label
Definition: adfa.h:25
State * head
Definition: adfa.h:66
Opt opts
Definition: opt.cc:7
static BitMap * first
Definition: bitmap.h:19
void emit_data(const char *fname)
bool bEmitYYCh
Definition: opt.h:118
void warn_unreachable_rules()
Definition: unreachable.cc:37
void warn_undefined_control_flow()
Definition: control_flow.cc:43
void set_initial(label_t label, bool used_marker)
Definition: action.h:59
enum re2c::Action::type_t type
bool need_accept
Definition: adfa.h:72
std::string condDividerParam
Definition: opt.h:118
std::string yyctype
Definition: opt.h:118
const std::string name
Definition: adfa.h:59
State * next
Definition: adfa.h:27

Here is the call graph for this function:

void re2c::DFA::prepare ( )

Definition at line 140 of file prepare.cc.

141 {
142  bUsedYYBitmap = false;
143 
144  // create rule states
145  std::map<rule_rank_t, State *> rules;
146  for (State * s = head; s; s = s->next)
147  {
148  if (s->rule)
149  {
150  if (rules.find (s->rule->rank) == rules.end ())
151  {
152  State *n = new State;
153  n->action.set_rule (s->rule);
154  rules[s->rule->rank] = n;
155  addState(n, s);
156  }
157  for (uint32_t i = 0; i < s->go.nSpans; ++i)
158  {
159  if (!s->go.span[i].to)
160  {
161  s->go.span[i].to = rules[s->rule->rank];
162  }
163  }
164  }
165  }
166 
167  // create default state (if needed)
168  State * default_state = NULL;
169  for (State * s = head; s; s = s->next)
170  {
171  for (uint32_t i = 0; i < s->go.nSpans; ++i)
172  {
173  if (!s->go.span[i].to)
174  {
175  if (!default_state)
176  {
177  default_state = new State;
178  addState(default_state, s);
179  }
180  s->go.span[i].to = default_state;
181  }
182  }
183  }
184 
185  // find backup states and create accept state (if needed)
186  if (default_state)
187  {
188  for (State * s = head; s; s = s->next)
189  {
190  if (s->rule)
191  {
192  for (uint32_t i = 0; i < s->go.nSpans; ++i)
193  {
194  if (!s->go.span[i].to->rule && s->go.span[i].to->action.type != Action::RULE)
195  {
196  const uint32_t accept = static_cast<uint32_t> (accepts.find_or_add (rules[s->rule->rank]));
197  s->action.set_save (accept);
198  }
199  }
200  }
201  }
202  default_state->action.set_accept (&accepts);
203  }
204 
205  // split ``base'' states into two parts
206  for (State * s = head; s; s = s->next)
207  {
208  s->isBase = false;
209 
210  if (s->fill != 0)
211  {
212  for (uint32_t i = 0; i < s->go.nSpans; ++i)
213  {
214  if (s->go.span[i].to == s)
215  {
216  s->isBase = true;
217  split(s);
218 
219  if (opts->bFlag)
220  {
221  BitMap::find(&s->next->go, s);
222  }
223 
224  s = s->next;
225  break;
226  }
227  }
228  }
229  }
230 
231  // find ``base'' state, if possible
232  findBaseState();
233 
234  for (State * s = head; s; s = s->next)
235  {
236  s->go.init (s);
237  }
238 }
bool bFlag
Definition: opt.h:118
bool bUsedYYBitmap
Definition: main.cc:16
State * head
Definition: adfa.h:66
Opt opts
Definition: opt.cc:7
static const BitMap * find(const Go *, const State *)
Definition: bitmap.cc:30
size_t find_or_add(const value_t &v)
Definition: uniq_vector.h:29
State * next
Definition: adfa.h:27

Here is the call graph for this function:

Here is the caller graph for this function:

void re2c::DFA::reorder ( )

Definition at line 92 of file adfa.cc.

93 {
94  std::vector<State*> ord;
95  ord.reserve(nStates);
96 
97  std::queue<State*> todo;
98  todo.push(head);
99 
100  std::set<State*> done;
101  done.insert(head);
102 
103  for(;!todo.empty();)
104  {
105  State *s = todo.front();
106  todo.pop();
107  ord.push_back(s);
108  for(uint32_t i = 0; i < s->go.nSpans; ++i)
109  {
110  State *q = s->go.span[i].to;
111  if(q && done.insert(q).second)
112  {
113  todo.push(q);
114  }
115  }
116  }
117 
118  assert(nStates == ord.size());
119 
120  ord.push_back(NULL);
121  for(uint32_t i = 0; i < nStates; ++i)
122  {
123  ord[i]->next = ord[i + 1];
124  }
125 }
State * head
Definition: adfa.h:66
uint32_t nStates
Definition: adfa.h:65

Here is the caller graph for this function:

Member Data Documentation

const std::string re2c::DFA::cond

Definition at line 60 of file adfa.h.

State* re2c::DFA::head

Definition at line 66 of file adfa.h.

uint32_t re2c::DFA::lbChar

Definition at line 63 of file adfa.h.

const uint32_t re2c::DFA::line

Definition at line 61 of file adfa.h.

size_t re2c::DFA::max_fill

Definition at line 69 of file adfa.h.

const std::string re2c::DFA::name

Definition at line 59 of file adfa.h.

bool re2c::DFA::need_accept

Definition at line 72 of file adfa.h.

bool re2c::DFA::need_backup

Definition at line 70 of file adfa.h.

bool re2c::DFA::need_backupctx

Definition at line 71 of file adfa.h.

uint32_t re2c::DFA::nStates

Definition at line 65 of file adfa.h.

uint32_t re2c::DFA::ubChar

Definition at line 64 of file adfa.h.


The documentation for this class was generated from the following files: