polytracker.grammars

grammars classes

Completion

class polytracker.grammars.Completion(prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int)

Bases: EarleyState

__init__(prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int)
add_predecessor(left_sibling: EarleyState)
completed_by: Set[EarleyState]
depth: int
expected: Tuple[str | Terminal, ...]
property finished: bool
index: int
property next_element: str | Terminal
parsed: Tuple[str | Terminal, ...]
predecessors: FrozenSet[EarleyState]
prediction: Prediction
property production: Production
to_tree() MutableParseTree[Production | Rule | Terminal]

CorruptedGrammarError

class polytracker.grammars.CorruptedGrammarError

Bases: GrammarError

__init__(*args, **kwargs)
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

DisconnectedGrammarError

class polytracker.grammars.DisconnectedGrammarError

Bases: GrammarError

__init__(*args, **kwargs)
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

EarleyParser

class polytracker.grammars.EarleyParser(grammar: Grammar, sentence: str | bytes, start: Production | None = None)

Bases: object

__init__(grammar: Grammar, sentence: str | bytes, start: Production | None = None)
property end_states: Iterator[EarleyState]
parse() Iterator[ParseTree[Production | Rule | Terminal]]
parse_trees() Iterator[ParseTree[Production | Rule | Terminal]]

Reconstructs all parse trees from the parse

EarleyQueue

class polytracker.grammars.EarleyQueue(parser: EarleyParser)

Bases: object

__init__(parser: EarleyParser)
add(state: S, left_sibling: EarleyState | None = None) S
complete_state(state: EarleyState, completed: EarleyState)
remove(*states: EarleyState | Iterable) int

EarleyState

class polytracker.grammars.EarleyState(prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int)

Bases: object

__init__(prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int)
add_predecessor(left_sibling: EarleyState)
depth: int
expected: Tuple[str | Terminal, ...]
property finished: bool
index: int
property next_element: str | Terminal
parsed: Tuple[str | Terminal, ...]
predecessors: FrozenSet[EarleyState]
prediction: Prediction
property production: Production
abstract to_tree() MutableParseTree[Production | Rule | Terminal]

EmptyProduction

class polytracker.grammars.EmptyProduction(production: Production, prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int)

Bases: EarleyState

__init__(production: Production, prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int)
add_predecessor(left_sibling: EarleyState)
depth: int
expected: Tuple[str | Terminal, ...]
property finished: bool
index: int
property next_element: str | Terminal
parsed: Tuple[str | Terminal, ...]
predecessors: FrozenSet[EarleyState]
prediction: Prediction
property production: Production
to_tree() MutableParseTree[Production | Rule | Terminal]

Grammar

class polytracker.grammars.Grammar

Bases: object

__init__()
dependency_graph() DiGraph[Production]
find_partial_trees(sentence: bytes, start: Production | None = None) Iterator[ParseTree[Production | Rule | Terminal]]

Enumerates all partial parse trees that could result in the given starting sentence fragment.

load(raw_grammar: Dict[str, Any])
match(sentence: str | bytes, start: Production | None = None) Match
remove(production: str | Production) bool
simplify() bool
verify(test_disconnection: bool = True)

GrammarError

class polytracker.grammars.GrammarError

Bases: RuntimeError

__init__(*args, **kwargs)
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Match

class polytracker.grammars.Match(parser: EarleyParser)

Bases: object

__init__(parser: EarleyParser)
property parse_tree: ParseTree[Production | Rule | Terminal] | None

Returns the first parse tree matched

MissingProductionError

class polytracker.grammars.MissingProductionError

Bases: CorruptedGrammarError

__init__(*args, **kwargs)
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

PartialMatch

class polytracker.grammars.PartialMatch(tree: ParseTree[Production | Rule | Terminal], remaining_symbols: Tuple[str | Terminal, ...], remaining_bytes: bytes)

Bases: object

__init__(tree: ParseTree[Production | Rule | Terminal], remaining_symbols: Tuple[str | Terminal, ...], remaining_bytes: bytes)
remaining_bytes: bytes
remaining_symbols: Tuple[str | Terminal, ...]
tree: ParseTree[Production | Rule | Terminal]

Prediction

class polytracker.grammars.Prediction(production: Production, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int, rule: Rule)

Bases: EarleyState

__init__(production: Production, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int, rule: Rule)
add_predecessor(left_sibling: EarleyState)
depth: int
expected: Tuple[str | Terminal, ...]
property finished: bool
index: int
property next_element: str | Terminal
parsed: Tuple[str | Terminal, ...]
predecessors: FrozenSet[EarleyState]
prediction: Prediction
property production: Production
rule: Rule
to_tree() MutableParseTree[Production | Rule | Terminal]

Production

class polytracker.grammars.Production(grammar: Grammar, name: str, *rules: Rule)

Bases: object

__init__(grammar: Grammar, name: str, *rules: Rule)
_propagate_terminals()

Calculates if this production can produce a terminal. If a rule calls another terminal whose _can_produce_terminal member is None, assume that it is False

add(rule: Rule) bool
property can_produce_terminal: bool
first_rule() Rule | None
static load(grammar: Grammar, name: str, *rules: Iterable[str]) Production
partial_match(sentence: bytes) Iterator[PartialMatch]

Enumerates all partial parse trees and remaining symbols that match the given sentence

remove_recursive_rules() Set[Rule]

Removes and returns all rules that solely recursively call this same production

remove_sub_production(name: str)
replace_sub_production(to_replace: str, replace_with: str | Rule)
property used_by: Iterable[Production]

Rule

class polytracker.grammars.Rule(grammar: Grammar, *sequence: str | Terminal)

Bases: object

__init__(grammar: Grammar, *sequence: str | Terminal)
property can_produce_terminal: bool
static combine_terminals(sequence: Iterable[str | Terminal]) Tuple[str | Terminal, ...]
static load(grammar: Grammar, *sequence: str | Terminal) Rule
remove_sub_production(prod_name: str) bool
replace_sub_production(to_replace: str, replace_with: str | Rule) bool

ScannedTerminal

class polytracker.grammars.ScannedTerminal(prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int, terminal: Terminal)

Bases: EarleyState

__init__(prediction: Prediction, parsed: Tuple[str | Terminal, ...], expected: Tuple[str | Terminal, ...], index: int, terminal: Terminal)
add_predecessor(left_sibling: EarleyState)
depth: int
expected: Tuple[str | Terminal, ...]
property finished: bool
index: int
property next_element: str | Terminal
parsed: Tuple[str | Terminal, ...]
predecessors: FrozenSet[EarleyState]
prediction: Prediction
property production: Production
terminal: Terminal
to_tree() MutableParseTree[Production | Rule | Terminal]

grammars functions

extract

polytracker.grammars.extract(traces: Iterable[ProgramTrace], simplify: bool = False) Grammar

extract a grammar from a set of traces

parse_tree_to_grammar

polytracker.grammars.parse_tree_to_grammar(tree: NonGeneralizedParseTree) Grammar

production_name

polytracker.grammars.production_name(event: TraceEvent) str

to_dot

polytracker.grammars.to_dot(graph: DiGraph, comment: str | None = None) Digraph
Parameters:

comment – comment for the graph

Returns:

Graphviz DiGraph

This function creates a dot object which can be saved to disk and converted to PDF its a visualization of the chain fragment, useful for visualizing a reorg.

trace_to_grammar

polytracker.grammars.trace_to_grammar(trace: ProgramTrace) Grammar