Guest User

parser_class

a guest
Oct 31st, 2012
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. class Parsers(object):
  2.  
  3. def parse_verb(self, word_list):
  4. skip(word_list, 'stop')
  5.  
  6. if peek(word_list) == 'verb':
  7. return match(word_list, 'verb')
  8. else:
  9. raise ParserError("Expected a verb next")
  10.  
  11.  
  12. def parse_object(self, word_list):
  13. skip(word_list, 'stop')
  14. next = peek(word_list)
  15. if next == 'noun':
  16. return match(word_list, 'noun')
  17. if next == 'direction':
  18. return match(word_list, 'direction')
  19. else:
  20. raise ParserError("Expected a noun or direction next")
  21.  
  22.  
  23. def parse_subject(self, word_list, subj):
  24. verb = parse_verb(word_list)
  25. obj = parse_object(word_list)
  26.  
  27. return Sentence(subj, verb, obj)
  28.  
  29.  
  30. def parse_sentence(self, word_list):
  31. skip(word_list, 'stop')
  32.  
  33. start = peek(word_list)
  34.  
  35. if start == 'noun':
  36. subj = match(word_list, 'noun')
  37. return parse_subject(word_list, subj)
  38. elif start == 'verb':
  39. # assume the subject is the player then
  40. return parse_subject(word_list, ('noun', 'player'))
  41. else:
  42. raise ParserError("Must start with the subject, object, or verb not: %s" % start)
Advertisement
Add Comment
Please, Sign In to add comment