Created
February 18, 2015 05:00
-
-
Save jdecuirm/3148f3c1b3d1c687fda8 to your computer and use it in GitHub Desktop.
Not getting the logic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative 'sentence.rb' | |
| #Check the type of the word and returns it. | |
| def peek(word_list) | |
| if word_list | |
| word = word_list[0] | |
| word[0] | |
| else | |
| nil | |
| end | |
| end | |
| #If an array of words exists, it pops out the first tuple([type,word]) | |
| #and comparing the type with word[0](this gets the type of the word in the tuple) | |
| #If they are equal, it will return the word tuple([type,word]), if not, returns nil | |
| #shift method shrink the array by popping out the first element of the array. | |
| def match(word_list, expecting) | |
| if word_list | |
| word = word_list.shift | |
| if word[0] == expecting | |
| word | |
| else | |
| nil | |
| end | |
| else | |
| nil | |
| end | |
| end | |
| #As peek return the type of the word, it is compared with word_type | |
| #if the type is equal, match method is called with the array of words and the type. | |
| def skip(word_list,word_type) | |
| while peek(word_list) == word_type | |
| match(word_list, word_type) | |
| end | |
| end | |
| def parse_verb(word_list) | |
| skip(word_list,'stop') | |
| if peek(word_list) == 'verb' | |
| match(word_list,'verb') | |
| else | |
| raise ParserError.new("Expected a verb next.") | |
| end | |
| end | |
| def parse_object(word_list) | |
| skip(word_list,'stop') | |
| next_word = peek(word_list) | |
| if next_word == 'noun' | |
| match(word_list,'noun') | |
| elsif next_word == 'direction' | |
| match(word_list,'direction') | |
| else | |
| raise ParserError.new("Expected a noun for direction next.") | |
| end | |
| end | |
| def parse_subject(word_list) | |
| skip(word_list, 'stop') | |
| next_word = peek(word_list) | |
| if next_word == 'noun' | |
| match(word_list, 'noun') | |
| elsif next_word == 'verb' | |
| ['noun','player'] | |
| else | |
| raise ParserError.new("Expected a verb next.") | |
| end | |
| end | |
| def parse_sentence(word_list) | |
| subj = parse_subject(word_list) | |
| verb = parse_verb(word_list) | |
| obj = parse_object(word_list) | |
| Sentence.new(subj,verb,obj) | |
| end | |
| p x = parse_sentence([['verb','eats'],['stop','a'],['noun','sandwich']]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ParserError < Exception | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Sentence | |
| attr_accessor :subject, :verb, :obj | |
| def initialize(subject, verb, obj) | |
| @subject = subject[1] | |
| @verb = verb[1] | |
| @obj = obj[1] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment