Last active
October 9, 2015 01:57
-
-
Save practicingruby/9532497 to your computer and use it in GitHub Desktop.
This file contains 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
# Defines a parser roughly equivalent to the regular expression | |
# pattern /(a|c)*abb/ | |
# | |
# To try it out, run the following commands: | |
# | |
# $ gem install racc | |
# $ racc parser_example.y -o parser | |
# $ ruby parser abb | |
# $ ruby parser acccaaaabb | |
# $ ruby parser zaaa | |
# | |
# The first two strings should parse correctly, the | |
# last one should result in an error being raised. | |
class Parser | |
rule | |
string | |
| a_or_cs abb | |
| abb | |
; | |
a_or_cs | |
: a_or_cs a_or_c | |
| a_or_c | |
; | |
a_or_c : 'a' | 'c' ; | |
abb : 'a' 'b' 'b' { puts "I found abb!" }; | |
end | |
---- inner | |
require "strscan" | |
def next_token | |
c = @ss.getch | |
c ? [c, c] : nil | |
end | |
def parse(text) | |
@ss = StringScanner.new(text) | |
do_parse | |
end | |
---- footer | |
parser = Parser.new | |
# raises Racc::ParseError on failed match | |
parser.parse(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It wasn't hard to find but your page link is lost https://gist.githubusercontent.com/sandal/9532497/raw/8e3bb03fc24c8f6604f96516bf242e7e13d0f4eb/parser_example.y
Remember the article by Aaron Patterson?
Practicing-Ruby/v6-001-parsing-json.html is my own file but that's the topic.
It should be in your archives by now.
http://myrackapps.herokuapp.com/docs/Ruby/html/Practicing-Ruby/v6-001-parsing-json.html