Package td_parser :: Module td_parser :: Class Parser
[hide private]
[frames] | no frames]

Class Parser

source code

Class for top down recursive descent parser.

Instance Methods [hide private]
  __init__(self, grammar, string='')
A parser instance must have a grammar at instance creation time.
  recognize_string(self, string='')
  recurse_recognize(self, goals, agenda, wordlist)
Parse wordlist using goals (derivation).
  expand(self, next_goal, rest_goals, agenda, wordlist)
Expand next_goal using grammar (self.productions) Generate new GoalList using one production and add unused productions to agenda.
  match_word(self, next_goal, wordlist, rest_goals, agenda)
next_goal is a terminal.
  backtrack(self, agenda)
Suppose agenda is non-empty:
  parse_string(self, string='')
Not yet implemented
  trace(self, boolean=True)
  trace_match(self, boolean, next_goal, word, rest_goals, rest_wordlist)
  trace_expand(self, next_goal, next_production, goals, agenda, wordlist)
  trace_backtrack(self, goals, wordlist)
  string_to_wordlist(self, string)
  legal_input(self, string='')
  recursion_check(self)

Method Details [hide private]

__init__(self, grammar, string='')
(Constructor)

source code 

A parser instance must have a grammar at instance creation time. The terminals, productions, and start_cat for the grammar are stored upon creation.

If a string is supplied it is converted to a list words and stored in self.input. A string may also be supplied when the recognizer- or parser- method is called.

recognize_string(self, string='')

source code 
Parameters:
  • string - A string. The string to be parsed. Immediately converted to a [WordList].by self.legal_input.
Returns:
The result of calling recurse_recognize on the start-cat, the empty agenda, and the wordlist generated from string.

recurse_recognize(self, goals, agenda, wordlist)

source code 

Parse wordlist using goals (derivation). Return true if current grammar accepts wordlist. Else False.

Return True whenever goals and wordlist are empty.

Suppose goals is non-empty:

If goals[0] is a nonterminal, call expand (expand it with the grammar and continue parsing recursively). If goals[0] is a terminal then call match_word (try to match the next word and continue parsing). Both expand and match_word contain recursive calls to recurse_recognize.

Suppose goals is empty:

Then if wordlist is empty, return True. Else backtrack (If {agenda} is non-empty continue parsing with the next state on the agenda; else return False) backtrack also contains a recursive call to recurse_recognize.
Parameters:
  • goals - A list of categories and/or words proposed to cover wordlist, a partial derivation from the grammar.
  • agenda - A list of ParserStates: each a pair of ([GoalList],[WordList])
  • wordlist - a list of words.

expand(self, next_goal, rest_goals, agenda, wordlist)

source code 
Expand next_goal using grammar (self.productions) Generate new GoalList using one production and add unused productions to agenda. Keep parsing with new GoalList, new agenda, old WordList.
Parameters:
  • next_goal - grammar nonterminal
  • rest_goals - other gramar elements from the same gramar production
  • agenda - list of parser states.
  • wordlist - list of words (strings).

match_word(self, next_goal, wordlist, rest_goals, agenda)

source code 

next_goal is a terminal.

Suppose wordlist is non-empty:

If next_goal matches wordlist[0], keep parsing with rest_goals, agenda and wordlist[1:].

Else this parse path fails; call backtrack (if agenda is non-empty, continue parsing with the next parser state on agenda, and if agenda is empty, <Return>: False)

Suppose wordlist is empty.

This parse path fails. Call backtrack.
Parameters:
  • next_goal - grammar preterminal
  • wordlist - list of words (strings).
  • rest_goals - other gramar elements from the same gramar production
  • agenda - list of parser states.

backtrack(self, agenda)

source code 

Suppose agenda is non-empty:

Then:
 agenda[0].GoalList = new goals
 agenda[0].WordList = new wordlist

Keep parsing with new goals and new worldlist and popped agenda.

Suppose agenda is empty

return False

parse_string(self, string='')

source code 
Not yet implemented

trace(self, boolean=True)

source code 
None

trace_match(self, boolean, next_goal, word, rest_goals, rest_wordlist)

source code 
None

trace_expand(self, next_goal, next_production, goals, agenda, wordlist)

source code 
None

trace_backtrack(self, goals, wordlist)

source code 
None

string_to_wordlist(self, string)

source code 
None

legal_input(self, string='')

source code 
None

recursion_check(self)

source code 
None