|
|||||
|
|||||
Ruby Parsec
Jparsec is now ported to Ruby, with a way lighter syntax than Java. Calculator demoThe following is the ruby version of calculator: As simple as that! S-expression demoThe above example utilizes the pre-built Expressions class to help building expression parser. Another example is for a simpler s-expression parser (in lisp syntax, "- (+ 1 (* 2 2)) (1)" sort of thing). As s-expression is way simpler to parse than expressions with infix operators, we will build the parser without using Expressions class: The code is pretty self-explanatory. The only thing to note is that we had to manually skip whitespaces using the "ignore" function. (Whitespace skipping can be more elegant if we do a 2-phase parsing, where lexer takes care of them before grammar rule kicks in. Such use is demonstrated in detail in the test code.) More examplesTake a look at the SQL Parser for a more sophisticated parser, where more traditional lex/parse approach is used. Installing rparsecrparsec can be downloaded at http://rubyforge.org/frs/?group_id=2326 Rdoc is available online. Created by benyu benyu |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||