Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/7ca09373e9af5b477190 to your computer and use it in GitHub Desktop.
Save JoshCheek/7ca09373e9af5b477190 to your computer and use it in GitHub Desktop.
Exploring how to parse `word space word` with a student.
# Parses word lists without doing anything fancy
require 'treetop' # => true
Treetop.load_from_string <<GRAMMAR # => AParser
grammar A
rule word_list
word (' ' word)* {
def eval
head = elements[0].eval
tail = elements[1].elements.map { |space_word| space_word.elements[1].eval }
[head] + tail
end
}
end
rule word
[a-z]+ {
def eval
"EVALUATED(\#{text_value})"
end
}
end
end
GRAMMAR
# word list
AParser.new.parse('hello world how are you') # => SyntaxNode+WordList2+WordList1 offset=0, "...lo world how are you" (eval,word):\n SyntaxNode+Word0 offset=0, "hello" (eval):\n SyntaxNode offset=0, "h"\n SyntaxNode offset=1, "e"\n SyntaxNode offset=2, "l"\n SyntaxNode offset=3, "l"\n SyntaxNode offset=4, "o"\n SyntaxNode offset=5, " world how are you":\n SyntaxNode+WordList0 offset=5, " world" (word):\n SyntaxNode...
.eval # => ["EVALUATED(hello)", "EVALUATED(world)", "EVALUATED(how)", "EVALUATED(are)", "EVALUATED(you)"]
# many words
AParser.new.parse('hello world ') # => nil
# same thing, but we name some of the elements and use some fancy Ruby syntax
require 'treetop' # => true
Treetop.load_from_string <<GRAMMAR # => AParser
grammar A
rule word_list
head:word tail_words:(' ' word)* {
def eval
[head.eval, *tail.map(&:eval)]
end
def tail
tail_words.elements.map(&:word)
end
}
end
rule word
[a-z]+ {
def eval
"EVALUATED(\#{text_value})"
end
}
end
end
GRAMMAR
# word list
AParser.new.parse('hello world how are you') # => SyntaxNode+WordList2+WordList1 offset=0, "...lo world how are you" (eval,tail,head,tail_words):\n SyntaxNode+Word0 offset=0, "hello" (eval):\n SyntaxNode offset=0, "h"\n SyntaxNode offset=1, "e"\n SyntaxNode offset=2, "l"\n SyntaxNode offset=3, "l"\n SyntaxNode offset=4, "o"\n SyntaxNode offset=5, " world how are you":\n SyntaxNode+WordList0 offset=5, " world" (word):\n...
.eval # => ["EVALUATED(hello)", "EVALUATED(world)", "EVALUATED(how)", "EVALUATED(are)", "EVALUATED(you)"]
# many words
AParser.new.parse('hello world ') # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment