Skip to content

Instantly share code, notes, and snippets.

@artob
Created August 19, 2010 17:11
Show Gist options
  • Save artob/538391 to your computer and use it in GitHub Desktop.
Save artob/538391 to your computer and use it in GitHub Desktop.
Possible syntax for parser combinators in Ruby.
module SXP
module Grammar extend Parallax::Grammar
LPAREN = char('(')
RPAREN = char(')')
INTEGER = term(/^([+-]?\d+)/) { |s| s.to_i }
STRING = term(/^"([^"]+)"/) { |s| s }
SYMBOL = term(/^([\w\d_-]+)/) { |s| s.to_sym }
Atom = INTEGER | STRING | SYMBOL
List = LPAREN << +Atom >> RPAREN
Nil = LPAREN << RPAREN
Expr = Atom | Nil | List
Top = Expr*n
end
end
input = %q((foo (bar 1 2 3) "Hello, world!"))
p Parallax::Parser.new(SXP::Grammar).parse(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment