Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parsers(object):
- def parse_verb(self, word_list):
- skip(word_list, 'stop')
- if peek(word_list) == 'verb':
- return match(word_list, 'verb')
- else:
- raise ParserError("Expected a verb next")
- def parse_object(self, word_list):
- skip(word_list, 'stop')
- next = peek(word_list)
- if next == 'noun':
- return match(word_list, 'noun')
- if next == 'direction':
- return match(word_list, 'direction')
- else:
- raise ParserError("Expected a noun or direction next")
- def parse_subject(self, word_list, subj):
- verb = parse_verb(word_list)
- obj = parse_object(word_list)
- return Sentence(subj, verb, obj)
- def parse_sentence(self, word_list):
- skip(word_list, 'stop')
- start = peek(word_list)
- if start == 'noun':
- subj = match(word_list, 'noun')
- return parse_subject(word_list, subj)
- elif start == 'verb':
- # assume the subject is the player then
- return parse_subject(word_list, ('noun', 'player'))
- else:
- raise ParserError("Must start with the subject, object, or verb not: %s" % start)
Advertisement
Add Comment
Please, Sign In to add comment