I am trying to create a text parser that will allow limited custom replacement rules.
In particular, I am reading codes from a DOS ASCII file in which the ordering is significant and line numbering should be supported. Using this input, I want to apply custom substitution rules (exchange this line for this line if we see that this line, followed by this line, performs this translation, etc.).
The output is also a DOS ASCII formatted file.
Most rules directly replace tit to replace tat, however there are situations when I want to define a rule, for example, if A follows B at any time in the future, apply this rule.
For this, I use a tree of structures as such:
struct node { list<string> common;
At any time when I come across such a rule, I can support the output with the rule to be omitted and applied, delaying the decision to use it until it is unambiguously resolved.
A bit of memory is wasteful, but it seems like the best way to avoid having to skip the input twice (the size of the input is unknown, but probably less than 1 megabyte).
Of course, such a case may exist if another rule of this type is run in one or both of the child nodes, therefore the structure of the tree.
There are no restrictions on the fact that children must be resolved in front of their parents; it is possible that a parent can be allowed in only one branch of the child. The EOF meeting will resolve all unresolved children in a false direction.
Thus, I have to be careful when rewinding and folding nodes.
Is there a simpler solution to this common problem? Is there a way to use standard library containers in a more efficient way than my tree?