Derivation
SRToolkit.utils.grammar.derivation
Stateful leftmost derivation for CFG/PCFG grammars.
Provides Derivation. Obtain a derivation by calling Grammar.start_derivation rather than constructing Derivation directly.
The engine threads per-slot local state and per-derivation global state through every registered Constraint. See SRToolkit.utils.grammar.constraints for the full protocol documentation.
Derivation
Stateful leftmost derivation over a Grammar.
A derivation begins at a start non-terminal and proceeds by repeatedly choosing a rule to apply to the leftmost unexpanded non-terminal. options returns candidate rules filtered by all registered constraints; apply advances the derivation by one production.
Per-slot local state and per-derivation global state for each registered constraint are maintained internally; constraint instances carry only construction-time configuration and are safe to share across parallel derivations.
Obtain a Derivation via Grammar.start_derivation.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["x"]))
>>> d = g.start_derivation("E")
>>> d.complete
False
>>> opts = d.options()
>>> len(opts)
1
>>> d.apply(opts[0])
>>> d.complete
True
>>> d.to_token_list()
['x']
Source code in SRToolkit/utils/grammar/derivation.py
complete
property
local_stack
Return the local state stack for constraint across the open frontier,
leftmost slot first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint
|
Constraint
|
A constraint previously registered on the grammar. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
One entry per open non-terminal in left-to-right order. |
Source code in SRToolkit/utils/grammar/derivation.py
global_state
Return the per-derivation global state for constraint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint
|
Constraint
|
A constraint previously registered on the grammar. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The current global value owned by this derivation for |
Source code in SRToolkit/utils/grammar/derivation.py
options
Return candidate rules for the current leftmost unexpanded non-terminal, filtered by every registered constraint's allows.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["x"]))
>>> g.add_rule(Rule("E", ["y"]))
>>> d = g.start_derivation("E")
>>> len(d.options())
2
Returns:
| Type | Description |
|---|---|
list[Rule]
|
List of Rule objects that every |
list[Rule]
|
constraint accepts. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the derivation is already complete. |
Source code in SRToolkit/utils/grammar/derivation.py
apply
Apply rule to the current leftmost unexpanded non-terminal.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["E", "+", "F"]))
>>> g.add_rule(Rule("E", ["x"]))
>>> g.add_rule(Rule("F", ["y"]))
>>> d = g.start_derivation("E")
>>> d.apply(g.rules_for("E")[0]) # E -> E + F
>>> d.apply(g.rules_for("E")[1]) # E -> x
>>> d.apply(g.rules_for("F")[0]) # F -> y
>>> d.to_token_list()
['x', '+', 'y']
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
Rule
|
A rule whose |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the derivation is already complete. |
ValueError
|
If |
Source code in SRToolkit/utils/grammar/derivation.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
sample
Apply one rule chosen proportionally by weight (PCFG) or uniformly (CFG) from the surviving candidates.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["x"]))
>>> d = g.start_derivation("E")
>>> d.sample()
>>> d.complete
True
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the derivation is already complete. |
RuntimeError
|
If all candidate rules are filtered out by allows. |
Source code in SRToolkit/utils/grammar/derivation.py
generate
Run the derivation to completion and return the token list.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["x"]))
>>> g.start_derivation("E").generate()
['x']
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int
|
Maximum number of rule applications. A negative value means
unlimited. Default |
1000
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Flat list of terminal tokens in left-to-right order. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the derivation does not complete within
|
Source code in SRToolkit/utils/grammar/derivation.py
to_token_list
Return the completed expression as a flat token list.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["x"]))
>>> d = g.start_derivation("E")
>>> d.apply(d.options()[0])
>>> d.to_token_list()
['x']
Returns:
| Type | Description |
|---|---|
list[str]
|
Flat list of terminal tokens in left-to-right order. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the derivation is not yet complete. |
Source code in SRToolkit/utils/grammar/derivation.py
to_parse_tree
Return the completed derivation as a ParseTree.
Examples:
>>> from SRToolkit.utils.grammar import Grammar, Rule
>>> g = Grammar()
>>> g.add_rule(Rule("E", ["x"]))
>>> d = g.start_derivation("E")
>>> d.apply(d.options()[0])
>>> isinstance(d.to_parse_tree(), ParseTree)
True
Returns:
| Type | Description |
|---|---|
ParseTree
|
The ParseTree rooted at the |
ParseTree
|
start symbol. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the derivation is not yet complete. |