Skip to content

Commit

Permalink
Fix GeneratorTool.replicate_quantified when token limit is inf (#276)
Browse files Browse the repository at this point in the history
If token limit is inf, an infinite (or rather, NaN) number of
copies could be made of a */+-quantified subtree.
  • Loading branch information
akosthekiss authored Feb 18, 2025
1 parent cdd3d98 commit a678556
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions grammarinator/tool/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,9 @@ def replicate_quantified(self, individual=None, _=None):
recipient_root_tokens < recipient_root_tokens + annot.node_tokens[child] <= self._limit.tokens]
if node_options:
node_to_repeat = random.choice(node_options)
max_repeat = (self._limit.tokens - recipient_root_tokens) // annot.node_tokens[node_to_repeat]
for _ in range(random.randint(1, max_repeat)):
max_repeat = (self._limit.tokens - recipient_root_tokens) // annot.node_tokens[node_to_repeat] if self._limit.tokens != RuleSize.max.tokens else 1
repeat = random.randint(1, max_repeat) if max_repeat > 1 else 1
for _ in range(repeat):
node_to_repeat.parent.insert_child(idx=random.randint(0, len(node_to_repeat.parent.children)), node=deepcopy(node_to_repeat))

# Return with the original root, whether the replication was successful or not.
Expand Down

0 comments on commit a678556

Please sign in to comment.