final class TerminationCost
Restricted visibility: declared "@visibility namespace". Code outside that scope must not name this declaration.

Answers the least a rule can cost to finish.

A walk that has to stop soon needs to know which alternative gets there first, and that depends on every rule below it. The answer is found by assuming nothing terminates and then relaxing: each pass takes the cheapest alternative of each rule under the current answers, and the passes repeat until none of them changes anything. A rule that never settles below the sentinel can only expand into itself and so can never be finished at all.

The same relaxation answers two different questions depending on what a step is counted as: tokens written, or rules expanded. What each costs is given to the constructor rather than written twice.

Methods§

public function __construct(
    Grammar $grammar,
    private Closure(string): bool $terminalSupported,
    private int $perTerminal,
    private int $perExpansion,
)
public function of(string $symbol): int

Answers the least one symbol can cost to finish.

A symbol the grammar declares no rule for is a token, and costs what reaching a token costs — unless nothing can write it, in which case it cannot be finished at all.

Parameters

$symbolstringSymbol to cost

Returns

int Least it can cost, or PHP_INT_MAX when it can never be finished
Test cases 4
Called from 1
public function ofProduction(Production $production): int

Private surface 4§

Implementation details, listed for orientation only.

private array<string, int> $minimum
private Closure(string): bool $terminalSupported
private int $perTerminal
private int $perExpansion

Test cases 25§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 8
Other tests reaching this symbol 17

Relations§

Instantiated in 2
Method calls 3
Type declarations 2