SHARE
TWEET

Untitled

a guest Oct 13th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.   1 class ParserError(Exception):
  3.   2     pass
  4.   3
  5.   4 class Sentence(object):
  6.   5
  7.   6     def __init__(self, subject, verb, obj):
  8.   7         self.subject = subject[1]
  9.   8         self.verb = verb[1]
  10.   9         self.object = obj[1]
  11.  10
  12.  11 class action_methods(object):
  13.  12
  14.  13     def __init__(self, word_list, word_type):
  15.  14         self.word_list = word_list
  16.  15         self.word_type = word_type
  17.  16         self.peek()
  18.  17         self.match()
  19.  18         self.skip()
  20.  19
  21.  20     def peek(self):
  22.  21         if word_list:
  23.  22             word = word_list[0]
  24.  23             return word[0]
  25.  24         else:
  26.  25             return None
  27.  26
  28.  27     def match(self, expecting):
  29.  28         if word_list:
  30.  29             word = word_list.pop(0)
  31.  30
  32.  31             if word[0] == expecting:
  33.  32                 return word
  34.  33             else:
  35.  34                 return None
  36.  35         else:
  37.  36             return None
  38.  37
  39.  38     def skip(self):
  40.  39         while peek(word_list) == word_type:
  41.  40             match(word_list, word_type)
  42.  41
  43.  42 class parse_methods(object):
  44.  43
  45.  44     def __init__(self, actions, word_list):
  46.  45         #TODO fix error with arguments
  47.  46         self.word_list = word_list
  48.  47         self.actions = actions
  49.  41
  50.  42 class parse_methods(object):
  51.  43
  52.  44     def __init__(self, actions, word_list):
  53.  45         #TODO fix error with arguments
  54.  46         self.word_list = word_list
  55.  47         self.actions = actions
  56.  48         self.parse_verb()
  57.  49         self.parse_object()
  58.  50         self.parse_subject()
  59.  51
  60.  52     def parse_verb(self):
  61.  53         self.actions.skip(self.word_list, 'stop')
  62.  54
  63.  55         if actions.peek(word_list) == 'verb':
  64.  56             return self.actions.match(word_list, 'verb')
  65.  57         else:
  66.  58             raise ParserError("Expected a verb next.")
  67.  59
  68.  60     def parse_object(self):
  69.  61         actions.skip(word_list, 'stop')
  70.  62         next_word = actions.peek(word_list)
  71.  63         if next_word == 'noun':
  72.  64             return actions.match(word_list, 'noun')
  73.  65         elif next_word == 'direction':
  74.  66             return actions.match(word_list, 'direction')
  75.  67         else:
  76.  68             raise ParserError("Expected a noun or direction next.")
  77.  69
  78.  70     def parse_subject(self):
  79.  71         actions.skip(word_list, 'stop')
  80.  72         next_word = actions.peek(word_list)
  81.  73
  82.  74         if next_word == 'noun':
  83.  75             return actions.match(word_list, 'noun')
  84.  76         elif next_word == 'verb':
  85.  77             return ('noun', 'player')
  86.  78         else:
  87.  79             raise ParserError("Expected a verb next.")
  88.  80
  89.  81 def parse_sentence(word_list):
  90.  82     parser = parse_methods(action_methods(), word_list)
  91.  83     subj = methods.parse_subject(word_list)
  92.  84     verb = methods.parse_verb(word_list)
  93.  85     obj = methods.parse_object(word_list)
  94.  86
  95.  87     return Sentence(subj, verb, obj)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top