I get syntax errors from RDParser example I found on internet

Discussion in 'Ruby' started by wbsurfver@yahoo.com, Nov 28, 2007.

  1. Guest

    =begin
    I got this recursive decent parser from:
    http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html

    I get this syntax error:
    C:/rb-play/parser/try.rb:13: syntax error, unexpected ']', expecting
    kEND
    g.operation /[+-*/]/

    if I change that line to:
    g.operation /[+-]/
    that line no longer has an error, so the / and * threw it off

    then I get an error on another line:

    g.string %r(["'](.*?[^\]|.*?)["'])
    C:/rb-play/parser/try.rb:15: warning: character class has `[' without
    escape
    C:/rb-play/parser/try.rb:15: unmatched :) /["'](.*?[^\]|.*?)["']/

    Any ideas on what to change to make it work ?

    thanks

    =end

    require 'rdparser'

    parser = RDParser.new do |g|
    g.main 'line(s)'
    g.line 'expression separator(?) comment(?)'
    g.comment '"#" rest_of_line'
    g.rest_of_line /.+$/
    g.separator /;/
    g.expression 'term operation expression | term'
    g.term 'number | variable | string | brkt_expression'
    g.brkt_expression '"(" expression ")"'
    g.number /d+(.d+)?/
    g.operation /[+-*/]/
    g.variable /[a-z][a-z0-9]*/
    g.string %r(["'](.*?[^\]|.*?)["'])
    end

    content = %q{
    (34 - 3) * 42; # Comment here..
    "a" + "bcd"
    }

    syntax_tree = parser.parse:)main, content)
    puts RDParser.text_syntax_tree(syntax_tree)
    , Nov 28, 2007
    #1
    1. Advertising

  2. yermej Guest

    On Nov 28, 2:59 pm, "" <> wrote:
    > =begin
    > I got this recursive decent parser from:http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html
    >
    > I get this syntax error:
    > C:/rb-play/parser/try.rb:13: syntax error, unexpected ']', expecting
    > kEND
    > g.operation /[+-*/]/
    >
    > if I change that line to:
    > g.operation /[+-]/
    > that line no longer has an error, so the / and * threw it off
    >
    > then I get an error on another line:
    >
    > g.string %r(["'](.*?[^\]|.*?)["'])
    > C:/rb-play/parser/try.rb:15: warning: character class has `[' without
    > escape
    > C:/rb-play/parser/try.rb:15: unmatched :) /["'](.*?[^\]|.*?)["']/
    >


    In the first, you need the - at the beginning of the (or escaped with
    a backslash) or Ruby interprets it as a character range. You also need
    to escape the first forward slash or Ruby interprets it as the end of
    the Regex. So:

    /[-+*\/]/

    In the second, the backslash needs to be escaped. If it isn't, then it
    is used to escape the following ] so the character class isn't closed
    until the final ] which is just wrong. So:

    %r(["'](.*?[^\\]|.*?)["'])

    Consider both issues were due to missing backslashes, they were
    probably originally correct. I guess somewhere along the way the
    backslashes were interpreted as escape characters and didn't make it
    to the final page.

    Jeremy
    yermej, Nov 28, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 28, 5:04 pm, yermej <> wrote:
    > On Nov 28, 2:59 pm, "" <> wrote:
    >
    >
    >
    > > =begin
    > > I got this recursive decent parser from:http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html

    >
    > > I get this syntax error:
    > > C:/rb-play/parser/try.rb:13: syntax error, unexpected ']', expecting
    > > kEND
    > > g.operation /[+-*/]/

    >
    > > if I change that line to:
    > > g.operation /[+-]/
    > > that line no longer has an error, so the / and * threw it off

    >
    > > then I get an error on another line:

    >
    > > g.string %r(["'](.*?[^\]|.*?)["'])
    > > C:/rb-play/parser/try.rb:15: warning: character class has `[' without
    > > escape
    > > C:/rb-play/parser/try.rb:15: unmatched :) /["'](.*?[^\]|.*?)["']/

    >
    > In the first, you need the - at the beginning of the (or escaped with
    > a backslash) or Ruby interprets it as a character range. You also need
    > to escape the first forward slash or Ruby interprets it as the end of
    > the Regex. So:
    >
    > /[-+*\/]/
    >
    > In the second, the backslash needs to be escaped. If it isn't, then it
    > is used to escape the following ] so the character class isn't closed
    > until the final ] which is just wrong. So:
    >
    > %r(["'](.*?[^\\]|.*?)["'])
    >
    > Consider both issues were due to missing backslashes, they were
    > probably originally correct. I guess somewhere along the way the
    > backslashes were interpreted as escape characters and didn't make it
    > to the final page.
    >
    > Jeremy


    That first one I had tried before and it didn't work, but if I move
    the * to the begining it does work:
    /[*+-\/]/
    , Nov 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    942
    Mark Goldin
    Jan 17, 2004
  2. cmay
    Replies:
    0
    Views:
    1,129
  3. loial
    Replies:
    1
    Views:
    2,947
    Tris Orendorff
    Apr 28, 2006
  4. yawnmoth
    Replies:
    97
    Views:
    4,702
    Bent C Dalager
    Feb 27, 2009
  5. John Smith
    Replies:
    1
    Views:
    139
    Evertjan.
    Mar 12, 2005
Loading...

Share This Page