Fleet  0.0.9
Inference in the LOT
BasicEnumeration.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "EnumerationInference.h"
4 
5 // We require that each NT leads to a nonterminal -- if not, we
6 // can run into problem where some integers do not map to trees. In this case,
7 // we throw this assumption
8 struct EnumerationNotInjectiveException : std::exception {};
9 
10 template<typename Grammar_t>
12 public:
13 
14  Grammar_t* grammar;
15 
16  BasicEnumeration(Grammar_t* g) : grammar(g) {}
17 
30  virtual Node toNode(IntegerizedStack& is, const nonterminal_t& nt) {
31 
32  // NOTE: for now this doesn't work when nt only has finitely many expansions/trees
33  // below it. Otherwise we'll get an assertion error eventually.
34  enumerationidx_t numterm = this->grammar->count_terminals(nt);
35  if(is.get_value() < numterm) {
36  return this->grammar->makeNode(this->grammar->get_rule(nt, is.get_value())); // whatever terminal we wanted
37  }
38  else {
39  // Otherwise it is encoding numterm+which_terminal
40  is -= numterm;
41 
42  const auto numnonterm = this->grammar->count_nonterminals(nt);
43  if(numnonterm == 0) throw EnumerationNotInjectiveException();
44 
45  auto ri = is.pop(numnonterm); // this already includes the shift from all the nonterminals
46 
47  Rule* r = this->grammar->get_rule(nt, ri+numterm); // shift index from terminals (because they are first!)
48  Node out = this->grammar->makeNode(r);
49 
50  int i=0;
51  for(auto v : is.split(r->N)) {
52  // note that this recurses on the enumerationidx_t format -- so it makes a new IntegerizedStack for each child
53  out.set_child(i, toNode(v, r->type(i)) );
54  i++;
55  }
56 
57  return out;
58  }
59  }
60 
61  [[nodiscard]] virtual Node toNode(enumerationidx_t z, const nonterminal_t nt) {
62  IntegerizedStack is(z);
63  return toNode(is, nt);
64  }
65 
66  [[nodiscard]] virtual Node toNode(const nonterminal_t nt, enumerationidx_t z) {
67  // include this because we are SO bad at getting the order right
68  assert(false && "*** You got the order of nonterminal_t and enumerationidx_t wrong and I'm really friendly because I'm here just to tell you that you should switch the order. ");
69  }
70 
71  virtual enumerationidx_t toInteger(const Node& n) {
72  // inverse of the above function -- what order would we be enumerated in?
73  if(n.nchildren() == 0) {
74  return this->grammar->get_index_of(n.rule);
75  }
76  else {
77  // here we work backwards to encode
78  nonterminal_t nt = n.rule->nt;
79  enumerationidx_t numterm = this->grammar->count_terminals(nt);
80 
81  IntegerizedStack is(toInteger(n.child(n.rule->N-1)));
82  for(int i=n.rule->N-2;i>=0;i--) {
83  is.push(toInteger(n.child(i)));
84  }
85  is.push(this->grammar->get_index_of(n.rule)-numterm, this->grammar->count_nonterminals(nt));
86  is += numterm;
87  return is.get_value();
88  }
89  }
90 };
91 
value_t pop()
Definition: IntegerizedStack.h:82
Grammar_t * grammar
Definition: BasicEnumeration.h:14
Definition: Node.h:22
nonterminal_t type(size_t i) const
Definition: Rule.h:152
Definition: Rule.h:21
value_t get_value() const
Definition: IntegerizedStack.h:124
std::vector< value_t > split(size_t n)
Split into n children – this is the same as looping and popping, but provides a nicer interface NOTE...
Definition: IntegerizedStack.h:114
virtual size_t count_terminals() const
Definition: BaseNode.h:388
size_t N
Definition: Rule.h:29
virtual Node toNode(IntegerizedStack &is, const nonterminal_t &nt)
This is a handy function to enumerate trees produced by the grammar. This works by encoding each tree...
Definition: BasicEnumeration.h:30
Definition: BasicEnumeration.h:8
virtual Node toNode(const nonterminal_t nt, enumerationidx_t z)
Definition: BasicEnumeration.h:66
void push(value_t x)
Definition: IntegerizedStack.h:94
this_t & child(const size_t i)
Definition: BaseNode.h:175
const Rule * rule
Definition: Node.h:32
unsigned short nonterminal_t
Definition: Nonterminal.h:4
void set_child(const size_t i, Node &n)
Definition: Node.h:88
BasicEnumeration(Grammar_t *g)
Definition: BasicEnumeration.h:16
virtual Node toNode(enumerationidx_t z, const nonterminal_t nt)
Definition: BasicEnumeration.h:61
nonterminal_t nt
Definition: Rule.h:27
size_t enumerationidx_t
Definition: IntegerizedStack.h:3
Definition: IntegerizedStack.h:12
Definition: BasicEnumeration.h:11
size_t nchildren() const
Definition: BaseNode.h:208
virtual enumerationidx_t toInteger(const Node &n)
Definition: BasicEnumeration.h:71