Guest User

parser_def

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