class documentation

A ShiftReduceParser that allows you to setp through the parsing process, performing a single operation at a time. It also allows you to change the parser's grammar midway through parsing a text.

The initialize method is used to start parsing a text. shift performs a single shift operation, and reduce performs a single reduce operation. step will perform a single reduce operation if possible; otherwise, it will perform a single shift operation. parses returns the set of parses that have been found by the parser.

See Also
nltk.grammar
Method __init__ Create a new ShiftReduceParser, that uses grammar to parse texts.
Method initialize Start parsing a given text. This sets the parser's stack to [] and sets its remaining text to tokens.
Method parse When possible this list is sorted from most likely to least likely.
Method parses No summary
Method reduce Use production to combine the rightmost stack elements into a single Tree. If production does not match the rightmost stack elements, then do nothing.
Method reducible_productions No summary
Method remaining_text No summary
Method set_grammar Change the grammar used to parse texts.
Method shift Move a token from the beginning of the remaining text to the end of the stack. If there are no more tokens in the remaining text, then do nothing.
Method stack No summary
Method step Perform a single parsing operation. If a reduction is possible, then perform that reduction, and return the production that it is based on. Otherwise, if a shift is possible, then perform it, and return True...
Method undo Return the parser to its state before the most recent shift or reduce operation. Calling undo repeatedly return the parser to successively earlier states. If no shift or reduce operations have been performed, ...
Instance Variable _grammar Undocumented
Instance Variable _history A list of (stack, remaining_text) pairs, containing all of the previous states of the parser. This history is used to implement the undo operation.
Instance Variable _remaining_text Undocumented
Instance Variable _stack Undocumented

Inherited from ShiftReduceParser:

Method grammar No summary
Method trace Set the level of tracing output that should be generated when parsing a text.
Method _check_grammar Check to make sure that all of the CFG productions are potentially useful. If any productions can never be used, then print a warning.
Method _match_rhs No summary
Method _reduce Find a CFG production whose right hand side matches the rightmost stack elements; and combine those stack elements into a single Tree, with the node specified by the production's left-hand side. If more than one CFG production matches the stack, then use the production that is listed earliest in the grammar...
Method _shift Move a token from the beginning of remaining_text to the end of stack.
Method _trace_reduce Print trace output displaying that production was used to reduce stack.
Method _trace_shift Print trace output displaying that a token has been shifted.
Method _trace_stack Print trace output displaying the given stack and text.
Instance Variable _trace Undocumented

Inherited from ParserI (via ShiftReduceParser):

Method parse_all No summary
Method parse_one No summary
Method parse_sents Apply self.parse() to each element of sents. :rtype: iter(iter(Tree))
def __init__(self, grammar, trace=0): (source)

Create a new ShiftReduceParser, that uses grammar to parse texts.

Parameters
grammar:GrammarThe grammar used to parse texts.
trace:intThe level of tracing that should be used when parsing a text. 0 will generate no tracing output; and higher numbers will produce more verbose tracing output.
def initialize(self, tokens): (source)

Start parsing a given text. This sets the parser's stack to [] and sets its remaining text to tokens.

def parse(self, tokens): (source)

When possible this list is sorted from most likely to least likely.

Parameters
tokensUndocumented
sent:list(str)The sentence to be parsed
Returns
iter(Tree)An iterator that generates parse trees for the sentence.
def parses(self): (source)
Returns
iter(Tree)An iterator of the parses that have been found by this parser so far.
def reduce(self, production=None): (source)

Use production to combine the rightmost stack elements into a single Tree. If production does not match the rightmost stack elements, then do nothing.

Returns
Production or NoneThe production used to reduce the stack, if a reduction was performed. If no reduction was performed, return None.
def reducible_productions(self): (source)
Returns
list(Production)A list of the productions for which reductions are available for the current parser state.
def remaining_text(self): (source)
Returns
list(str)The portion of the text that is not yet covered by the stack.
def set_grammar(self, grammar): (source)

Change the grammar used to parse texts.

Parameters
grammar:CFGThe new grammar.
def shift(self): (source)

Move a token from the beginning of the remaining text to the end of the stack. If there are no more tokens in the remaining text, then do nothing.

Returns
boolTrue if the shift operation was successful.
def stack(self): (source)
Returns
list(str and Tree)The parser's stack.
def step(self): (source)

Perform a single parsing operation. If a reduction is possible, then perform that reduction, and return the production that it is based on. Otherwise, if a shift is possible, then perform it, and return True. Otherwise, return False.

Returns
Production or boolFalse if no operation was performed; True if a shift was performed; and the CFG production used to reduce if a reduction was performed.
def undo(self): (source)

Return the parser to its state before the most recent shift or reduce operation. Calling undo repeatedly return the parser to successively earlier states. If no shift or reduce operations have been performed, undo will make no changes.

Returns
booltrue if an operation was successfully undone.
_history: list = (source)

A list of (stack, remaining_text) pairs, containing all of the previous states of the parser. This history is used to implement the undo operation.

_remaining_text = (source)

Undocumented

_stack: list = (source)

Undocumented