class documentation

A simple bottom-up CFG parser that uses two operations, "shift" and "reduce", to find a single parse for a text.

ShiftReduceParser maintains a stack, which records the structure of a portion of the text. This stack is a list of strings and Trees that collectively cover a portion of the text. For example, while parsing the sentence "the dog saw the man" with a typical grammar, ShiftReduceParser will produce the following stack, which covers "the dog saw":

[(NP: (Det: 'the') (N: 'dog')), (V: 'saw')]

ShiftReduceParser attempts to extend the stack to cover the entire text, and to combine the stack elements into a single tree, producing a complete parse for the sentence.

Initially, the stack is empty. It is extended to cover the text, from left to right, by repeatedly applying two operations:

  • "shift" moves a token from the beginning of the text to the end of the stack.
  • "reduce" uses a CFG production to combine the rightmost stack elements into a single Tree.

Often, more than one operation can be performed on a given stack. In this case, ShiftReduceParser uses the following heuristics to decide which operation to perform:

  • Only shift if no reductions are available.
  • If multiple reductions are available, then apply the reduction whose CFG production is listed earliest in the grammar.

Note that these heuristics are not guaranteed to choose an operation that leads to a parse of the text. Also, if multiple parses exists, ShiftReduceParser will return at most one of them.

See Also
nltk.grammar
Method __init__ Create a new ShiftReduceParser, that uses grammar to parse texts.
Method grammar No summary
Method parse When possible this list is sorted from most likely to least likely.
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 _grammar Undocumented
Instance Variable _trace Undocumented

Inherited from ParserI:

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 grammar(self): (source)
Returns
The grammar used by this parser.
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 trace(self, trace=2): (source)

Set the level of tracing output that should be generated when parsing a text.

Parameters
trace:intThe trace level. A trace level of 0 will generate no tracing output; and higher trace levels will produce more verbose tracing output.
Returns
NoneUndocumented
def _check_grammar(self): (source)

Check to make sure that all of the CFG productions are potentially useful. If any productions can never be used, then print a warning.

Returns
NoneUndocumented
def _match_rhs(self, rhs, rightmost_stack): (source)
Parameters
rhs:list(terminal and Nonterminal)The right hand side of a CFG production.
rightmost_stack:list(string and Tree)The rightmost elements of the parser's stack.
Returns
booltrue if the right hand side of a CFG production matches the rightmost elements of the stack. rhs matches rightmost_stack if they are the same length, and each element of rhs matches the corresponding element of rightmost_stack. A nonterminal element of rhs matches any Tree whose node value is equal to the nonterminal's symbol. A terminal element of rhs matches any string whose type is equal to the terminal.
def _reduce(self, stack, remaining_text, production=None): (source)

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. The new Tree replaces the elements in the stack.

Parameters
stack:list(string and Tree)A list of strings and Trees, encoding the structure of the text that has been parsed so far.
remaining_text:list(str)The portion of the text that is not yet covered by stack.
productionUndocumented
Returns
Production or NoneIf a reduction is performed, then return the CFG production that the reduction is based on; otherwise, return false.
def _shift(self, stack, remaining_text): (source)

Move a token from the beginning of remaining_text to the end of stack.

Parameters
stack:list(str and Tree)A list of strings and Trees, encoding the structure of the text that has been parsed so far.
remaining_text:list(str)The portion of the text that is not yet covered by stack.
Returns
NoneUndocumented
def _trace_reduce(self, stack, production, remaining_text): (source)

Print trace output displaying that production was used to reduce stack.

Returns
NoneUndocumented
def _trace_shift(self, stack, remaining_text): (source)

Print trace output displaying that a token has been shifted.

Returns
NoneUndocumented
def _trace_stack(self, stack, remaining_text, marker=' '): (source)

Print trace output displaying the given stack and text.

Parameters
stackUndocumented
remaining_textUndocumented
markerA character that is printed to the left of the stack. This is used with trace level 2 to print 'S' before shifted stacks and 'R' before reduced stacks.
Returns
NoneUndocumented

Undocumented