Skip to content

Instantly share code, notes, and snippets.

@manveru
Created November 24, 2010 12:44
Show Gist options
  • Save manveru/713591 to your computer and use it in GitHub Desktop.
Save manveru/713591 to your computer and use it in GitHub Desktop.
Minimal treetop example
require 'treetop'
grammar = Treetop.load_from_string(DATA.read)
parser = MyGrammarParser.new
p parser.parse(" a = 1 \n b = 2 \n").value
__END__
grammar MyGrammar
rule main
pair* {
def value
elements.map(&:key_value)
end
}
end
rule pair
space keyword:(keyword) space '=' space value:(value) space eol {
def key_value
[keyword.text_value, value.text_value]
end
}
end
rule space
' '+
end
rule keyword
[a-z]+
end
rule value
[0-9]+
end
rule eol
"\n"+
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment