tdp4r
「^」にも対応 #!/usr/bin/env ruby require 'tdp' require 'tdp/utils' class Calc include TDParser include TDPUtils def input rule(:expr) >> proc {|x| puts "\t#{x}"} end def expr token(:VAR) - token("=") - rule(:expr) >> proc {|x| x[0].value[…
「^」にも対応 #!/usr/bin/env ruby require 'tdp' require 'tdp/utils' class Calc include TDParser include TDPUtils def expr1 rule(:expr2) - ((token("+")|token("-")) - rule(:expr2))*0 >> proc{|x| x[1].inject(x[0]){|n, y| case y[0] when "+" n …
「^」にも対応 #!/usr/bin/env ruby require 'tdp' require 'tdp/utils' class Calc include TDParser include TDPUtils def expr1 rule(:expr2) - ((token("+")|token("-")) - rule(:expr2))*0 >> proc{|x| x[1].inject(x[0]){|n, y| case y[0] when "+" n …
不明点 rule * n..m label stack negative lookahead fail leftrec, rightrec chainl, chainr 逆ポーランド記法電卓って書けるの?(そもそも tdp4r を使って書く意味がなさそうなのだけど) 右結合って書けるの? エラー検出はどうやってやれば?
bison マニュアルの多機能電卓を サンプル sample2.rb を参考に記述 #!/usr/bin/env ruby require 'tdp' require 'tdp/utils' class Calc include TDParser include TDPUtils def input rule(:expr) >> proc {|x| puts "\t#{x}"} end def expr token(:VAR) -…
StringTokenizer でなく、『Rubyを256倍使うための本 無道編』由来の lex を使って #!/usr/bin/env ruby require 'tdp' require 'tdp/utils' class Calc include TDParser include TDPUtils def expr1 rule(:expr2) - ((token("+")|token("-")) - rule(:expr…
#!/usr/bin/env ruby require 'tdp' require 'tdp/utils' parser = TDParser.define {|g| g.input = token(:int) - token("+") - token(:int) >> proc {|x| p x } def parse() input.parse {|x| x.yield(TDPUtils::Token.new(:int, "1")); x.yield("+"); x.y…
bison マニュアルの中間記法電卓を サンプル sample2.rb を参考に記述 #!/usr/bin/env ruby require 'tdp' require 'tdp/utils' class Calc include TDParser include TDPUtils def expr1 rule(:expr2) - ((token("+")|token("-")) - rule(:expr2))*0 >> pro…
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.b = "b" g.c = "c" g.input = g.a - g.b - g.c >> proc {|x| p x } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } parser.parse("a b c") で、 ["a", …
#!/usr/bin/env ruby require 'tdp' require 'tdp/utils' parser = TDParser.define {|g| g.a = "a" g.input = g.aa >> proc {|x| x } g.aa = (g.a - g.aa) | empty() def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("…
#!/usr/bin/env ruby require 'tdp' require 'tdp/utils' parser = TDParser.define {|g| g.a = "a" g.input = g.a - g.token(:int) - g.token("\n") >> proc {|x| x } def parse(str) tokenizer = TDPUtils::StringTokenizer[ /\d+/ => :int, ] #input.pars…
#!/usr/bin/env ruby require 'tdp' require 'tdp/utils' parser = TDParser.define {|g| g.a = "a" g.input = g.a - g.token(:int) >> proc {|x| x } def parse(str) tokenizer = TDPUtils::StringTokenizer[ /\d+/ => :int, ] input.parse(tokenizer.gener…
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.b = "b" g.c = "c" g.d = "d" g.input = g.leftrec(g.a, g.b, g.c) {|x| p x } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("a b");…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = ((token(/\d+/) - token("b") - token("c")) | (token(/./) - token("b") - token("d"))) >> proc {|x| [x[0], x[1], x[2]] } def parse(str) tokens = str.sp…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = token("a") - token("b") - token("c") - fail() >> proc {|x| [x[0], x[1], x[2]] } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = ((token("a") - token("b") - token("c")) | empty()) >> proc {|x| [x[0], x[1], x[2]] } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) en…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = token("a") - token("b") - token("c") - none() >> proc {|x| [x[0], x[1], x[2]] } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = any() - token("b") - token("c") >> proc {|x| [x[0], x[1], x[2]] } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse(…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = (rule(:abc) | rule(:def)) >> proc {|x| x } def abc token("a") - token("b") - token("c") >> proc {|x| x } end def def token("d") - token("e") - token…
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.a = "a" g.b = "b" g.c = "c" g.d = "d" g.e = "e" g.f = "f" g.input = (rule(:abc) | rule(:def)) >> proc {|x| x } g.abc = g.a - g.b - g.c >> proc {|x| x } g.de…
label, stack, negative lookahead というものは良く分からないのでとばす。 #!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = token("a") - token("b") - token("c") >> proc {|x| [x[0], x[1], x[2]] } def p…
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.input = (g.a * (2..4)) >> proc {|x| x } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("a") p parser.parse("a a") p parser.parse…
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.input = (g.a * 3) >> proc {|x| x } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("a a") p parser.parse("a a a") p parser.parse(…
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.b = "b" g.c = "c" g.d = "d" g.e = "e" g.f = "f" g.input = g.abc | g.def >> proc {|x| x } g.abc = g.a - g.b - g.c >> proc {|x| [x[0], x[1], x[2]] } g.def = g.d - g.…
#!/usr/bin/env ruby require 'tdp' class MyParser include TDParser def input token("a") - token("b") - token("c") >> proc {|x| [x[0], x[1], x[2]] } end def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end end parser = MyParser.n…
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.b = "b" g.c = "c" g.input = g.a - g.b - g.c >> proc {|x| [x[0], x[1], x[2]] } def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("a …
#!/usr/bin/env ruby require 'tdp' parser = TDParser.define {|g| g.a = "a" g.b = "b" g.c = "c" g.input = g.a - g.b - g.c def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("a b c") p parser.parse("a b c") p pa…